BookmarkSubscribeRSS Feed
SASguyCO
Calcite | Level 5
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;
4 REPLIES 4
ieva
Pyrite | Level 9
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! 🙂
Patrick
Opal | Level 21
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
SASguyCO
Calcite | Level 5
Great suggestions, Thanks for the help all!
Peter_C
Rhodochrosite | Level 12
"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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 880 views
  • 0 likes
  • 4 in conversation