Title: Macros: Code cutters or code generators
1Macros Code cutters or code generators
- By
- Prashant Mittal
- pmittal_at_usm.maine.edu
2Introduction
- Macros Code cutters or code generators?
- Why I learnt Macros
3Macro Structure
- macro macro_name
-
-
- mend macro_name
- macro_name
- run
- Regular SAS data steps.
- Procedure statements.
- Macro Statements.
4SAS OOP or not
- Function z f ( x, y)
- Java, C, C
- ? Have built in functions.
- ? Users may define their own functions.
- ? Reuse these functions by storing them in a
library.
- SAS
- ?Has built in procedures.
- (Proc Means, Proc Freq. Proc Reg etc.)
- ? Users may define their own functions in the
form of macros. - ? Reuse these functions by storing them in a
library.
5Macros written as Functions
- macro xyz (a , b ,c )
-
- data any
- set any1
- newvar1 a
- newvar3 b
- newvar3 c
- run
-
- mend xyz
- xyz (a var1, b var2, c var3)
- xyz (a var4, b var5, c var6)
- xyz (a var7, b var8, c var9)
6Macro variables and their scope
- Tools that enable to dynamically modify the text
in a SAS program though symbolic substitution are
Macro Variables. - Macro variables are default characters.
- They can be temporarily converted to numeric for
calculations. - User defined macro variables Automatic macro
variables. - Global macro variables Local Macro Variables
7Macro variables and their scope
- To get description of macro variables that exist
at a particular time - Put _all_ _automatic_ _global_ _local_
_user_ - To write instructions in SAS log, the following
code could be used - ? Put str ( string)
- The above code will print string in SAS log.
- ? Put Report for sysday, sysdate generates
the following in SAS log - Report for Thursday 02APR04
8Creating new Macro Variables
- let macro_variable This is it
- put macro_variable
-
- The above two lines will produce This is it
- in SAS log.
- let a Example
- data new
- do i 1 to 10
- var ranuni(0)
- newvar "a"
- output
- end
- run
- proc print data new
- run
- Obs i var newvar
- 1 1
0.83883 Example - 2 2
0.82309 Example - 3 3
0.20670 Example - 4 4
0.48918 Example
9Creating Macro Variables
- data team2
- input position 12. player 12.
- call symput ('POS' left (_n_), position)
- cards
- batter Ann
- pitcher Tom
- frstbase Bill
-
- run
- data team2
- set team2
- new "pos1"
- run
- proc print data team2
- Title Newly created macro variables are pos1,
pos2, pos3" - run
- Newly created macro variables are batter ,
pitcher , frstbase - 1903 Thursday, April 1, 2004
- Obs position player new
- 1 batter Ann batter
- 2 pitcher Tom batter
- 3 frstbase Bill batter
10Description of the dataset used
- Medicaid claims file.
- Originally had more than 2.5 million
observations. - Randomly chose 1000 observations.
11Writing repetitive code using Macros
- Proc means n min max mean sum median std P50 data
any - Var charg1 Paid1
- Class elig1
- Run
- Proc means n min max mean sum median std P50 data
any - Var charg2 Paid2
- Class elig2
- Run
- Proc means n min max mean sum median std P50 data
any - Var charg3 Paid3
- Class elig3
- Run
- .
- .
- How wonderful would it be to write a do loop to
handle these repetitive procedures
12Writing repetitive code using Macros
- Do loop is not restricted to the data step.
- macro easy
- do i 1 to 15
- Proc means n mean sum median std data any
- Var chargi Paidi Indirect referencing
- Class eligi
- run
-
- end
- mend
- easy
- run
13Writing repetitive code using Macros Example 1
- Proc means mean median std data any
- Var charg1 Paid1
- Class elig1
- Run
- Proc means mean median std data any
- Var charg2 Paid2
- Class elig2
- Run
- Proc means mean median std data any
- Var charg3 Paid3
- Class elig3
- Run
- .
- .
- .
- .
- macro easy
- do i 1 to 7
- Proc means mean median std data any
- Var chargi Paidi Indirect referencing
- Class eligi
- run
-
- end
- mend
- easy
- run
14Writing repetitive code using Macros Example 2
- proc sort data any
- by elig1
- run
- proc freq data mainecare95_02
- table chg1paid1
- by elig1
- title' Cross tabulation between chg1 and paid1'
- run
- proc sort data any
- by elig2
- run
- proc freq data mainecare95_02
- table chg2paid2
- by elig2
- title' Cross tabulation between chg2 and paid2'
- run
- .
- .
- The previous repetitive code of more than 150
lines could be written as follows - macro easy
- do i 1 to 15
- proc sort data any
- by eligI
- proc freq data any
- table chgipaidi
- by eligi
- title' Cross tabulation between chgi and
paidi' - run
- end
- mend
- easy
- run
15Repetitive case where variable names are not
sequential
-
- Suppose you want to have descriptive
statistics for the variables in line1 by
variables in the line 2. - Line 1 A B C D E
- Line 2 V W X Y Z
16Writing repetitive code using Macros Example 3,
Case where variables are not sequential.
- Proc means mean median std data any
- Var A
- Class V
- Run
- Proc means mean median std data any
- Var B
- Class W
- Run
- Proc means mean median std data any
- Var C
- Class X
- Run
- Proc means mean median std data any
- Var D
- Class Y
- Run
- macro easy (var1 , var2 )
- Proc means mean sum median std data any
- Var var1
- Class var2
- Run
- mend
- easy(var1 A, Var2 V)
- easy(var1 B, Var2 W)
- easy(var1 C, Var2 X)
- easy(var1 D, Var2 Y)
- easy(var1 E, Var2 Z)
- run
17Output chi-sq of multiple cross-tabs directly to
a dataset.
- Consider an example where the experimenter is
looking to get only the chi-square values of
cross tabulations of a variable say A, with 12
other variables (B, C, D, E, F, G, H, I, J, K, L,
M). - Regular code will be written the following way
- proc freq data any
- table A( B C D E F G H I J K L M) /CHISQ EXACT
- run
18Output chi-sq of multiple cross-tabs directly to
a dataset.
- data final
- set _null_
- proc freq data any noprint
- table AB/ chisq
- output PCHI out a
- data a
- Set a
- vars "A-B"
- data final
- set final a
- run
- proc freq data any noprint
- table AC/ chisq
- output PCHI out a
- (continued..)
- proc freq data any noprint
- table AD/ chisq
- output PCHI out a
- data a
- Set a
- vars "A-D"
- data final
- set final a
- run
- proc freq data any noprint
- table AC/ chisq
- output PCHI out a
- data a
19Output chi-sq of multiple cross-tabs directly to
a dataset.
- data final
- set _null_
- macro chisq(var1 ,var2 )
- proc freq data any noprint
- table var1var2/chisq
- output PCHI out a
- data final
- set final a
- vars "var1-var2"
- run
- mend
- chisq(var1 A,var2 B)chisq(var1 A,var2
C) chisq(var1 A,var2 D) - chisq(var1 A,var2 E)chisq(var1 A,var2
F)chisq(var1 A,var2 G) - run
20Time trend analysis
- ? Study unpaid amount by Medicaid with time.
- ? Unpaid amount Charges Payments.
- ? To study if there is a time trend in unpaid
amounts. - ? Procedure Break down the whole dataset into
groups of 50 (say), sort them by year and study
the descriptive statistics of unpaid amount.