SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS...

28
SAS Macros SAS Macros A. Dhandapani A. Dhandapani Pi i lSi ti t (St ti ti /C t A li ti Pi i lSi ti t (St ti ti /C t A li ti Principal Scientist (Statistics/Computer Applications Principal Scientist (Statistics/Computer Applications National Academy of Agricultural Research National Academy of Agricultural Research Management Management Management Management Rajendranagar, Hyderabad 500 407 Rajendranagar, Hyderabad 500 407 dhandapani.appavoo@gmail.com dhandapani.appavoo@gmail.com

Transcript of SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS...

Page 1: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

SAS MacrosSAS MacrosA. DhandapaniA. Dhandapani

P i i l S i ti t (St ti ti /C t A li tiP i i l S i ti t (St ti ti /C t A li tiPrincipal Scientist (Statistics/Computer ApplicationsPrincipal Scientist (Statistics/Computer ApplicationsNational Academy of Agricultural Research National Academy of Agricultural Research

ManagementManagementManagementManagementRajendranagar, Hyderabad 500 407Rajendranagar, Hyderabad 500 [email protected]@gmail.comp pp @gp pp @g

Page 2: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

SAS MacrosSAS MacrosSAS MacrosSAS Macros

Useful for reducing SAS CodesUseful for reducing SAS CodesEase of doing reports/analysis which areEase of doing reports/analysis which areEase of doing reports/analysis which are Ease of doing reports/analysis which are frequently requiredfrequently requiredC l l i hi h i fC l l i hi h i fComplex analysis which require use of Complex analysis which require use of many types of analysis many types of analysis –– simplified into simplified into macros (like statistical genetics analysis)macros (like statistical genetics analysis)Facilitate passing information from oneFacilitate passing information from oneFacilitate passing information from one Facilitate passing information from one procedure to anotherprocedure to another

Page 3: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

SAS Macro Example 1SAS Macro Example 1SAS Macro Example 1SAS Macro Example 1

%l t A Dh d i%let myname = A. Dhandapani;Title1 “Output from a macro by &myname”;P i t d t h l lProc print data=sashelp.class;Run;

Page 4: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Example 1: Output (part)Example 1: Output (part)Example 1: Output (part)Example 1: Output (part)

Page 5: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

SAS Macro variablesSAS Macro variablesSAS Macro variablesSAS Macro variables

Nothing but text valuesNothing but text valuesAny text can be assigned to a macroAny text can be assigned to a macroAny text can be assigned to a macro Any text can be assigned to a macro variablevariable%l t i bl l%l t i bl l%let <macrovariablename> = <value>; %let <macrovariablename> = <value>;

Statement assigns the value to the macro Statement assigns the value to the macro State e t ass g s t e a ue to t e ac oState e t ass g s t e a ue to t e ac ovariablevariableMacro ariable can be sed an here inMacro ariable can be sed an here inMacro variable can be used anywhere in Macro variable can be used anywhere in SAS Code using &macrovariableSAS Code using &macrovariable

Page 6: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Symbols % and &Symbols % and &Symbols % and &Symbols % and &

Macro language statement starts with a Macro language statement starts with a sign %sign %ggMacro variable references start with a Macro variable references start with a symbol &symbol &symbol &symbol &

Page 7: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Example 2Example 2Example 2Example 2

Task: Create a report to compute Task: Create a report to compute treatment means for a locationtreatment means for a locationData File to use: CombinedData File to use: CombinedV i bl l t t S i ldV i bl l t t S i ldVariables: loc, treat, SyieldVariables: loc, treat, Syield

Page 8: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Example 3Example 3Example 3Example 3

Macro Reports() Macro Reports() –– Prints treatments from Prints treatments from all locationsall locations

Page 9: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Example 4: Using System Example 4: Using System VariablesVariables

Several System variables are available Several System variables are available such as such as –– &sysdate&sysdate

&systime&systime–– &systime&systime–– &sysday&sysday

Example 4 shows use of sysdate and Example 4 shows use of sysdate and sysday option in footnote statementsysday option in footnote statementsysday op o oo o e s a e esysday op o oo o e s a e e

Page 10: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Macro VariablesMacro VariablesMacro VariablesMacro Variables

Macro variable can be referenced Macro variable can be referenced anywhere in the SAS Program other than anywhere in the SAS Program other than y gy gdata linesdata lines

Macro variable can be used in openMacro variable can be used in open Macro variable can be used in open Macro variable can be used in open statement as well as within a macro statement as well as within a macro programprogram

Macro variable can be created by SASMacro variable can be created by SAS Macro variable can be created by SAS Macro variable can be created by SAS and by your programsand by your programs

Page 11: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Macro VariablesMacro VariablesMacro VariablesMacro Variables

Macro variable can be global or localMacro variable can be global or local Macro variable values are textMacro variable values are text Macro variable values are textMacro variable values are text Name assigned to a macro variable must Name assigned to a macro variable must

b lid SAS Nb lid SAS Nbe a valid SAS Namebe a valid SAS Name

Page 12: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Defining and using Macro variablesDefining and using Macro variablesDefining and using Macro variablesDefining and using Macro variables

%l t t t t t t%let treatment = treat;PROC GLM DATA=crd;CLASS &t t tCLASS &treatment;

%let statement defines a macro variable called treatment and assigns the value treattreatment and assigns the value treat

&treatment would be replaced by treat, when the code runsthe code runs

Page 13: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Using single and double quotesUsing single and double quotesUsing single and double quotesUsing single and double quotesT f i bl i t i d blT f i bl i t i d blTo reference a macro variable in a string, use double To reference a macro variable in a string, use double quotes.quotes.Example:Example:Example:Example:Correct statement:Correct statement:Title1 “Treatment Means for &loc”;Title1 “Treatment Means for &loc”;Title1 Treatment Means for &loc ;Title1 Treatment Means for &loc ;

Incorrect:Incorrect:Title1 ‘Treatment Means for &loc’;Title1 ‘Treatment Means for &loc’;This will print title as:This will print title as:

Treatment Means for &locTreatment Means for &loc

Page 14: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

%put statement%put statement%put statement%put statement

%put statement prints the value of the %put statement prints the value of the macro variable name in LOG filemacro variable name in LOG fileUseful for trouble shooting/debuggingUseful for trouble shooting/debuggingE l 5E l 5Example5.sas Example5.sas

Page 15: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

SYMBOLGENSYMBOLGENSYMBOLGENSYMBOLGEN

OPTION SYMBOLGEN;OPTION SYMBOLGEN;Prints the value of the macro variable asPrints the value of the macro variable asPrints the value of the macro variable as Prints the value of the macro variable as

resolved by SAS in LOG fileresolved by SAS in LOG fileT t ffT t ffTo turn off, useTo turn off, useOPTION NOSYMBOLGEN;OPTION NOSYMBOLGEN;O O OS O G ;O O OS O G ;

Example6Example6

Page 16: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Exercise: Find out the valuesExercise: Find out the valuesExercise: Find out the valuesExercise: Find out the values

%let compute = 12 *43;%let compute = 12 *43;%let value1 = 30;%let value1 = 30;%let value1 30;%let value1 30;%let value2 = 20;%let value2 = 20;%let value3 = value1 + value2;%let value3 = value1 + value2;% let company = “SAS && Co”;% let company = “SAS && Co”;% let company = SAS && Co ;% let company = SAS && Co ;Title1 “The value3 is &value3”;Title1 “The value3 is &value3”;Title1 ‘The value3 is &value3’;Title1 ‘The value3 is &value3’;

Page 17: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Placing text BEFORE a macro Placing text BEFORE a macro variablevariable

%let number = 1;%let number = 1;%let independent = X&number;%let independent = X&number;%let independent X&number;%let independent X&number;

Independent is resolved into X1Independent is resolved into X1No need of any special symbol, if any No need of any special symbol, if any prefix is to be added to a macro variable prefix is to be added to a macro variable p e s to be added to a ac o a ab ep e s to be added to a ac o a ab evalue. (Here X is added to macro variable value. (Here X is added to macro variable value 1)value 1)value 1)value 1)

Page 18: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Placing text AFTER a macro Placing text AFTER a macro variablevariable

To add value and the end of a macro variable To add value and the end of a macro variable value, . (dot) is usedvalue, . (dot) is used

ExampleExample( Wrong)( Wrong)%let independent = loc;%let independent = loc;%let independent loc;%let independent loc;Proc Print Data = dset;Proc Print Data = dset;Var &independent1 &independent2;Var &independent1 &independent2;Var &independent1 &independent2;Var &independent1 &independent2;Run;Run;Interpreted as two macro variable, independent1 Interpreted as two macro variable, independent1

and independent2 not as loc1 & loc2and independent2 not as loc1 & loc2

Page 19: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Placing text AFTER a macro Placing text AFTER a macro variablevariable

ExampleExample( Correct)( Correct)%let independent = loc;%let independent = loc;ppProc Print Data = dset;Proc Print Data = dset;

VAR &independentVAR &independent.1 &independent1 &independent.2;2;Run;Run;Run;Run;Use of dot makes VAR statement resolves intoUse of dot makes VAR statement resolves intoVAR loc1 loc2;VAR loc1 loc2;

Page 20: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Referring a libraryReferring a libraryReferring a libraryReferring a library

%let mylib = training;%let mylib = training;PROC PRINT DATA = &mylib crd;PROC PRINT DATA = &mylib crd;PROC PRINT DATA &mylib.crd;PROC PRINT DATA &mylib.crd;

&mylib.crd resolves into &mylib.crd resolves into trainingcrdtrainingcrdtrainingcrdtrainingcrd

To resolve correctly, useTo resolve correctly, usePROC PRINT DATA = &mylib..crd;PROC PRINT DATA = &mylib..crd;

Page 21: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Use of &&Use of &&Use of &&Use of &&

%let number = 1;%let number = 1;%let val1 = section;%let val1 = section;%let val1 section;%let val1 section;%let doubleand = &&val&number;%let doubleand = &&val&number;

&& forces SAS to evaluate twice&& forces SAS to evaluate twice&& forces SAS to evaluate twice&& forces SAS to evaluate twiceFirst Pass: &val1First Pass: &val1Second Pass:sectionSecond Pass:section

Page 22: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Developing a MacroDeveloping a MacroDeveloping a MacroDeveloping a Macro

General Syntax is:General Syntax is:%macro <name> (<params>);%macro <name> (<params>);%macro <name> (<params>);%macro <name> (<params>);……

%mend;%mend;%mend;%mend;To call a macro, invoke,To call a macro, invoke,%<name> (<params>);%<name> (<params>);

Page 23: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Running a macroRunning a macroRunning a macroRunning a macro

Submit the code containing macro first and Submit the code containing macro first and invoke it later (Macro will be available for invoke it later (Macro will be available for ((that session only)that session only)Place the program containing macro codePlace the program containing macro codePlace the program containing macro code Place the program containing macro code into certain folder (called autocall) into certain folder (called autocall) ––Available whenever desiredAvailable whenever desiredUse include statementUse include statementUse include statementUse include statement

Page 24: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Macro without parametersMacro without parametersMacro without parametersMacro without parameters

Example 9:Example 9:Here global variable is used and macro codeHere global variable is used and macro codeHere global variable is used and macro code Here global variable is used and macro code

is submitted before invoking it.is submitted before invoking it.

Example 10: (include statement.sas)Example 10: (include statement.sas)a p e 0 ( c ude state e t sas)a p e 0 ( c ude state e t sas)Use include statement to include the macro Use include statement to include the macro

and in oke itand in oke itand invoke itand invoke it

Page 25: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Macros with parameterMacros with parameterMacros with parameterMacros with parameter

Any number of parameters can be defined Any number of parameters can be defined in a macro programin a macro programp gp gParameters are separated by commasParameters are separated by commasTh d d b f tTh d d b f tThe order and number of parameters The order and number of parameters should be same while invoking the macroshould be same while invoking the macro

Example 11:Macro with one parameterExample 11:Macro with one parameter

Page 26: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Macros with default parameter Macros with default parameter valuesvalues

Macro may have parameters having default Macro may have parameters having default valuevalueDefined as parameter name = valueDefined as parameter name = valueIf it is not specified, default value is usedIf it is not specified, default value is usedIf it is not specified, default value is usedIf it is not specified, default value is used

Example 12Example 12Example 12Example 12First option invokes with both parameter valuesFirst option invokes with both parameter valuesSecond option invokes with one parameterSecond option invokes with one parameter

Page 27: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

Exercise: How to create a macro?Exercise: How to create a macro?Exercise: How to create a macro?Exercise: How to create a macro?

P bl C t l f RBDP bl C t l f RBDProblem: Create a general macro for RBDProblem: Create a general macro for RBDStep 1:Step 1:Write a code to do the analysisWrite a code to do the analysisWrite a code to do the analysisWrite a code to do the analysisStep 2:Step 2:Identify parametersIdentify parametersIdentify parametersIdentify parametersStep 3:Step 3:Replace the parameter with macro variablesReplace the parameter with macro variablesReplace the parameter with macro variablesReplace the parameter with macro variablesStep 4:Step 4:Create Macro statementCreate Macro statementStep 5:Step 5:Check itCheck it

Page 28: SAS Macros - Welcome to IASRIapps.iasri.res.in/sscnars/sas_manual/21-SAS Macros.pdf · SAS MacrosSAS Macros Useful for reducing SAS Codes ... Example 1: Output (part)Example 1: Output

To move furtherTo move furtherTo move furtherTo move further

%do%do--%end, %while%end, %while--%end statements%end statements%if %then %else statements%if %then %else statements%if %then %else statements%if %then %else statementsProc SQL & Macro Proc SQL & Macro –– useful for retrieving useful for retrieving i f ti f t t t bli f ti f t t t blinformation from output tablesinformation from output tablesSAS HelpSAS HelpS S e pS S e pBook:SAS Macro Programming Made Book:SAS Macro Programming Made Eas 2Eas 2ndnd Edition (2007) b Michale MEdition (2007) b Michale MEasy 2Easy 2ndnd Edition (2007) by Michale M. Edition (2007) by Michale M. Burlew, SAS Press Burlew, SAS Press