Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Jerry Tsai This presentation and code available at: clintuition.com/pubs/

Similar presentations


Presentation on theme: "1 Jerry Tsai This presentation and code available at: clintuition.com/pubs/"— Presentation transcript:

1

2 1 Jerry Tsai Jerry.Tsai@clintuition.com This presentation and code available at: clintuition.com/pubs/

3 22 Efficiently Handle Lists of Items using the SAS® Macro Language --- Save Time and Reduce Errors Jerry Tsai

4 3 Problem Statement Working with a data set that contains many variables often requires repeating the same editing task over and over on those variables. Problems with having to make repetitive edits: Increased chance of inconsistency, leading to errors in syntax or in logic Increase in time required for task completion

5 4 Data Set Examples #1: Laboratory values Variables: hemoglobin, hematocrit, MCH, MCV, RBC, platelets, etc. #2: Survey data Variables: Question 1, Question 2, Question 3, etc.

6 5 Desired Solution Write code addressing these multiple items with as little unnecessary repetition as possible– and do it correctly the first time.

7 6 Process Flow Write code that addresses one item on the list and validate that it works as desired. Extend the validated code to all items on the list.

8 7 Method Edit the validated code for use with a macro: Replace the list item with a wildcard character. With each iteration, this wildcard will be replaced with a item on the list. The macro will iterate through the list using a loop.

9 8 MACRO %ITERLIST Two parameters: LIST CODE %iterlist(list=, code=)

10 9 Setup Macro variable containing a space-delimited list, e.g.: %let mylist = alfa bravo charlie...yankee zulu;

11 10 data new; set old ( rename = ) ; run; Example #1: Renaming a List of Vars (alfa = alfa_n)

12 11 data new; set old ( rename = ) ; run; Example #1: Renaming a List of Vars (alfa = alfa_n) ? = ?_n Replace item with wildcard character, ?

13 12 data new; set old ( rename = ) ; run; Example #1: Renaming a List of Vars ? = ?_n) (

14 13 data new; set old ( rename = ) ; run; Example #1: Renaming a List of Vars ( ? = ?_n )

15 14 data new; set old ( rename = ) ; run; Example #1: Renaming a List of Vars )) ( ? = ?_n ) %iterlist(list = &mylist., code = %str(

16 15 data new; set old ( rename = ) ; run; Example #1: Renaming a List of Vars (alfa = alfa_n beta = beta_n... zulu = zulu_n )

17 16 proc means data = old; var &mylist.; output out = oldstats (drop=_type_ _freq_) ; run; Example #2: PROC MEANS statistics n(alfa )= alfa_nb mean(alfa )= alfa_mn std(alfa )= alfa_sd

18 17 proc means data = old; var &mylist.; output out = oldstats (drop=_type_ _freq_) ; run; Example #2: PROC MEANS statistics %iterlist(list = &mylist., code = %str( n(?)=?_nb)) %iterlist(list = &mylist., code = %str(mean(?)=?_mn)) %iterlist(list = &mylist., code = %str( std(?)=?_sd))

19 18 %printem(inputds = old, variabl = alfa); %macro printem(inputds =, variabl =); proc print data=&inputds.; var &variabl. ; run; %mend printem; Example #3: Nested Macro Call

20 19 %iterlist ( list = &mylist., code = %nrstr( %printem(inputds = old, variabl = ?); ) ); Example #3: Nested Macro Call

21 20 Advantages of %ITERLIST() Robust approach separates programming logic from the items being iterated over. Save programming time by avoiding needless typing. By retaining the syntax of validated code, reduce the likelihood of error.

22 21 Noteworthy Aspects of %ITERLIST %DO-%END looping Macro quoting functions, e.g., %STR(), %UNQUOTE() %QSYSFUNC() TRANWRD()

23 22 Suggested Reading Carpenter, Art. Resolving and Using &&var&i Macro Variables - SUGI 22 Proceedings (1997) Carpenter, Art. Storing and Using a List of Values in a Macro Variable - SUGI 30 Proceedings (2005) Morris, Robert. Text Utility Macros for Manipulating Lists of Variable Names - SUGI 30 Proceedings (2005) Clay, Ted. Tight Looping With Macro Arrays - SUGI 31 Proceedings (2006)

24 23 Jerry Tsai Jerry.Tsai@clintuition.com This presentation and code available at: clintuition.com/pubs/ Questions? Comments?


Download ppt "1 Jerry Tsai This presentation and code available at: clintuition.com/pubs/"

Similar presentations


Ads by Google