Computing for Research I Spring 2014 January 22 2014 Why learn SAS macros Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another ID: 208220
Download Presentation The PPT/PDF document "SAS: Macros" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
SAS: Macros
Computing for Research I
Spring 2014
January 22, 2014Slide2
Why learn SAS macros?
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 timeSlide3
Pros and Cons of Macros
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 executeSlide4
The SAS Macro Facility
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 processorSlide5
The SAS Macro Facility
Macro statements
Macro processor
Standard SAS statementsSlide6
Activating the Macro Processor
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 substitutionSlide7
Learning Macro Coding
Beginner Tips
Creating macro variables for text substitution
Dynamic macro variables using
data
Defining and calling macros
Conditional processing using
macros
Iterative processing using macrosSlide8
Beginner Tips
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 efficiencySlide9
Macro Variables: System
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 stringsSlide10
Macro Variables: System
SYSCMD LAST NON-SAS COMMAND ENTERED
SYSDATE CURRENT DATE IN DATE6. OR DATE7. FORMAT
SYSDAY CURRENT DAY OF THE WEEK
SYSDEVIC CURRENT GRAPHICS DEVICE
SYSDSN LAST SAS DATASET BUILT
SYSINDEX NUMBER OF MACROS STARTED IN JOB
SYSINFO SYSTEM INFORMATION GIVEN BY SOME PROCS
SYSPROD INDICATES WHETHER A SAS PRODUCT IS LICENSED
SYSSCP OPERATING SYSTEM WHERE SAS IS RUNNING
SYSTIME STARTING TIME OF JOB
SYSVER SAS VERSIONSlide11
Macro Variables: System
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 21JAN14
AUTOMATIC SYSDATE9 21JAN2014
AUTOMATIC SYSDAY Tuesday
AUTOMATIC SYSTIME 17:45
AUTOMATIC SYSVER 9.2Slide12
Using System Macro Variables
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 21JAN2014Log: Macro variable SYSDATE9 resolves to 21JAN2014Slide13
Macro Variables: General
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.Slide14
Macro Variables: User Defined
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, 2014Slide15
Macro Variables: Rules
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 textSlide16
Using Macro Variables
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 (‘)
ExampleSlide17Slide18Slide19Slide20
Using Macro Variables
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
ExampleSlide21Slide22Slide23
Macro Variable Resolution
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 periodsSlide24
Macro Variable Resolution
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.Slide25
Macro Variables: Part Two
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.Slide26Slide27Slide28Slide29Slide30
Call Symput
Example
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 totalSyntax: 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)Slide31
Defining Macros
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;Slide32
Defining Macros
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 errorsSlide33
Calling a Macro
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 callSlide34
Defining Macro Parameters
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 macroSlide35
Macro Example
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;Slide36
Macro Example
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)Slide37
Macro Example 2
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 necessarySlide38
Macro Example 2
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)Slide39
Keys to Macro Development
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 parametersSlide40
Using Macros: Conditional Processing
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 dataSlide41
Conditional Processing Example
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, form02Slide42
Conditional Processing Example
%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)Slide43
Using Macros: Iterative Processing
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 availableSlide44
Iterative Processing Example
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 datasetSlide45
%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;
%
subsSlide46
Wrap-Up
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