BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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);
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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(&param.);
%end;
%mend all;
%all

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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(&param.);
%end;
%mend all;
%all
ballardw
Super User

@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(&param.);
%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.);

Patrick
Opal | Level 21

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;
ballardw
Super User

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=&param.);
%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);

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1009 views
  • 2 likes
  • 4 in conversation