BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi Everyone,

 

I want to run a macro as below:

 

%macro check (var=var1);

%macro check(var=var2);

...

%macro check (var=var1000);

 

So I need to run it over a list of 1000 variable. 

I have the list of the name in file "name"

 

Is there any way that I dont have to write the %check() 1000 time?

 

Thank you,

 

HC

 

data source; 
input abc xyz var1 varx;
datalines;
1 0 0 0
0 2 0 1
1 1 1 2
;run;

data name; 
input  name $;
datalines;
abc
xyz
var1
varx
;run;

%macro check (var=);
data _temp_&var; set source;
if abc>0;
run;
%mend;

%check (var=abc);
%check (var=xyz);
%check (var=var1);
%check (var=varx);
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Look at CALL EXECUTE.

 

/********************************************************************
Example : Call macro using parameters from data set
********************************************************************/

proc sort data=sashelp.class out=class;
by age sex;
run;

%macro summary(age=, sex=);

proc print data=sashelp.class;
	where age=&age and sex="&sex";
run;

%mend;

data sample;
set class;
by age sex;

if last.sex;
string =
    catt('%summary(age=', age, ',sex=', sex, ');');
put string;
run;


data _null_;
set sample;
call execute(string);
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

Look at CALL EXECUTE.

 

/********************************************************************
Example : Call macro using parameters from data set
********************************************************************/

proc sort data=sashelp.class out=class;
by age sex;
run;

%macro summary(age=, sex=);

proc print data=sashelp.class;
	where age=&age and sex="&sex";
run;

%mend;

data sample;
set class;
by age sex;

if last.sex;
string =
    catt('%summary(age=', age, ',sex=', sex, ');');
put string;
run;


data _null_;
set sample;
call execute(string);
run;
Reeza
Super User

Look at CALL EXECUTE.

 

/********************************************************************
Example : Call macro using parameters from data set
********************************************************************/

proc sort data=sashelp.class out=class;
by age sex;
run;

%macro summary(age=, sex=);

proc print data=sashelp.class;
	where age=&age and sex="&sex";
run;

%mend;

data sample;
set class;
by age sex;

if last.sex;
string =
    catt('%summary(age=', age, ',sex=', sex, ');');
put string;
run;


data _null_;
set sample;
call execute(string);
run;
art297
Opal | Level 21

I agree with @Reeza, but don't see why you'd even need a macro. I'd do something like the following:

data source; 
input abc xyz var1 varx;
datalines;
1 0 0 0
0 2 0 1
1 1 1 2
;run;

data name; 
input  name $;
datalines;
abc
xyz
var1
varx
;run;

data _null_;
  set name;
  length forexec $80;
  forexec=catt('data _temp_',name,';set source; if');
  call execute(forexec);
  forexec=catt(name,'>0;run;');
  call execute(forexec);
run;

Art, CEO, AnalystFinder.com

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 966 views
  • 2 likes
  • 3 in conversation