SAS Macros A. Dhandapani Pi i lS Principal Scientist i ti t (St (Statistics/Computer ti ti /C t A Applications li ti National Academy of Agricultural Research Management Rajendranagar, Hyderabad 500 407 dhandapani.appavoo@gmail.com p pp @g SAS Macros Useful for reducing SAS Codes Ease of doing reports/analysis which are frequently required C Complex l analysis l i which hi h require i use off many types of analysis – simplified into macros (like statistical genetics analysis) Facilitate passing information from one procedure to another SAS Macro Example 1 %lett myname = A. %l A Dh Dhandapani; d i Title1 “Output from a macro by &myname”; P Proc print i t data=sashelp.class; d t h l l Run; Example 1: Output (part) SAS Macro variables Nothing but text values Any text can be assigned to a macro variable %l t <macrovariablename> %let i bl = <value>; l Statement State e t assigns ass g s the t e value a ue to tthe e macro ac o variable Macro variable ariable can be used sed an anywhere here in SAS Code using &macrovariable Symbols % and & Macro language statement starts with a sign g % Macro variable references start with a symbol & Example 2 Task: Create a report to compute treatment means for a location Data File to use: Combined Variables: V i bl lloc, ttreat, t S Syield i ld Example 3 Macro Reports() – Prints treatments from all locations Example 4: Using System Variables Several System variables are available such as – &sysdate – &systime – &sysday Example 4 shows use of sysdate and sysday op option o in footnote oo o e sstatement ae e Macro Variables Macro variable can be referenced anywhere y in the SAS Program g other than data lines Macro variable can be used in open statement as well as within a macro program Macro variable can be created by SAS and by your programs Macro Variables Macro variable can be global or local Macro variable values are text Name assigned to a macro variable must b a valid be lid SAS N Name Defining and using Macro variables %lett ttreatment %l t t = ttreat; t PROC GLM DATA=crd; CLASS &t &treatment; t t %let statement defines a macro variable called treatment and assigns the value treat &treatment would be replaced by treat, when the code runs Using single and double quotes T reference To f a macro variable i bl iin a string, ti use d double bl quotes. Example: Correct statement: Title1 “Treatment Treatment Means for &loc”; &loc ; Incorrect: Title1 ‘Treatment Means for &loc’; This will print title as: Treatment Means for &loc %put statement %put statement prints the value of the macro variable name in LOG file Useful for trouble shooting/debugging E Example5.sas l 5 SYMBOLGEN OPTION SYMBOLGEN; Prints the value of the macro variable as resolved by SAS in LOG file T turn To t off, ff use OPTION O O NOSYMBOLGEN; OS O G ; Example6 Exercise: Find out the values %let compute = 12 *43; %let value1 = 30; %let value2 = 20; %let value3 = value1 + value2; % let company = “SAS SAS && Co”; Co ; Title1 “The value3 is &value3”; Title1 ‘The value3 is &value3’; Placing text BEFORE a macro variable %let number = 1; %let independent = X&number; Independent is resolved into X1 No need of any special symbol, if any prefix p e is s to be added to a macro ac o variable a ab e value. (Here X is added to macro variable value 1) Placing text AFTER a macro variable To add value and the end of a macro variable value, . (dot) is used Example(( Wrong) Example %let independent = loc; Proc Print Data = dset; Var &independent1 &independent2; Run; Interpreted as two macro variable, independent1 and independent2 not as loc1 & loc2 Placing text AFTER a macro variable Example( Correct) Example( p = loc; %let independent Proc Print Data = dset; . . VAR &independent 1 &independent 2; Run; Use of dot makes VAR statement resolves into VAR loc1 loc2; Referring a library %let mylib = training; PROC PRINT DATA = &mylib.crd; &mylib crd; &mylib.crd resolves into trainingcrd To resolve correctly, use PROC PRINT DATA = &mylib..crd; Use of && %let number = 1; %let val1 = section; %let doubleand = &&val&number; && forces SAS to evaluate twice First Pass: &val1 Second Pass:section Developing a Macro General Syntax is: %macro <name> (<params>); … %mend; To call a macro, invoke, %<name> (<params>); Running a macro Submit the code containing macro first and invoke it later ((Macro will be available for that session only) Place the program containing macro code into certain folder (called autocall) – Available whenever desired Use include statement Macro without parameters Example 9: Here global variable is used and macro code is submitted before invoking it. Example a p e 10: 0 ((include c ude state statement.sas) e t sas) Use include statement to include the macro and in invoke oke it Macros with parameter Any number of parameters can be defined in a macro p program g Parameters are separated by commas Th order The d and d number b off parameters t should be same while invoking the macro Example 11:Macro with one parameter Macros with default parameter values Macro may have parameters having default value Defined as parameter name = value If it is not specified, default value is used Example 12 First option invokes with both parameter values Second option invokes with one parameter Exercise: How to create a macro? Problem: P bl C Create t a generall macro ffor RBD Step 1: Write a code to do the analysis Step 2: Identify parameters Step 3: Replace the parameter with macro variables Step 4: Create Macro statement Step 5: Check it To move further %do-%end, %while%do%while-%end statements %if %then %else statements Proc SQL & Macro – useful for retrieving i f information ti ffrom output t t tables t bl SAS S S Help ep Book:SAS Macro Programming Made Easy Eas 2nd Edition (2007) b by Michale M M. Burlew, SAS Press