I use a proc summary to produce total premium figures for the last 5 years but I currently need to hard code the variables in as follows:
Proc summary data = test;
class premium;
var ep2008 ep 2009 ep 2010 ep 2011 ep 2012;
output out = test1 sum =
run;
Is there any way of making this more dynamic by having something like:
%let startyear = 2008;
%let endyear = 2012:
and build a macro within the proc summary with a loop or something
Thanks
There's no need to complicate a simple step by adding macro language. Just use a variable list:
var ep2008-ep2012;
No further changes are needed.
Sure, should be straightforward (not tested either of these but should be close):
Macro:
%macro Summ (start=,end=);
Proc summary data = test;
class premium;
var
%do I=&start. %to &end.;
ep%trim(&I.)
%end;
;
output out = test1 sum =
run;
%mend Summ;
Generated:
data _null_;
call execute('Proc summary data = test;
class premium;
var ');
do I=2008 to 2012;
call execute('ep'||put(I,4.)||" ");
end;
call execute('; output out = test1 sum =; run;');
run;
create date macros using data _null_ statement. Then use these variables in your code.
data _null_;
call symput ('endyear ', put(intnx('year',today(),-2),year4.); /*2012*/
call symput ('previous_yr1 ', put(intnx('year',today(),-3),year4.);
call symput ('previous_yr2', put(intnx('year',today(),-4),year4.);
call symput ('previous_yr3', put(intnx('year',today(),-5),year4.);
call symput ('startyear', put(intnx('year',today(),-6),year4.);
run;
%put &endyear &previous_yr1 &previous_yr2 &previous_yr3 &startyear ;
Proc summary data = test;
class premium;
var ep &startyear ep &previous_yr3 ep &previous_yr2 ep &previous_yr1 ep &endyear ;
output out = test1 sum =
run;
There's no need to complicate a simple step by adding macro language. Just use a variable list:
var ep2008-ep2012;
No further changes are needed.
Or if you have no other variables that start with "ep" that you do not want in the analysis you can use
var ep: ;
Then if you later add additional epyyyy variables they will be included automatically.
Hello,
You can create a macro that builds the names of the variables you plan to use within the var statement:
%macro a;
%Do y = 2008 %to 2013;
ep&y
%end;
%mend a;
Afterwards you can simply call the macro from inside proc means:
var %a;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.