Help using Base SAS procedures

how to use a list of macro variables?

Reply
Contributor
Posts: 42

how to use a list of macro variables?

This is probably a really dumb question, but how in the world do you sum or better yet, use a list of macro variables with just the "-" without having to write out evey single &var_1, &var_2.....&var_i. I use this all the time for non macro variables, but thought it would be the same for macro variables.

For example:

%let var1=1;
%let var2=2;
%let var3=3;

data t;
varall=%sysfunc(sum(of &var1-&var3));
run;

or even as such:

%let dset1=dsn_2011;
%let dset2=dsn_2010;
%let dset3=dsn_2009;

data all;
&dset1-&dset3;
run;
Frequent Contributor
Posts: 82

Re: how to use a list of macro variables?

Hi,

Some thoughts on your question:

You could create macro variable list (though, this won't work on that sum (of ..) question, as macro variables generated are not numbers but character string):


data macro_variables;
input variables $;
datalines;
dsn_2011
dsn_2010
dsn_2009
;
run;

proc sql noprint;
select variables into: macro_var_list separated by ' ' from macro_variables;
quit;


data all;
set &macro_var_list;
run;


Another way how to get similar results:

%macro collect_data;

data all;
set %do i=2009 %to 2011;
dsn_&i
%end;;
run;
%mend ;
%collect_data;

Hope it helps or at least gives some ideas to think about! Smiley Happy
Respected Advisor
Posts: 4,173

Re: how to use a list of macro variables?

Hi

You probabely would solve your first example this way:

%let var1=1;
%let var2=2;
%let var3=3;

proc sql noprint;
select cats('&',name) into :MacroVarList separated by ','
from DICTIONARY.MACROS
where name like 'VAR%';
%put &MacroVarList;
quit;

data _null_;
varall=sum(&MacroVarList);
put varall=;
run;


And the second one very similar only that the separator would be a blank and not a comma.

HTH
Patrick
Contributor
Posts: 42

Re: how to use a list of macro variables?

Great suggestions, Thanks for the help all!
Valued Guide
Posts: 2,177

Re: how to use a list of macro variables?

"using a list of macro variables"
is close to a thread I worked on earlier
"list programming with sas "
1 here is a list
2 here is a process
3 how to apply the process(=2) to each item in the list (=1)

see
List Processing - Make Light Work of List Processing in SAS®
http://www2.sas.com/proceedings/sugi31/012-31.pdf
Ask a Question
Discussion stats
  • 4 replies
  • 138 views
  • 0 likes
  • 4 in conversation