macro_lecture_spring_2014

advertisement
Computing for Research I
Spring 2014
January 22, 2014





Avoid repetitious SAS code
Create generalizable and flexible SAS code
Pass information from one part of a SAS job to
another
Conditionally execute data steps and PROCs
Dynamically create code at execution time

Pros
Coding time – once familiar with macros
 Updates – will propagate throughout code
 Validation – only needs to be done once
 Allows data-driven conditional processing


Cons


Initial coding is more complex
May take longer to compile or execute

A tool for text substitution
 Simple text strings or SAS language statements

A component of Base SAS
 May be used with SAS procedures, graphics, data steps,
ODS, etc.

Facility has two main components
 Macro processor – portion of SAS that does the work
 Macro language – syntax that communicates with the
processor
Macro
statements
Macro
processor
Standard SAS
statements

Macro Variable
 Syntax: &name
 Refers to a macro variable name reference
 Processor completes the text substitution

Macro
 Syntax: %name
 Refers to a macro call
 Processor compiles the macro with text substitution

Beginner Tips

Creating macro variables for text substitution

Dynamic macro variables using data

Defining and calling macros

Conditional processing using macros

Iterative processing using macros

Read your LOG
 Turn on system options to view text substitution and
macro execution notes
 Syntax: OPTIONS MPRINT SYMBOLGEN;
 Default is NOMPRINT and NOSYMBOLGEN
 Allows validation of code and aids in debugging

Focus on the program then incorporate the
macros
 Plan out your program before you start coding
 Write syntax that works
 Supplement the syntax with macros for efficiency

SAS opening triggers several automatic macro
variables
 Global: these variables are available at any time in the




session in any part of the code
User-defined variables may be added to global symbol
table
Minimum length of 0 (missing)
Maximum length of 65,534 characters
Stores numeric values as character strings
SYSCMD
SYSDATE
SYSDAY
SYSDEVIC
SYSDSN
SYSINDEX
SYSINFO
SYSPROD
SYSSCP
SYSTIME
SYSVER
LAST NON-SAS COMMAND ENTERED
CURRENT DATE IN DATE6. OR DATE7. FORMAT
CURRENT DAY OF THE WEEK
CURRENT GRAPHICS DEVICE
LAST SAS DATASET BUILT
NUMBER OF MACROS STARTED IN JOB
SYSTEM INFORMATION GIVEN BY SOME PROCS
INDICATES WHETHER A SAS PRODUCT IS
LICENSED
OPERATING SYSTEM WHERE SAS IS RUNNING
STARTING TIME OF JOB
SAS VERSION

Display all automatic system macro variables
after opening SAS
 Syntax: %put _automatic_;
 The following will appear in the SAS log after
submitting the above code:
AUTOMATIC SYSDATE
AUTOMATIC SYSDATE9
AUTOMATIC SYSDAY
AUTOMATIC SYSTIME
AUTOMATIC SYSVER
21JAN14
21JAN2014
Tuesday
17:45
9.2

May want to include a run date on reports
 “Report run as of January 21, 2014.”

Macro variables references begin with an
ampersand (&) followed by a macro variable
name
 Syntax: &sysdate9

To create a footnote with a run date:
 Syntax: footnote1 “Report run as of &sysdate9”;
 Output: Report run as of 21JAN2014
 Log: Macro variable SYSDATE9 resolves to 21JAN2014



An efficient way of replacing text strings in
SAS code.
Can be defined within a macro definition or
within a statement that is outside a macro
definition, referred to as OPEN code.
Are independent of SAS data set variables.

Create your own macro variables
 Helpful to include these at the top of the program or to
create instructions at the top of the program of which
variables will need updated

Global macro variable creation
 %LET statement will create a macro variable
 Syntax: %LET newvar=January 1, 2014;
 The macro variable “newvar” now contains the text
“January 1, 2014”
 Syntax: title “Data as of &newvar”;
 Output: Data as of January 1, 2014

Naming Conventions
 Must start with a letter or _
 Can be followed by letters or digits
 Cannot be a reserved word
 http://support.sas.com/documentation/cdl/en/mcrolref
/61885/HTML/default/viewer.htm#a001958322.htm
 Cannot start with SYS, AF, or DMS

Defining the value of the macro variable
 May be character or numeric
 Do not use quotes around text

Call the macro variable with the ampersand
and the variable name
 May be called from statements such as title or footnote,
within data steps, and in procedures
 Syntax: &newvar

Using quotes around a macro variable
 Text statements such as title that call a macro variable
must use double quotes (“)
 The macro variable will not resolve if placed inside
single quotes (‘)
 Example

Macro variables can also be used in both DATA
steps and PROCs (procedures)
 Can be used for numeric or character values
 Only use quotes around the macro variable name if
quotes would be used without macros
 Always use double quotes
 Example



The ampersand & tells SAS that the text
following it is a macro variable name
SAS considers all text following & to be part of
the macro variable name until it encounters
another & or other delimiter such as “ or ;
A period . can be used as a delimiter to
separate a macro variable name from text
 If a period is part of the text then include two periods

Examples of the use of delimiters for macro
variables in text
 %let var1=final;
 %let var2=exam;
Syntax
Result
Title “This is the &var1&var2”;
This is the finalexam
Title “This is the &var1..”;
This is the final.
Title “This is the &var1. &var2..”;
This is the final exam.

SAS frequently allows for more than one way
to do things
 The SYMPUT routine can also create macro variables
 Can be used to create macro variables with static values
or dynamic (data dependent) values
 Creates a global variable if created in open code
(outside a macro) similar to %let
 Syntax: CALL SYMPUT(“macro variable name”,text);
 If text appears in quotes then it is a literal value. If it
appears without quotes as it does here, then it is a
variable name.

You create a report with multiple data listing tables.
You receive a request to have the title of each table
contain the number of observations.
 Example: A listing of hundreds of Adverse Events
 Write code to count the total observations in the table
 Syntax: proc sql; create table total as select count(*) as
freq from aetable; quit;
 Create a macro variable that contains the total
 Syntax: data _null_; set total; call symput(“tot”,freq); run;
 Create a title statement that calls the macro variable
 Syntax: title “This is the AE Listing (N=&tot.)”;
 Output: This is the AE Listing (N=100)

A macro enables you to write macro programs
 Programs will enable you to do iterative and
conditional processing

General syntax of a macro or macro definition:
 %MACRO macro_name;
macro_text
%MEND macro_name;



Macro name follows SAS variable naming
conventions – identifies the macro
Macro text may include text, SAS statements,
and macro variables, functions, or calls
After submitting a macro definition:
 Macro language statements are checked for syntax
errors and compiled
 SAS statements are not checked for syntax errors

Calling a macro causes it to execute
 Call can be made anywhere in the program after the
macro has been defined
 Represents a macro trigger

Syntax for calling a macro:
 % macro_name
 Note that a semi-colon is not needed after the macro
call

Macros with keyword parameters
 There are other types of parameters but these are the
most easily identified in code
 Syntax:
%MACRO macro_name (keyword=value,…,keyword=value);
macro text
%MEND macro_name;

Keyword parameters
 Can be specified in any order
 Are assigned a default or null value after equal sign
 Are local macro variables (as opposed to global)
 They can only be called within the specified macro


You need to be able to output the grade
distribution by gender and type of sport for
athletes at your college as quickly as possible.
Original Syntax:
proc freq data=grades;
where gender=“Male” and sport=“Football”;
table grades;
title “Grade Distribution for Male Football
Players”;
run;

Define the macro syntax:
%macro grade(gender=,sport=);
proc freq data=grades;
where gender=“&gender” and sport=“&sport”;
table grades;
title “Grade Distribution for &gender. &sport. Players”;
run;
%mend grade;

Call the macro:
%grade(gender=Male,sport=Football)


Create a customized graph of drug levels in the
blood over time for several cohorts and dose
levels, but not all
Original Syntax:
title “Cohort 1, Dose Level 1 Drug Levels Over Time”;
proc gplot data=new(where=(cohort=1 and dose=1));
plot dose_one_level*time;
run; quit;

This syntax would need repeated for each
cohort and dose level necessary

Macro Syntax:
%macro doses (cohort=,dose=,num=);
title “Cohort &cohort., Dose Level &dose. Over Time”;
proc gplot data=new(where=(cohort=&cohort. and
dose=&dose.));
plot dose_&num._level*time;
run; quit;
%mend doses;
%doses(cohort=1,dose=1,num=one)
%doses(cohort=1,dose=2,num=two)




Write and debug the SAS program without
macro coding
Generalize by removing hardcoded constants
and creating macro variables with %LET
statements
Define a macro by placing %MACRO and
%MEND statements around the program
Convert %LET statements to macro parameters

Defining a macro allows you to use conditional
processing outside of the data step
 IF, THEN, ELSE, DO, END

Conditional keywords must have the % sign in
front of them inside the macro definition
 %IF, %THEN, %ELSE, %DO, %END
 AND/OR are exceptions

This enables you to execute certain portions of
the code based on the data

You are creating a report with numerous tables
by injury type. The reports for each injury type
have identical information, however, the lab
values are stored in different tables for each
injury type. Run the report program to create a
report by injury type using conditional
processing.
 Injury types: severe, mild
 Respective lab table names: form01, form02
%MACRO report(injurytype=,num=);
%IF &injurytype=severe %THEN %DO;
data labs;
set form01;
run;
%END;
%ELSE %IF &injurytype=mild %THEN %DO;
data labs;
set form02;
run;
%END;
proc means data=labs n mean std;
var inr creatinine;
run; quit;
%MEND report;
%report(injurytype=severe)
%report(injurytype=mild)

Iterative processing can be done within a
macro definition
 Can repeatedly execute macro statements
 Can repeatedly generate SAS code

Keywords enable the processing
 %DO, %END, %BY
 The index variable (%I) is a macro variable
 %START and %STOP macro variables are available


Your client requests that a dataset be created
for each subject. You have several hundred
subjects in your study. How can you do this
efficiently?
Use iterative processing and macro variables to
create a loop that output the data for each
subject into its own dataset
%macro subs;
proc sql; create table total as select
count(distinct subject) as total_subjects
from masterdata; quit;
data _null_;
set total;
call symput ('total',total_subjects);
run;
%do i=1 %to &total;
data sub&i;
set masterdata;
if _n_=&i;
run;
%end;
%mend subs;
%subs

Programming in SAS using macros can make
your life easier
 Reduces updates throughout programs
 Reduces repetitious code
 Allows for conditional and iterative processing outside
the data step


Evaluate your programming goals to decide if
macros are needed for each program
Start simple and expand to include all parts of
macro programming
Download