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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Vish33
Lapis Lazuli | Level 10

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;

Astounding
PROC Star

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.

ballardw
Super User

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.

Loko
Barite | Level 11

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 5 replies
  • 1446 views
  • 3 likes
  • 6 in conversation