Presentation is loading. Please wait.

Presentation is loading. Please wait.

Passing Simple and Complex Parameters In and Out of Macros

Similar presentations


Presentation on theme: "Passing Simple and Complex Parameters In and Out of Macros"— Presentation transcript:

1 Passing Simple and Complex Parameters In and Out of Macros
This Too Shall Pass Passing Simple and Complex Parameters In and Out of Macros

2 Learning Objectives Differentiate between passing by value and reference Describe the syntactical differences between macro positional and keyword parameters Create macros passing input parameters by value Create macros passing input parameters by reference Create macros passing output parameters by value Create macros passing output parameter by reference Explain when and how to validate parameters passed by reference and by value

3 What’s In, What’s Out Conference white paper has a step-by-step walkthrough of all of the code Presentation will touch on the key concepts and syntax

4 Basic Macro Definition
%MACRO MyMacroName; /*do stuff here*/ %MEND; Execution %MyMacroName;

5 What’s the Problem? Lacks Flexibility (Reuse)
Lacks Portability (Sharing) %MACRO CreateLog(); DATA ExecutionLog; ATTRIB CalledBy LENGTH=$ 100; ATTRIB Message LENGTH=$ 1000; RUN; %MEND; %CreateLog();

6 Differentiate between passing by value and reference

7 Passing by Value The message contains everything need (simple values)
No external dependencies Limited validation required

8 Passing By Reference Message “points” to another location, where the actual contents resides Many external dependencies Handle with caution Requires greater validation Complex data types

9 Passing Input Parameters by Value
Two techniques: Positional Keyword Positional parameters are specified by the order, separated by commas Keyword parameters are specified by name and can be in any order

10 Positional Parameter Definition Execution
%MACRO CreateLogWithPositionalParameter ( DatasetName ); DATA &DatasetName; ATTRIB CalledBy LENGTH = $ 100; ATTRIB Message LENGTH = $ 1000; RUN; %MEND; %CreateLogWithPositionalParameter (MyDS); %CreateLogWithPositionalParameter (YourDS); %CreateLogWithPositionalParameter (OurDS); The previous example has been modified to pass in the dataset name as a positional parameter. Now the macro can be reused to create multiple datasets depending on project needs Note that the name of the new dataset appears in the parentheses with no qualifiers. To add more parameters, simply separate them in the definition (left side) and in the execution (right side)

11 Keyword Parameter Definition Execution
%MACRO CreateLogWithKeywordParameter ( DatasetName= ); DATA &DatasetName; ATTRIB CalledBy LENGTH = $ 100; ATTRIB Message LENGTH = $ 1000; RUN; %MEND; %CreateLogWithKeywordParameter (DatasetName=MyDS); (DatasetName=YourDS); (DatasetName=OurDS); Keyword parameters are particularly helpful when there is more than 1 or 2 parameters. Then parameter is specified by name. Note that both the definition and the call have changed. It is worth noting that when using multiple parameters, the order does not matter when using keyword parameters

12 Validating ‘by value’ input parameters
%MACRO CreateLogWithValidation (DatasetName=); %if %Length(&DatasetName)=0 %then %LET DatasetName = DefaultLogName; DATA &DatasetName; ATTRIB CalledBy LENGTH = $ 100; ATTRIB Message LENGTH = $ 1000; RUN; %MEND; Validating the parameters prevents runtime errors See my presentation on runtime error for techniques to control program execution when either runtime errors or validation errors occur

13 Passing Output Parameters by Value
%MACRO DatasetNameValid(Dataset); %LOCAL Result; %LET Result=0; %IF %LENGTH(&Dataset)>0 %THEN %LET Result=1; &Result %MEND; In this simple macro, a local variable “Result” is defined, set to a 1 or 0, then returned to the calling macro Note the variable reference &Result preceeds the %MEND statement. This is how the value is returned Keep in mind you cannot execute any PROCs and still return a macro value in this way. It will cause an error. EXTREMELY frustrating

14 Using Output Parameters
%MACRO CheckAndBuild(DatasetName=); %if %DatasetNameValid(&DatasetName)=1 %then %do; DATA &DatasetName; ATTRIB CalledBy LENGTH = $ 100; ATTRIB Message LENGTH = $ 1000; RUN; %end; %else %put Cannot create a dataset without a name; %MEND; DatasetNameValid can now be used in place of the built in function %LENGTH.

15 When to use ‘by reference’ input parameters
Complex Data types Datasets External files Arrays Delimited text

16 Passing Input Parameters by Reference
Same syntax as passing by reference Different interpretation Different validation

17 Passing Input Parameters By Reference
%MACRO DatasetExist(Dataset); %LOCAL Result; %LET Result=0; %IF %LENGTH(&Dataset)=0 %THEN %ELSE %LET Result =%SYSFUNC(EXIST(&Dataset)); &Result %MEND; Same syntax, the only difference is the interpretation. The text string is interpreted as a reference to an existing dataset. The macro “knows” something about the parameter tat

18 Validating By Reference Input Parameters
%MACRO ValidateAndBuild(DatasetName=); %if %DatasetNameValid(&DatasetName)=1 %then %do; %if %DatasetExist(&DatasetName)=0 %then DATA &DatasetName; ATTRIB CalledBy LENGTH = $ 100; ATTRIB Message LENGTH = $ 1000; RUN; %end; %else %put Dataset &DatasetName already exists; %else %put Cannot create a dataset without a name ; %MEND; Although these two check can be rolled into one macro, I’ve left them separate here to illustrate the difference. The DatasetName parameter is passed by value to DatasetNameValid, then by reference to DatasetExist.

19 Passing Output Parameters by Reference
Macros cannot perform complex operations (PROCs) and return a value Therefore, the output result must be listed as a parameter and provided by the calling procedure

20 Passing Output Parameters by Reference
%MACRO SummarizeLog(LogName=,SummaryName=); /*Validate input and output parameters*/ … /*Create Output dataset*/ PROC FREQ DATA=&LogName; TABLES CalledBy / NOPRINT OUT =&SummaryName; RUN; %MEND; LogName is the input parameter, SummaryName is the output parameter. Notice that the output parameter cannot be returned using the mend statement.

21 Validating By Reference Output Parameters
%Macro FillTheLog(DatasetName); %ValidateAndBuild(&DatasetName); %LogSome(&DatasetName); %SummarizeLog( LogName=&DatsetName, SummaryName=MySummary); %IF %DatsetExist(MySummary) %then %do; PROC PRINT DATA=MySummary; RUN; %end; %else %PUT MySummary was not created correctly; %mend; When calling a macro with a by reference output parameter, it is best to validate the parameter before using it.

22 Conclusions Macros can accept parameters
By position By Name By Reference By Value Reusable helper macros make parameter validation into a simple one-line statement Parameters reduce development time by making macros more flexible and reusable

23 Contact Information Name: Ted D. Williams, PharmD, BCPS Company: Magellan Method City/State: Middletown, RI Phone:


Download ppt "Passing Simple and Complex Parameters In and Out of Macros"

Similar presentations


Ads by Google