Wednesday, March 21, 2018

Calling a SAS macro from a data step

Controlling a macro using a data step can be very helpful when you want to use a set of values in a macro and would like a clean way to introduce them.

The real trick is using the line "call excecute(%macro_name);" This allows the macro to be called for each row of data.

Sample code:

Data set:

data data_1;
   input name $ age;
datalines;
Fred 5
Mike 8 
Joel 6
;


Macro sample:

%macro print(name,n);
%put "&name. is &n years old.";
%mend;

Macro call:

Data _null_;
set data_1;
call execute('%print('||name||','||'age)');
run;


Thanks for your time today.