SAS Workshop Introduction to SAS Programming Day 1 Session 1 Iowa State University May 9, 2016 Structure of a SAS Program SAS programs consist of SAS statements General Form: SAS_keyword operand; e.g. proc anova data=survey14; SAS statements are interpreted and executed in their order of appearance in a SAS program Some statements are non-executable in the sense that they only provide information to SAS on how to set things up; but no action results when the program is run However, blocks of statements called steps, that define one of two basic activities, are executed as groups. DATA step: statements leading to the creation of a SAS data set PROC step: statements needed to tell a SAS procedure to perform a statistical analysis A SAS program consists of several logically related DATA and PROC steps Example A1 data oranges; input Variety $ Flavor Texture Looks; Rating=(Flavor+Texture+Looks)/3; datalines; navel 9 8 6 temple 7 7 7 valencia 8 9 9 mandarin 5 7 8 ; proc sort data=oranges; by descending Total; run; proc print data=oranges; title 'Taste Test Results for Oranges'; run; proc sgplot data=oranges; vbar Variety/response=Rating stat=mean fillattrs=(color=bisque); run; SAS Data Set A SAS data set (or table) is a rectangular table of rows and columns. Below is an external view of a SAS data set. Rows (observations) Columns (variables) SAS Data Set All variables must have a name, type, and length. A variable’s type is either character (string) or numeric. The type plays a role in determining the length. Character values are 1 to 32,767 characters (bytes) long. Numeric values are 8 bytes of floating point storage: Numeric Currency Date (days from 01JAN1960) Time (seconds from midnight) SAS Data Set A format is used to control how values are displayed. Formats do not affect how values are stored. Format: Width: Stored value: MMDDYY 10 15766 Format: DOLLAR Width: 8 Decimal Places: 2 Stored value: 234.60 Formats A format is an instruction that SAS uses to convert internal data values to the external appearance (i.e., as the printed values in the SAS output.) The FORMAT statement can be used to associate standard SAS formats (or user-written formats) with variables. You use a FORMAT statement in the DATA step to permanently associate a format with a variable. You can use a FORMAT statement in some PROC steps, to temporarily associate a format with a variable. The FORMAT procedure enables you to define your own formats for variables (user-written formats) Some Standard Formats w.d Example: Internal value 1234.5 Format 8.2 Output b1234.50 w. Example: Internal value 1234 Format 6. Output bb1234 $w. Example: Internal value Mary Format $5. Output Mary# mmddyyw. Example: Internal value 18537 Format mmddyy8. Output 10/02/10 • dollarw.d Example: Internal value 123.45 Format dollar8.2. Output bb$123.45 Note: b and # symbols in above examples denote blanks Example A2 data garden; input Date ddmmyy8. Item $10. Price 5.2; datalines; 07/10/87Sprinkler 875 17/09/88Bench 12000 25/10/89Planter 1365 03/08/86Sweeper 1185 19/09/88Edging 750 11/06/85Shears 2100 29/10/87Trimmer 7645 21/09/87Gloves 350 ; proc print; *format Date ddmmyy8. Price dollar8.2; run; Some Additional Details SAS variable names must be 32 characters or less, constructed of letters, digits and the underscore character. Names beginning with the underscore character are reserved for special system variables. Data set names follow similar rules as variables. SAS is not case sensitive, except inside of quoted strings. However, SAS will remember the case of variable names when it displays them. Each statement in SAS must end in a semicolon (;). Lines beginning with an asterisk (*) are treated as comments. Multiple comments are enclosed within a /* and a */. The run; command signals to SAS that the previous step can be executed.