Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS macro


May 26, 2021 SAS


Table of contents


SAS has a powerful programming feature called macros that allow us to avoid duplicate snipppies and use them again and again when needed. I t also helps create dynamic variables in your code that can use different values for different run instances of the same code. A macro can also be declared as a block of code, and it will be reused multiple times in a manner similar to a macro variable. W e'll see both in the following example.

The macro variable

These are variables that hold values that are used again and again by SAS programs. T hey are declared at the beginning of the SAS program and called later in the body. T hey can be global or local.

Global macro variables

They are called global macro variables because they can be accessed by any SAS program available in the SAS environment. T ypically, they are system-assigned variables accessed by multiple programs. A general example is the system date.

Cases

The following is an example of an SAS variable called SYSDATE that represents the system date. C onsider a scenario to print the system date in the title of the daily SAS report that generated the report. T he title displays the current date and date without encoding any values for them. W e use the built-in SAS dataset called CARS available in theASHELP library.

proc print data = sashelp.cars;
where make = 'Audi' and type = 'Sports' ;
 TITLE "Sales as of &SYSDAY &SYSDATE";
run;

When we run the code above, we get the following output.

SAS macro

Local macro variables

These variables can be accessed by SAS programs, where they are declared as part of the program. T hey are typically used to provide different variables to the same SAS statement SL, allowing them to work with different observations of the dataset.

Syntactic

Local variables are declared using the following syntax.

% LET (Macro Variable Name) = Value;

The value field here can take any number, text, or date value that the program needs. T he macro variable name is any valid SAS variable.

Cases

These variables are used by SAS statements and are attached to the beginning of the variable name. T he following program gets all our observations of 'Audi' and type 'Sports'. I f we want different results for make, we need to change the make_name of the variable without changing any other part of the program. I n the case of a bring-in program, this variable can be referenced again in any SAS statement.

%LET make_name = 'Audi';
%LET type_name = 'Sports';
proc print data = sashelp.cars;
where make = &make_name and type = &type_name ;
 TITLE "Sales as of &SYSDAY &SYSDATE";
run;

When the above code runs, we get the same output as the previous program. B ut let'type_name change the name to 'Wagon' and run the same program. W e will get the following results.

SAS macro

Macro program

A macro is a set of SAS statements that are referenced by a name and used anywhere in a program. I t starts with the %MACRO statement and ends with the %MEND statement.

Syntactic

Local variables are declared using the following syntax.

# Creating a Macro program.
%MACRO (Param1, Param2,….Paramn);

Macro Statements;

%MEND;

# Calling a Macro program.
%MacroName (Value1, Value2,…..Valuen);

Cases

The following program decrypts show_result SET staemnets under a macro called "Staemnets"; T his macro is being called by another SAS statement.

%MACRO show_result(make_ , type_);
proc print data = sashelp.cars;
where make = "&make_" and type = "&type_" ;
TITLE "Sales as of &SYSDAY &SYSDATE";
run;
%MEND;

%show_result(BMW,SUV);

When we run the code above, we get the following output.

SAS macro

Common macros

SAS has many MACRO statements, which are built into the SAS programming language. T hey are used by other SAS programs and are not explicitly stated. A common example is - terminating a program or capturing the runtime value of a variable in the program log when some conditions are met. H ere are some examples.

Macro%PUT

This macro statement writes text or macro variable information to the SAS log. I n the following example, the value of the variable "today" is written to the program log.

data _null_;
CALL SYMPUT ('today',
TRIM(PUT("&sysdate"d,worddate22.)));
run;
%put &today;

Macro%RETURN

When the calculation of a condition is true, executing the macro causes the currently executed macro to terminate normally. I n the following example, the macro terminates other connections when the value of the variable "val" changes to 10.

%macro check_condition(val);
   %if &val = 10 %then %return;

    data p;
       x=34.2;
    run;  

%mend check_condition;  

%check_condition(11)  ;

Macro%END

This macro definition contains a %DO%WHILE loop that ends with a %END statement as needed. I n the following example, a macro named Test takes user input and runs a DO loop with this input value. T he end of the DO loop is implemented by the %end statement, while the end of the macro is implemented by the %mend statement.

%macro test(finish);
   %let i=1;
   %do %while (&i <&finish);
      %put the value of i is &i;
      %let i=%eval(&i+1);
   %end;
%mend test;
%test(5)