Hi,
I have a dataset with 5003 variables. 5000 of these are required to call a macro (Var1 to Var5000).
How I can I avoid explicitly writing the lines below out every time?
%macro_run (Var1);
%macro_run (Var2);
...
%macro_run (Var5000);
Thanks in advance.
proc contents data=have out=want (keep=name where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;
data _null_;
set want;
rc = dosubl(cats('%macro_run (',name,');'));
run;
Hi @PetePatel
You can use the DOSUBL function (or CALL EXECUTE) to execute code immediately:
data list;
do i=1 to 5000;
var = cats('Var',i);
output;
end;
run;
data _null_;
set list;
rc = dosubl(cats('%macro_run (',Var,');'));
run;
You might want to refactor the way the code works to eliminate the need for this, but without details we cannot help with that.
You need to get the variable names into a list. (Note if they are literally VAR1 to VAR5000 then you can just generate them with a DO loop). So use PROC CONTENTS or the dictionary view/tables. Then use that data to generate the macro calls.
Example:
%let ds=sashelp.class;
proc contents data=&ds out=contents noprint; run;
filename code temp;
data _null_;
set contents;
file code;
put '%macro_run(' name ');' ;
run;
%include code / source2;
Question: How do you know which 3 variables to exclude?
Thanks, Tom.
The variables are named differently but I know I need to exclude date time and month. The rest should be included.
So imagine if I have a dataset:
Date
Time
Month
GRCV
GHPI_2
SIORP_0
RHP
TRYP
How can I call the macro for the last 5 variables only.
Currently I am doing:
%Run_Macro (GRCV);
...
%Run_Macro (TRYP);
proc contents data=have out=want (keep=name where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;
data _null_;
set want;
rc = dosubl(cats('%macro_run (',name,');'));
run;
Thanks!
Here is one approach to retrieve the last five variables of your dataset:
proc contents data=have out=want (keep=name varnum where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;
data list;
if _n_=1 then do;
declare hash h (dataset:'want', ordered:'d');
h.definekey('VARNUM');
h.definedata('VARNUM','NAME');
h.definedone();
declare hiter C('h');
end;
set want;
C.first();
output;
do i=1 to 4;
C.next();
output;
end;
stop;
run;
@PetePatel wrote:
Thanks, Tom.
The variables are named differently but I know I need to exclude date time and month. The rest should be included.
So imagine if I have a dataset:
Date
Time
Month
GRCV
GHPI_2
SIORP_0
RHP
TRYP
How can I call the macro for the last 5 variables only.
Currently I am doing:
%Run_Macro (GRCV);
...
%Run_Macro (TRYP);
Again, depending on what %Run_Macro does, you may not need macros at all
proc contents data=have out=_contents_ noprint;
run;
proc sql noprint;
select distinct name into :names separated by ' ' from _contents_
where upcase(name) not in ('DATE','TIME','MONTH');
quit;
proc summary data=have;
var &names;
output out=stats mean= std=/autoname;
run;
Depending on what %macro_run does, you may not need macros at all.
Example:
proc summary data=abc;
var var1-var5000;
output out=_stats_ mean= std= /autoname;
run;
Hello,
You can loop on the dataset columns names with "call vnext" and execute the macro with "call execute" as follows :
Edit: added "stop;".
%macro mymacro(vname);
%put Macro execution for variable &vname.;
%mend;
data have;
input a b c d e f g h i j;
cards;
1 2 3 4 5 6 7 8 9 0
;
run;
data _NULL_;
set have;
length _VNAME_ $32.;
do while(_VNAME_ ne "_VNAME_");
call vnext(_VNAME_);
if _VNAME_ not in ("h" "i" "j" "_VNAME_") then
call execute(cats('%nrstr(%mymacro)(',_VNAME_,')'));
end;
stop;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.