1 / 28

SAS Module 3

SAS Module 3. 13 July 2009. So far we have read…. Brief overview of SAS SAS Windows SAS Library SAS Datasets SAS system options SAS Datastep statements Merging of two datasets. SAS functions SAS Logical operators IF… THEN… ELSE Missing Data Date & Time PROC PRINT PROC SORT

jacie
Download Presentation

SAS Module 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SAS Module 3 13 July 2009

  2. So far we have read… Brief overview of SAS SAS Windows SAS Library SAS Datasets SAS system options SAS Datastep statements Merging of two datasets SAS functions SAS Logical operators IF… THEN… ELSE Missing Data Date & Time PROC PRINT PROC SORT PROC TABULATE PROC TRANSPOSE

  3. Today's discussion _n_ RETAIN statement DO… END… loop ARRAY LENGTH statement ODS statement PROC REPORT Title, Footnote Comment in SAS SAS Help

  4. IF… THEN… ELSE statement DATA result; SET result; IF marks=>50 THEN pass=‘Yes’; ELSE pass=‘No’; RUN;

  5. RETAIN statement Suppose we want to add a counter to the dataset…. How to do that? You have to add this variable DATA one; SET one; RETAIN counter 0; counter=counter+1; RUN;

  6. RETAIN statement Suppose we want to count no. of observations per treatment…. How to do that? DATA efficacy1; SET efficacy; RETAIN count1 count2 0; IF trt=1THEN count1=count1+1; IF trt=2THEN count2=count2+1; RUN;

  7. _N_ This indicates the number of observations. Ex: Suppose you want to delete 2nd observation from a dataset… How to do that? DATA two; SET one; IF _n_=2 THEN DELETE; RUN;

  8. LENGTH statement • Defining the length of the variables. Ex: Suppose we want to add TRTNAME variable

  9. LENGTH statement DATA efficacy1; SET efficacy; LENGTH trtname $ 50; IF trt=1THEN trtname='Tamsulosin only'; ELSEIF trt=2THEN trtname='Tamsulosin + Tolterodine’ ; RUN;

  10. LENGTH statement With Length Statement Without Length Statement

  11. ARRAY • The use of arrays may allow us to simplify our processing. We can use arrays to help read and analyze repetitive data with a minimum of coding. • SAS arrays are another way to temporarily group and refer to SAS variables. • The variables referenced by the array are called elements. • No. of variables within a array is known as dimension of array Defining Array: ARRAY temp {5} temp1 temp2 temp3 temp4 temp5; Array statement Array name Dimension of Array within bracket List of Reference variables or dimension Note: Dimension of array can be replaced by ‘*’ also. Hence, following statement is also valid ARRAY temp{*} temp1 temp2 temp3 temp4 temp5;

  12. ARRAY ARRAY temp {5} temp1 temp2 temp3 temp4 temp5; Note that, in the above statement we have assumed that temp1, temp2, temp3, temp4 and temp5 are numeric variables. If an array used to refer character variables then we need to use ‘$’ after the dimension as follows: ARRAY demo {3} $ smoke Alcohol Allergy; After defining ARRAY, how to refer the variables within the ARRAY?? Temp[1] = temp1 Temp[2] = temp2 Temp[3] = temp3 Temp[4] = temp4

  13. ARRAY ARRAY temp {5} temp1 temp2 temp3 temp4 temp5; Ex: Suppose you have dataset of temperatures in degree centigrade. You have to covert temperatures into Farenhyte. 9C+160 F= 5

  14. ARRAY ARRAY temp {5} temp1 temp2 temp3 temp4 temp5; DATA tempout; SET temp; ARRAY tempcent {5} cent1 cent2 cent3 cent4 cent5; ARRAY tempfart {*} fart1 fart2 fart3 fart4 fart5; DO i=1 TO 5; FART{i}=(9*cent{i}+160)/5; END; RUN;

  15. DO… END…. DO… END… Group Iterative DO…END… Loop Iterative DO… END… Loop with ARRAY statement

  16. DO… END… Group Often used with IF… END… statement to implement to execute more than one statement. Ex. If Y>0 then calculate X+Y and X/Y DATA two; SET one; IF Y>0 THEN DO; U=X+Y; V=X/Y; END; RUN;

  17. Iterative DO… END… Loop To perform same operation again and again Ex. Generate 100 Random numbers in SAS. Index variable DATA randnum; DO a = 1 TO 100; rand = RANUNI(0); OUTPUT; END; RUN; Initial value End value RANUNI(0) – Generates random numbers OUTPUT – Adds datalines to the dataset.

  18. Iterative DO… END… Loop with ARRAY statement Already discussion in ARRAY section

  19. 2. To turn on/ off the SAS OUTPUT window ODS LISTING CLOSE; ODS statement ODS - Output Delivery System 1. To export output in RTF (Rich Text Format) or PDF (Portable Document Format) or HTML format. PROCPRINTdata=randnum; RUN; ODSPDFFILE='C:\New Folder\out.pdf'; ODSPDFCLOSE; ODSRTF FILE='C:\New Folder\out.rtf'; ODSRTFCLOSE; ODSHTML BODY='C:\New Folder\out.htm'; ODSHTMLCLOSE; ODS LISTING;

  20. ODS statement 3. To turn-off/ on the destination output ODS RESUTLS; ODS NORESULTS; 4. To save particular SAS output in DATASET form 5. To display only particular output

  21. PROC REPORT

  22. PROC REPORT PROCREPORTDATA=grocery NOWDHEADLINE HEADSKIP; PROCREPORTDATA=grocery NOWD; PROCREPORTDATA=grocery NOWD HEADLINE; PROCREPORTDATA=grocery ; COLUMN manager department sales; DEFINE manager /DISPLAY ; DEFINE manager /ORDER ORDER=FORMATTED FORMAT=$mgrfmt. ; DEFINE manager /DISPLAY FORMAT=$mgrfmt. ; DEFINE department /ORDER ORDER=FORMATTED FORMAT=$deptfmt. ; DEFINE department /DISPLAY ; DEFINE department /DISPLAY FORMAT=$deptfmt. ; DEFINE sales /DISPLAY ; DEFINE sales /DISPLAY FORMAT=$dollar7.2. ; DEFINE sales /ANALYSIS SUM FORMAT=$dollar7.2. ; BREAKAFTER manager / UL SUMMARIZE SKIP; COMPUTE AFTER; LINE 'Total sales for these stores were: ' sales.sum dollar9.2; ENDCOMP; RUN;

  23. PROC REPORT

  24. PROC REPORT Headline Headskip

  25. PROC REPORT Applying Formats

  26. PROC REPORT Applying ORDER on Manager & Department

  27. PROC REPORT Applying BREAK after Manager

  28. PROC REPORT Applying COMPUTE

More Related