avoid repetitious sas code create generalizable and flexible sas code pass information from one...
TRANSCRIPT
![Page 1: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/1.jpg)
SAS: MacrosComputing for Research I
Spring 2013January 29, 2013
![Page 2: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/2.jpg)
What can MACROS do for you?
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
![Page 3: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/3.jpg)
What are Macros and why do we need /use them?
A macro is a way to automate a task that you perform repeatedly or on a regular basis.
It is a series of commands and actions that can be stored and run whenever you need to perform the task.
Can you think of situations where you may want or have used a macro?
![Page 4: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/4.jpg)
SAS PROC statements
In essence all of the PROC statements in SAS are macros. They have been written, validated and incorporated into the system so that they can be used repeatedly when called to perform specific tasks.
![Page 5: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/5.jpg)
Example: PROC MEANS
Provides data summarization tools to compute descriptive statistics for variables across all observations and within groups of observations.› Calculates descriptive statistics based on
moments› Estimates quantiles, including the median› Calculates confidence limits for the mean› Indentifies extreme values› Performs a t test
![Page 6: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/6.jpg)
![Page 7: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/7.jpg)
What is the macro facility?
A tool for extending and customizing SAS and for reducing the amount of text you must enter to do common tasks.
Enables you to assign a name to character strings or groups of SAS programming statements.
![Page 8: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/8.jpg)
Components of macro facility:
The macro processor The portion of SAS that does the work
The macro language The syntax that you use to communicate
with the macro processor
![Page 9: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/9.jpg)
Macro Processor
Macro statements
Standard SAS statements
Macro processor
![Page 10: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/10.jpg)
How do we “trigger” the SAS macro processor?
&name› Refers to a macro variable› The form &name is called a macro variable
reference.
%name› Refers to a macro› The form %name is called a macro call.
![Page 11: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/11.jpg)
What we will cover:
Macro Variables and Using them for specific functions› SAS defined macro variables› How do we use these in conjunction with SAS
code? Creating SAS Code Using Macros
› Defining SAS macros› SYNTEX for SAS macros› Calling SAS macros
Applications Examples
![Page 12: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/12.jpg)
Macro Variables
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.
![Page 13: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/13.jpg)
Macro Variables defined by SAS
When you invoke SAS, the macro processor creates automatic macro variables that supply information related to the SAS session.
![Page 14: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/14.jpg)
Macro Variables defined by SAS
Some automatic SAS macro variablesSYSCMD LAST NON-SAS COMMAND ENTEREDSYSDATE CURRENT DATE IN DATE6. OR DATE7. FORMATSYSDAY CURRENT DAY OF THE WEEKSYSDEVIC CURRENT GRAPHICS DEVICESYSDSN LAST SAS DATASET BUILTSYSINDEX NUMBER OF MACROS STARTED IN JOBSYSINFO SYSTEM INFORMATION GIVEN BY SOME PROCSSYSPROD INDICATES WHETHER A SAS PRODUCT IS LICENSEDSYSSCP OPERATING SYSTEM WHERE SAS IS RUNNINGSYSTIME STARTING TIME OF JOBSYSVER SAS VERSION
![Page 15: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/15.jpg)
Macro Variables defined by SAS
To use an automatic macro variable, reference it with an ampersand followed by the macro variable name.› Example: SYSDATE and SYSDAY
SYSDATE contains a SAS date value in the DATE7. format, which displays a two-digit date, the first three letters of the month name, and a two-digit year.
SYSDAY contains a SAS day value
![Page 16: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/16.jpg)
Macro Variables defined by Users
Scope – Global vs Local› %GLOBAL – used to define a global macro
variable Naming conventions
› Must start with a letter or _› Can be followed by letters or digits› Cannot be a reserved word (HANDOUT 1)
![Page 17: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/17.jpg)
Macro Statements
Begin with a % and a macro keyword and end with a semicolon (;)
Assign values, substitute values, and change macro variables
Can branch or generate SAS statements conditionally
![Page 18: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/18.jpg)
Macro Variables defined by Users
%LETHow do we use this?
%LET CITY=CHARLESTON;TITLE “Demographics Data for &CITY”;
![Page 19: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/19.jpg)
Macro Variables defined by Users
%LET CITY=CHARLESTON;TITLE “Demographics Data for &CITY”;
Notes: 1. Once a macro variable is defined, you must
precede the name of the macro variable with an ampersand (&) each time it is used.
2. If a macro variable appears within a quoted string, double quotes must be used to ensure that the macro variable ‘resolves’ to its assigned value.
![Page 20: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/20.jpg)
Macro Program Statements
%DO
%GLOBAL
%LOCAL
%MACRO and %MEND
![Page 21: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/21.jpg)
Macro Functions
Processes one or more arguments and produces a result.
Can use all macro functions in both macro definitions and open code.
Examples: (%EVAL, %LENGTH, %UPCASE, %PUT)
![Page 22: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/22.jpg)
Using and Displaying Macro Variables
%LET A=1+2;%LET B=10*3;%LET EVAL_A=%EVAL(&A);%LET EVAL_B=%EVAL(&B);%PUT &A IS &EVAL_A;%PUT &B IS &EVAL_B; SUBMIT TO GET:
![Page 23: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/23.jpg)
Using and Displaying Macro Variables
%LET A=HAPPY;%LET B=BIRTHDAY;%PUT THE LENGTH OF &A IS %LENGTH(&A).;%PUT THE LENGTH OF &B IS %LENGTH(&B).; SUBMIT TO GET:
![Page 24: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/24.jpg)
Using and Displaying Macro Variables
%LET A=2;%LET B=5;%LET OPERATOR=+;%PUT THE RESULT OF &A &OPERATOR
&B IS %EVAL(&A &OPERATOR &B).; SUBMIT TO GET:
![Page 25: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/25.jpg)
Assigning SAS Statements
%LET DESSTAT=%STR(PROC MEANS;RUN;);
…&DESTAT; WHAT HAPPENS WHEN THIS IS
RUN?
![Page 26: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/26.jpg)
Notes to consider about macro variables:
Double quotation marks, i.e. “ “ vs ‘ ‘ Variable length Special characters Condition operations Complex tasks
![Page 27: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/27.jpg)
Generating SAS Code Using Macros
Macro Processing› Need to define what the macro function
will be› Process that SAS uses to program macro
elements Macro Programs
› Offers more programming flexibility than macro variables in that you can pass information to parts of the macro program via macro parameters
![Page 28: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/28.jpg)
SAS Macros
Can include:› Constants such as literals, variables,
names, and statements› Assignments to macro variables› Macro programming statements› Macro language functions› Invocations of other functions› Nested macro definitions› LOGIC to conditionally generate SAS code
![Page 29: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/29.jpg)
Defining and Calling Macros
%MACRO macro-name;
macro text
%MEND macro-name;
![Page 30: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/30.jpg)
Example:%MACRO FUNMAC (DATASET, VAR1, VAR2);DATA RTEMP;SET &DATASET;RUN;PROC MEANS;VAR &VAR1 &VAR2;RUN;%MEND FUNMAC;
%FUNMAC (RDATA, AGE, WEIGHT);
![Page 31: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/31.jpg)
Application in SAS code:
Vitals data over time
Summary statistics using PROC TABULATE› Categorical variables› Continuous variables
![Page 32: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/32.jpg)
Vitals Data over time:DATA FORM05;SET ALISAH.FORM05;SUBJECTID=ZSUBJECTID;MAX_CVPRESS=F05Q02;WEIGHT=F05Q06;MAX_SBP=F05Q07;MAX_DBP=F05Q09;MAX_HR=F05Q13; KEEP SUBJECTID ZVISITID MAX_CVPRESS WEIGHT MAX_SBP
MAX_ DBP MAX_HR;RUN; PROC SORT DATA=FORM05 OUT=FORM05T;BY SUBJECTID
ZVISITID;RUN;
![Page 33: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/33.jpg)
Vitals Data over time:*MACRO TO CREATE MULTIPLE DATA SETS-ONE FOR EACH DAY OF VITALS;%MACRO VITALS(DAY, VISIT, DATASET);DATA &DAY._VITAL (KEEP=SUBJECTID MAX_CVPRESS&
WEIGHT&DAY MAX_SBP&DAY MAX_DBP&DAY MAX_HR&DAY);SET &DATASET;IF ZVISITID=&VISIT;MAX_CVPRESS&DAY=MAX_CVPRESS;WEIGHT&DAY=WEIGHT;MAX_SBP&DAY=MAX_SBP;MAX_DBP&DAY=MAX_DBP;MAX_HR&DAY=MAX_HR;
![Page 34: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/34.jpg)
Vitals Data over time:LABEL MAX_CVPRESS&DAY="MAXIMUM CENTRAL VENOUS
PRESSURE&DAY.";LABEL WEIGHT&DAY="WEIGHT&DAY.";LABEL MAX_SBP&DAY="MAX SYSTOLIC BLOOD PRESSURE&DAY.";LABEL MAX_DBP&DAY="MAX DIASTOLIC BLOOD PRESSURE&DAY.";LABEL MAX_HR&DAY="MAX HEART RATE&DAY.";RUN; PROC SORT DATA=&DAY._VITAL; BY SUBJECTID; RUN;%MEND VITALS;
![Page 35: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/35.jpg)
Vitals Data over time:*CALLING MACRO TO CREATE THE DATA SETS FOR VITALS;%VITALS (BASE, 1, FORM05);%VITALS (D1, 2 , FORM05);%VITALS (D2, 3 , FORM05);%VITALS (D3, 4 , FORM05);%VITALS (D4, 5 , FORM05);%VITALS (D5, 6 , FORM05);%VITALS (D6, 7 , FORM05);%VITALS (D7, 8 , FORM05);
![Page 36: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/36.jpg)
Vitals Data over time:/*MACRO TO CREATE NEW VARIABLES TO BE
USED IN MERGED DATA SET TO CALCULATE DIFFERENCE EACH DAY FROM BASELINE*/
%MACRO CHANGES(NEWVAR, VAR1, VAR2, VITAL, DAY);IF &VAR1 NE . AND &VAR2 NE . THEN %DO;
&NEWVAR=&VAR1-&VAR2;%END;
LABEL &NEWVAR="CHANGE IN &VITAL. FROM BASELINE TO &DAY.";
%MEND CHANGES;
![Page 37: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/37.jpg)
PROC TABULATE for Categorical Variables:
*CATEGORICAL MACRO BY TREATMENT GROUP;%MACRO CATEGORY( INDATA, MYVAR, MYTITLE) ;DATA INDATA;SET &INDATA; RUN;PROC TABULATE DATA=INDATA MISSING FORMAT=7.0
STYLE=[FONT_SIZE=1.5]; CLASS &MYVAR ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5
FOREGROUND=BLACK BACKGROUND=WHITE] ; TABLE ALL="ALL SUBJECTS" &MYVAR=" ",
(ZTREATMENTCODE ALL="ALL GROUPS")*(N PCTN<&myvar ALL>*F=PCTFMT7.)
/BOX=[LABEL="&MYTITLE“ STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]] MISSTEXT="0" PRINTMISS;
LABEL ZTREATMENTCODE="TREATMENT GROUP"; KEYLABEL N="N" PCTN="PERCENT"; CLASSLEV &MYVAR ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5
FOREGROUND=BLACK BACKGROUND=WHITE]; KEYWORD ALL N PCTN /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK
BACKGROUND=WHITE];RUN;%MEND;
![Page 38: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/38.jpg)
TREATMENT GROUP
ALL GROUPSTIER 1 (0.625
G/KG)TIER 2 (1.25
G/KG)TIER 3 (1.875
G/KG)
N PERCENT N PERCENT N PERCENT N PERCENT
GENDER
ALL SUBJECTS
20 100.0% 20 100.0% 7 100.0% 47 100.0%
MALE 5 25.0% 7 35.0% 1 14.3% 13 27.7%
FEMALE 15 75.0% 13 65.0% 6 85.7% 34 72.3%
![Page 39: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/39.jpg)
PROC TABULATE for Continuous Variables:
%MACRO CONTINUOUS (INDATA, MYVAR, MYTITLE);PROC TABULATE DATA=&INDATA FORMAT=10.0
STYLE=[FONT_SIZE=1.5]; VAR &MYVAR/STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK
BACKGROUND=WHITE]; CLASS ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5
FOREGROUND=BLACK BACKGROUND=WHITE]; TABLE ALL="ALL RANDOMIZED" &MYVAR*(N="TOTAL_N"
MEAN*F=6.2 STD="SD"*F=4.2 MEDIAN MIN MAX), (ZTREATMENTCODE ALL="ALL GROUPS") /BOX=[LABEL="&MYTITLE"
STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]];
KEYLABEL N=" "; CLASSLEV ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5
FOREGROUND=BLACK BACKGROUND=WHITE]; KEYWORD ALL N MEAN STD MEDIAN MIN MAX /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK
BACKGROUND=WHITE];RUN;%MEND;
![Page 40: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/40.jpg)
TREATMENT GROUP
ALL GROUPSTIER 1 (0.625
G/KG)TIER 2 (1.25
G/KG)TIER 3 (1.875
G/KG)
AGE AT BASELINE
TOTAL N 20 20 7 47
Mean 50.80 51.45 53.57 51.49
SD 15.2 11.4 13.0 13.1
Median 51 51 55 51
Min 25 33 38 25
Max 79 77 75 79
![Page 41: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/41.jpg)
Summary:
SAS macros can make programming much easier.
Not all programs are enhanced by SAS macros.
If you do choose to incorporate SAS macros:› Syntax is crucial!!› Debugging can be tricky!!› Efficiency can be tremendously improved!
![Page 42: Avoid repetitious SAS code Create generalizable and flexible SAS code Pass information from one part of a SAS job to another Conditionally execute](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649da95503460f94a96550/html5/thumbnails/42.jpg)
Suggestions for starting off:
Always write them one piece at a time Write your program using standard SAS
code first. After debugging that, add %MACRO
and %MEND. Then add your parameters.