Basic SAS Functions in Version 8.2 Kim Michalski Office of the Actuary Rick Andrews Office of Research, Development, and Information.

Slides:



Advertisements
Similar presentations
Dates and Times SAS Date, Time and Date- Time Formats.
Advertisements

CS&E 1111 Exfunctions Using Functions in Excel Objectives: Using Excel functions l SUM, MIN, MAX, AVERAGE, COUNT, COUNTA l ROUND l COUNTIF, SUMIF, AVERAGEIF.
About Functions SUM, AVERAGE, MIN, MAX, COUNT, ROUND
Chapter 13 Transforming Data with SAS Functions
Examples from SAS Functions by Example Ron Cody
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Creating SAS® Data Sets
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
Chapter 5 new The Do…Loop Statement
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Syntax MROUND(number,multiple) Number is the value to round. Multiple is the multiple to which you want to round number. Remark MROUND rounds up, away.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Microsoft Visual Basic 2005 BASICS Lesson 4 Mathematical Operators.
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
Prepared by: Luigi Muro – Consultant
Question 10 What do I write?. Spreadsheet Make sure that you have got a printout of your spreadsheet - no spreadsheet, no marks!
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Input, Output, and Processing
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Chapter 9 Cleansing and Augmenting the Data Xiaogang Su Department of Statistics University of Central Florida.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
1 STRINGS String data type Basic operations on strings String functions String procedures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
CIS300 Test 3 Review Reinhardt. © 2009 Dale McIntosh. All Rights Reserved. This Review Will Cover Brief overview of EXCEL functions, followed by EXCEL.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
LISA SHORT COURSE SERIES: INTRODUCTION TO SAS UNIVERSITY William DeShong Fall 2015.
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming.
Chapter 14: Combining Data Vertically 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Topics Designing a Program Input, Processing, and Output
Open Source Server Side Scripting MySQL Functions
Introduction to Programming
MARK CARPENTER, Ph.D. Introduction to SAS Programming and Applications
Multiple variables can be created in one declaration
Introduction to Scripting
SET statement in DATA step
INPUT & OUTPUT scanf & printf.
Introduction to Programming
CSI 101 Elements of Computing Spring 2009
Sub Procedures and Functions
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Trainer: Bach Ngoc Toan– TEDU Website:
REACH Computer Resource Center
Presentation transcript:

Basic SAS Functions in Version 8.2 Kim Michalski Office of the Actuary Rick Andrews Office of Research, Development, and Information

Where to begin? n There are over 300 SAS Function in Version 8.2 n This presentation will describe approximately 2 dozen in the following areas: Truncation Character Conversion Date and Time n For a complete list visit: - Base SAS Software - SAS Language and Reference Dictionary - Functions and CALL Routines

Truncation n 1.A - Round up to the next largest integer - CEIL n 1.B - Round down to the next smallest integer - FLOOR n 1.C - Remove the fractional part of a number - INT n 1.D - Round a numeric value to specified unit - ROUND

Descriptive Statistics n 2.A - Compute sum of values in a list - SUM n 2.B - Compute average values of a list - MEAN n 2.C - Determine smallest value in a list - MIN n 2.D - Determine largest value in a list - MAX

Character n 3.A - Split text containing a delimiter (1)- SCAN n 3.B - Split text containing a delimiter (2) - SCAN n 3.C - Justify character values - LEFT n 3.D - Return part of character expression - SUBSTR n 3.E - Return location of character string - INDEX Should we add TRIM?

Character n 3.A - Split text containing a delimiter (1)- SCAN n 3.B - Split text containing a delimiter (2) - SCAN n 3.C - Justify character values - LEFT n 3.D - Return part of character expression - SUBSTR n 3.E - Return location of character string - INDEX Should we add TRIM?

Date and Time n 5.A - Various date functions - Multiple n 5.B - Intervals of a time span - INTCK n 5.C - Advance a date or time interval - INTNX n 5.D- Compute age as of last birthday- YRDIF

1.A - Round up to the next largest integer ( CEIL ) DATA temp1a; rate1 = CEIL(3.2); rate2 = CEIL(3.8); rate3 = CEIL(-3.2); rate4 = CEIL(-3.8); payment1 = CEIL(999.22); payment2 = CEIL(999.88); RUN; ******************************************************** rate1 rate2 rate3 rate4 payment1 payment ********************************************************

1.B - Round down to the next smallest int. ( FLOOR ) DATA temp1b; rate1 = FLOOR(3.2); rate2 = FLOOR(3.8); rate3 = FLOOR(-3.2); rate4 = FLOOR(-3.8); payment1 = FLOOR(999.22); payment2 = FLOOR(999.88); RUN; ******************************************************** rate1 rate2 rate3 rate4 payment1 payment ********************************************************

1.C - Remove the fractional part of a number ( INT ) DATA temp1c; rate1 = INT(3.2); * Same as FLOOR for positive numbers; rate2 = INT(3.8); rate3 = INT(-3.2); * Same as CEIL for negative numbers; rate4 = INT(-3.8); payment1 = INT(999.22); payment2 = INT(999.88); RUN; ********************************************************** rate1 rate2 rate3 rate4 payment1 payment **********************************************************

1.D - Round a numeric value to specified unit ( ROUND ) DATA temp1d; rate1 = ROUND(3.2); rate2 = ROUND(3.8); rate3 = ROUND(-3.2); marg1 = ROUND(12.49); marg2 = ROUND(12.49,.1); dec2 = ROUND( ,.01); five = ROUND(73.3,5); ten = ROUND(23.99,10); RUN; *********************************************************** rate1 rate2 rate3 marg1 marg2 dec2 five ten ***********************************************************

Descriptive Statistics n 2.A - Compute sum of values in a list - SUM n 2.B - Compute average values of a list - MEAN n 2.C - Determine smallest value in a list - MIN n 2.D - Determine largest value in a list - MAX

2.A - Compute sum of values in a list ( SUM ) DATA temp2a1; x=100; y=200; z=.; tot1=SUM(x,y,z); tot2=x+y+z; *<-- BEWARE: tot2 will be missing *; * since z is missing *; RUN; ********************************************************* x y z tot1 tot *********************************************************

2.A - Compute sum of values in a list ( SUM ) DATA temp2a2; a1=300; a2=.; a3=400; a4=500; tot3=SUM(a1,a2,a3,a4); tot4=SUM(OF a1-a4); * If the OF argument is not used *; tot5=SUM(a1-a4); * then a4 will be subtracted a1 *; RUN; ********************************************************* a1 a2 a3 a4 tot3 tot4 tot *********************************************************

2.A - Compute sum of values in a list ( SUM ) DATA temp2a3; x=100; y=200; z=.; a1=300; a2=.; a3=400; a4=500; tot6=SUM(OF a1-a4,x,y,z); tot7=SUM(100,200,.,300,.,400,500); tot8=SUM(z,a2); * Add a zero to the list if a *; tot9=SUM(0,z,a2); * missing value is not desired *; RUN; * when all values are missing *; ********************************************************* tot6 tot7 tot8 tot *********************************************************

Conversion n 4.A - Convert Character date to SAS date- INPUT n 4.B - Use of the YEARCUTOFF Option- INPUT n 4.C - Convert character to numeric - INPUT n 4.D - Convert numeric to character - PUT

3.A - Split text containing a delimiter ( SCAN ) DATA temp3a; dir = 'G:\IMG\Data'; level1 = SCAN(dir, 1,'\'); level2 = SCAN(dir, 2,'\'); level3 = SCAN(dir, 3,'\'); RUN; ***************************************** dir level1 level2 level G:\IMG\Data G: IMG Data *****************************************

3.B - Split text containing a delimiter ( SCAN ) DATA temp3b; state_cnty = 'Montgomery, MD'; county = SCAN(state_cnty,1,','); state = SCAN(state_cnty,2,','); RUN; ***************************************** state_cnty county state Montgomery, MD Montgomery MD Note the space! *****************************************

3.C - Justify character values ( LEFT / RIGHT) DATA temp3c; SET temp3b; state = LEFT(state); OUTPUT; state = RIGHT(state); OUTPUT; RUN; ***************************************** state_cnty county state Montgomery, MD Montgomery MD *****************************************

3.D - Return part of character expression ( SUBSTR ) DATA temp3d; * ; state_cnty = 'Montgomery, MD'; county = SUBSTR(state_cnty, 1,10 ); state = SUBSTR(state_cnty, 13,2 ); RUN; ***************************************** state_cnty county state Montgomery, MD Montgomery MD *****************************************

3.E - Return location of character string ( INDEX ) DATA temp3e; state_cnty = 'Montgomery, MD'; idx = INDEX(state_cnty,','); county = SUBSTR(state_cnty,1, idx-1 ); state = SUBSTR(state_cnty, idx+2,2); RUN; ***************************************** state_cnty idx county state Montgomery, MD 11 Montgomery MD ***************************************** Determine location of the comma. Use that location within the substring.

4.A - Convert CHAR date into SAS date ( INPUT ) OPTIONS YEARCUTOFF=1900; * Can cause an ERROR *; * for 2-digit years *; DATA temp4a; * ; * Month / Day / Year *; char_date = '02/29/2004'; sas_date = INPUT(char_date,MMDDYY10.); OUTPUT; char_date = ' '; sas_date = INPUT(char_date,MMDDYY10.); OUTPUT; char_date = ' '; sas_date = INPUT(char_date,MMDDYY8.); OUTPUT; char_date = '022903'; sas_date = INPUT(char_date,?? MMDDYY6.); OUTPUT; Note that question marks can be used to suppress error messages in the log.

4.A - Convert CHAR date into SAS date ( Continued ) ********************************************************* char_date sas_date Format /29/ /29/2004 MMDDYY /29/2004 MMDDYY /29/1904 MMDDYY ?? MMDDYY6. Note the invalid date of February 29, 2003 ! *********************************************************

4.B - Use of the YEARCUTOFF Option ( INPUT ) OPTIONS YEARCUTOFF=1920; *<-- Set date greater than largest expected 2-digit year !!!; DATA temp4b; * ; * Day / Month / Year *; char_date = '02/29/04'; sas_date = INPUT(char_date,DDMMYY8.); OUTPUT; char_date = '02/29/2004'; sas_date = INPUT(char_date,DDMMYY10.); OUTPUT; * Year / Month / Day *; char_date = ' '; sas_date = INPUT(char_date,YYMMDD8.); OUTPUT; char_date = '2004/02/29'; sas_date = INPUT(char_date,YYMMDD10.); OUTPUT;

4.B - Convert CHAR date into SAS date ( Continued ) DATA temp4b; * ; * Day / Char Month / Year *; char_date = '29feb04'; sas_date = INPUT(char_date,DATE7.); OUTPUT; char_date = '29feb2004'; sas_date = INPUT(char_date,DATE9.); OUTPUT; ********************************************************* char_date sas_date Format /03/04 03/01/2003 DDMMYY8. 02/03/ /01/2003 DDMMYY /31/2003 YYMMDD /02/29 01/31/2003 YYMMDD10. 29feb04 01/01/2003 DATE7. 29feb /01/2003 DATE9. *********************************************************

4.C - Convert Character to Numeric ( INPUT ) DATA temp4c; LENGTH char $25; FORMAT num 15.2; char = '123'; num = char + 0; OUTPUT; * Note ADDITION *; char = '123.45'; num = INPUT(char,6.2); OUTPUT; char = '12345'; num = INPUT(char,5.); OUTPUT; char = ' '; num = INPUT(char,15.); OUTPUT; char = '123,456,789,012'; num = INPUT(char,COMMA15.); OUTPUT; RUN;

4.C - Convert Character to Numeric ( Continued ) ********************************************************** char num Statement char INPUT(char,6.2) INPUT(char,5.) INPUT(char,15.) 123,456,789, INPUT(char,COMMA15.) **********************************************************

4.D - Convert Numeric to Character ( PUT ) DATA temp4d; LENGTH char $25; FORMAT num 15.2; num = 123; char = num || ''; OUTPUT; * Note CONCATENATION *; num = ; char = PUT(num,6.2); OUTPUT; num = 12345; char = PUT(num,5.); OUTPUT; num = ; char = PUT(num,15.); OUTPUT; num = ; char = PUT(num,COMMA15.); OUTPUT; RUN;

********************************************************** num char Statement num || '' PUT(num,6.2) PUT(num,5.) PUT(num,15.) ,456,789,012 PUT(num,COMMA15.) PUT(num,Z5.) ********************************************************** 4.D - Convert Numeric to Character ( Continued ) Note the leading zero. The “ Z5. “ informat can be used to convert numeric procedure codes imported from Excel. Note the leading spaces when using concatenation operator.

5.A - Date Functions DATA temp5a; var1 = TODAY(); OUTPUT; var1 = DATETIME(); OUTPUT; var1 = DATEPART(var1); OUTPUT; var1 = MONTH( TODAY() ); OUTPUT; var1 = YEAR( TODAY() ); OUTPUT; var1 = DAY( TODAY() ); OUTPUT; var1 = QTR( TODAY() ); OUTPUT; RUN;

5.A - Date Functions ****************************************** var1 Statement TODAY() DATETIME() DATEPART( var1 ) 1 MONTH( TODAY() ) 2007 YEAR( TODAY() ) 10 DAY( TODAY() ) 1 QTR( TODAY() ) ******************************************

5.B - Intervals of a Time Span ( INTCK ) DATA temp5b; FORMAT beg_date end_date MMDDYY10.; beg_date = '01JAN2001'D; end_date = '31DEC2002'D; example = INTCK('DAY', beg_date,end_date); OUTPUT; example = INTCK('MONTH',beg_date,end_date); OUTPUT; example = INTCK('MONTH',beg_date,end_date + 1); OUTPUT; example = INTCK('YEAR', beg_date,end_date); OUTPUT; example = INTCK('YEAR', beg_date,end_date + 1); OUTPUT; example = INTCK('QTR', beg_date,end_date); OUTPUT; RUN;

5.B - Intervals of a Time Span ( Continued ) ********************************************************** beg_date (b) end_date (e) example Statement /01/ /31/ INTCK('DAY', b,e) 01/01/ /31/ INTCK('MONTH',b,e) 01/01/ /31/ INTCK('MONTH',b,e + 1) 01/01/ /31/ INTCK('YEAR', b,e) 01/01/ /31/ INTCK('YEAR', b,e + 1) 01/01/ /31/ INTCK('QTR', b,e) **********************************************************

5.C - Advance a Date or Time Interval ( INTNX ) DATA temp5c; FORMAT orig_date example MMDDYY10.; orig_date = '31DEC2002'D; new_date = INTNX('DAY', orig_date,0); OUTPUT; new_date = INTNX('DAY', orig_date,1); OUTPUT; new_date = INTNX('MONTH',orig_date,1); OUTPUT; new_date = INTNX('MONTH',orig_date,3); OUTPUT; new_date = INTNX('YEAR', orig_date,1); OUTPUT; new_date = INTNX('YEAR', orig_date,2,'END'); OUTPUT; RUN;

5.C - Advance a Date or Time Interval ( Continued ) ***************************************************** orig_date(d) new_date Statement /31/ /31/2002 INTNX('DAY',d,0) 12/31/ /01/2003 INTNX('DAY',d,1) 12/31/ /01/2003 INTNX('MONTH',d,1) 12/31/ /01/2003 INTNX('MONTH',d,3) 12/31/ /01/2003 INTNX('YEAR',d,1) 12/31/ /31/2004 INTNX('YEAR',d,2,'END') ******************************************************

5.D - Compute Age as of Last Birthday ( YRDIF ) DATA temp5d; format dob1 dob2 mmddyy10.; dob = '23DEC1970'd; tmp = YRDIF(dob, TODAY(), 'ACTUAL'); age = FLOOR(tmp); RUN; ****************************** dob tmp age /23/ ****************************** Note - Age in this example correspond to TODAY;

About the Speakers Kim Michalski (410) Rick Andrews (410) Centers for Medicare and Medicaid Services 7500 Security Boulevard Baltimore, MD 21244