Hello
Lets say that user define a macro var that contain names of fields (predictors) with comma between.
Lets say that based on this macro variable should run a macro.
What is the way to run the macros automatically based on the user macro var?
%let Predictor_Var_list=X R T Q V S M W;
%information_value(VAR=X);
%information_value(VAR=R);
%information_value(VAR=T);
%information_value(VAR=Q);
%information_value(VAR=V);
%information_value(VAR=S);
Two ways:
from a DATA step:
data _null_;
do i = 1 to countw("&predictor_var_list.");
call execute(cats('%nrstr(%information_value(',scan("&predictor_var_list.",i),'))'));
end;
run;
from a macro:
%macro all;
%do i = 1 %to %sysfunc(countw(&predictor_var_list.));
%let param = %scan(&predictor_var_list.,i.);
%information_value(¶m.);
%end;
%mend all;
%all
Two ways:
from a DATA step:
data _null_;
do i = 1 to countw("&predictor_var_list.");
call execute(cats('%nrstr(%information_value(',scan("&predictor_var_list.",i),'))'));
end;
run;
from a macro:
%macro all;
%do i = 1 %to %sysfunc(countw(&predictor_var_list.));
%let param = %scan(&predictor_var_list.,i.);
%information_value(¶m.);
%end;
%mend all;
%all
@Kurt_Bremser wrote:
Two ways:
from a DATA step:
data _null_; do i = 1 to countw("&predictor_var_list."); call execute(cats('%nrstr(%information_value(',scan("&predictor_var_list.",i),'))')); end; run;
from a macro:
%macro all; %do i = 1 %to %sysfunc(countw(&predictor_var_list.)); %let param = %scan(&predictor_var_list.,i.); %information_value(¶m.); %end; %mend all; %all
Missing the & for I in the %scan. I thought I was the only one that did that....
%let param = %scan(&predictor_var_list.,&i.);
Here one option.
%macro information_value(VAR=);
%put Parameter Var has value &var;
%mend;
%let Predictor_Var_list=X R T Q V S M W;
data _null_;
do i=1 to countw("&Predictor_Var_list");
call execute(cats('%information_value(VAR=',scan("&Predictor_Var_list",i),');'));
end;
run;
Your comment says "a macro var that contain names of fields (predictors) with comma between.". But the only place that you show a macro variable there are no commas. So which is it?
Note that in general with macro language the comma is the worst possible delimiter in macro values as they are quite likely to be interpreted as intention macro delimiters and generate errors in function or macro calls.
Here is an example using @Kurt_Bremser second solution of the list without and with commas
/* define a macro so missing macro definition isn't the error*/ %macro information_value (Var=); %put Parm is: &var; %mend; %let Predictor_Var_list=X R T; %macro all; %do i = 1 %to %sysfunc(countw(&predictor_var_list.)); %let param = %scan(&predictor_var_list.,&i.); %information_value(var=¶m.); %end; %mend all; %all /* with comma list*/ %let Predictor_Var_list=X,R,T; %put Run with Predictor_Var_list= &Predictor_Var_list.; %all
@Ronein wrote:
Hello
Lets say that user define a macro var that contain names of fields (predictors) with comma between.
Lets say that based on this macro variable should run a macro.
What is the way to run the macros automatically based on the user macro var?
%let Predictor_Var_list=X R T Q V S M W; %information_value(VAR=X); %information_value(VAR=R); %information_value(VAR=T); %information_value(VAR=Q); %information_value(VAR=V); %information_value(VAR=S);
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.