MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
Macros II
1
1
Macro Logical Statements are tools which allow for pieces of code to be executed conditionally.
The concept is similar to regular logical statements (which are used to execute statements conditionally).
Macro Logical Statements can only be used inside of macros. Macro Logical Statements are differentiated from regular logical statements by the use of the % sign.
Macro Logical Statements include %DO, %TO, %END, %WHILE, %IF, %THEN, %ELSE
Note: There is no %AND / %OR in SAS (use AND / OR) instead.
Macro Logical Statements
2
2
“Double” Macro Variables
“Double” Macro Variables are a way to dynamically reference macro variables. In other words, a macro variable resolves to another macro variable (which then resolves to a value).
In these situations, SAS resolves macro variables in multiple “passes”.
In the first pass, macro variables with single ampersands are resolved to their value, double ampersands are resolved to single ampersands, and double periods are resolved to single periods.
SAS will continue to make passes until all macro variables are resolved.
“Double” Macro Variables (cont.)
Below are examples of how SAS resolves double macro variables.
%let mystate1 = MA;
%let mystate2 = CT;
%let i = 1;
%let j = 2;
Pass 1 Pass 2
&&mystate&i > &mystate1 -> MA
&&mystate&j > &mystate2 -> CT
&&mystate&i.._is_my_state > &mystate1._is_my_state -> MA_is_my_state
Call Symputx
We know that macro variables can be defined using a %LET statement. This requires us to manually enter the value. There are situations where we want the value of a macro variable to be from an observation in a SAS dataset.
CALL SYMPUTX is a statement that creates a macro variable whose value comes from an observation in the dataset. Include the ‘G’ input to make the macro variable global.
Data _NULL_;
set work.MA;
call symputx (“mystate”, state, ‘G’);
run;
&mystate will resolve to MA
Combining Macro Concepts
The various Macro concepts (Macro Variables, Macro Programs, Macro Logical Statements, Double Macro variable resolution, and Call Symput) can be combined to create projects, programs, and codes that are very dynamic.
Example of a well-organized folder structure:
Using the %Include statement to execute code saved in a program file:
%include “&loc.\Programs\ManageDates.sas”;
Example of a “Main” Program
Automating Import and Manipulation
Use the information in the zip file “Work Folders” to solve the problem.
The folder “Accounts” contains many sub-folders, each with a data set containing policies. Create a macro that combines all of the datasets into a single data set called Work.Policies. Use the Accounts_List dataset. There should be 5000 policies in total.
The folder “AllPolicies” contains four sub-folders. Create a macro that creates a dataset for each Subregion, containing the records for all of the states in that Subregion, and saves it in the appropriate Region folder. Use State_Info.
Readings
Textbook section (5th edition) 7.6, 7.7, 7.8
Textbook section (6th edition) 7.6, 7.7, 7.8, 7.10
9
9
/docProps/thumbnail.jpeg