SAS Macro 4

<<< Click here to go back


Macro variables are blank spaces that can be filled by any value as we have seen in the previous example ( M and N replacing rectangles)

Macro variables are dynamic variables declared that can be assigned various values at different points of time. 

These macro variables can be of two types : Global and Local 

Local Macro variables are assigned and are usable within a Macro, so we will discuss these along with Macro itself.

Global Macro Variables : These variables are the variables that can be assigned anywhere in the SAS code and once initialized (selected and F3ed), can be used anywhere in the SAS code(s), within the SAS session (across simultaneously open codes).

There are multiple ways to assign global macro variable(s).

1. %Let (we have seen it earlier)
2. In data step using call symput option
3. In Proc SQL, using into: method

You can choose the method on the basis of your requirement. Let us now discuss these three methods one by one.

1. %Let Method


This is the simplest method to assign macro variables. We simply write :

%Let a =  Europe ;
%let Report_date = 01Jan2009 ;

and the desired values are assigned to "a" and "Report_date" and these become macro variables.

we can test these by printing their values in log using :

%put &a.;
%put &Report_date.;

The “%let”  creates a macro variable

The “%put”  prints text in the SAS log

The symbol “&” before a macro variable extracts the value stored inside a macro variable.

Although sometimes redundant, it is advised to use  "." after macro variable name while being resolved with &, as sometimes it becomes necessary.

Properties of %let :

General Form :      %LET variable_name = value;

Rules for the %LET statement:

- Maximum length of variable_name is 32 characters (All rules of SAS name apply)
- Minimum length of value is 0 characters (null value)
- Numeric values are stored as character strings
- Mathematical expressions are not evaluated
- The case of value is preserved
- Quotes bounding literals are stored as part of value

- Leading and trailing blanks are removed from value before the assignment is made.






Next ... Now we shall discuss Data Step and Proc SQL methods of defining Global macro variables.



No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.