SAS: MACROS Computing for Research I Spring 2014 January 22, 2014

46 Slides987.76 KB

SAS: MACROS 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 Conditionally execute data steps and PROCs Dynamically create code at execution time

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 execute

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 processor

The SAS Macro Facility Macro statements Macro processor Standard SAS statements

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 substitution

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 macros

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 efficiency

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 strings

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 VERSION

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 AUTOMATIC AUTOMATIC AUTOMATIC AUTOMATIC SYSDATE SYSDATE9 SYSDAY SYSTIME SYSVER 21JAN14 21JAN2014 Tuesday 17:45 9.2

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 21JAN2014 Log: Macro variable SYSDATE9 resolves to 21JAN2014

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.

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, 2014

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 text

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 (‘) Example

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 Example

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

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.

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.

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 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)

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;

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 errors

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 call

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 macro

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;

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)

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 necessary

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)

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 parameters

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 data

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, form02

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)

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 available

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 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

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

Back to top button