SAS Macros Basics (Part-II) CALL SYMPUT( ) SYMGET( )

· CALL SYMPUT(‘macro_variable_name’ ‘expression’, <G / L>)

This expression creates a macro variable during data step execution, and assigns it a value resolved by the ‘expression’ given as second variable. It is useful to give a dynamic value to macro variable depending upon a conditional logic, as it gets resolved during execution time

Third argument is an optional argument where ‘G’ instructs SAS to store this macro variable in global symbol table where ‘L’ is used to save it Local symbol table.

· CALL SYMPUTX()

It is same as CALL SYMPUT(); just a difference is that it removes leading and trailing blanks from the macro variable name as well as from the resolved value as well

· SYMGET(‘macro_variable_nmae’)

Used to get value of macro variable in data step.

· PROC SQL can also be used to create macro variable using into: clause, e.g.

PROC SQL;

Select category into: cat from table_name where some_conditon;

Quit;

If sql query returns more than one row either we can create multiple macro variables or can assign multiple values to single macro variable separate by given delimiter

For creating multiple macro variables use following sql

Select category into : cat1- :catn from table_name;

For assigning multiple values to single macro variable use following sql

Select category into:cat separate by ‘,’ from table_name where some_condition;

<< Prev: SAS Macros Basics (Part-I)

>> Next: SAS Macros with parameters (parameterized macros)

SAS Macros have been studied within 3 parts here. Please go through all the three parts Sequentially;

Follow the links to study;

Part 1: SAS Macros Basics (Part-I)

Part 2: SAS Macros Basics (Part-II) CALL SYMPUT( ) SYMGET( )

Part 3: SAS Macros with parameters (parameterized macros)

· Macro Definition with parameters creates number of macros equal to parameter which are stored in local symbol table, however those macro variables are available during the macro execution only.

· What is Drawback of %str() function :

It allows normal tokenization within its arguments. So unmatched quotes or parameters or ‘%’, ‘&’ characters may create a warning or errors while compiling.

To avoid this we can use ‘%’ character before quotes or parenthesis. ‘%’ character works as escape character

Instead we can use %NRSTR() which treats those macro trigger characters as normal characters

· %STR() function is used to mask mnemonic characters at compilation time while %BQUOTE() is used to mask those at execution time

· %SUPERQ() is another compile time masking function which takes only one argument and treats it as macro variable, whose value gets resolved at execution time. It means the macro variable gets resolved only at run time

· Why to use SYSFUNC()?

Many functionalities are not available in Macro mode, meaning macro variables of many functions are not available so to cater with it we can use %SYSFUNC(BASE_SAS_FUNCTION()), e.g. %SYSFUNC(INTNX(week,19650,19000,0))

· However %SYSFUNC() takes only one function at a time as argument so we cant nest multiple functions inside one %SYSFUNC()

· PUTN( ) and PUTC( )

These functions are used to apply numeric and character format to macro variables; should be used inside %SYSFUNC( ) only

· Any MACRO expression returns string (i.e. character) values only, so we often need them to convert into numbers;

%let num = 5;

%let addn = 2+3;

num = &addn /* will give ERROR */

So Instead we can use %EVAL() function like

num = %EVAL(&addn);

while %EVAL is used for integers %EVALF() can be used more precisely for floating points, dates and long integers

%SYSEVALF() returns results in BEST32. format

· %RETURN statement is used to terminate the execution of SAS macro. If %return statement is encountered then other SAS statements following it does not get executed

· %ABORT statement aborts the SAS session