Help using Base SAS procedures

Proc summary

Accepted Solution Solved
Reply
Super Contributor
Posts: 259
Accepted Solution

Proc summary

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


Accepted Solutions
Solution
‎08-18-2014 09:36 AM
Super User
Posts: 5,085

Re: Proc summary

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


All Replies
Super User
Super User
Posts: 7,413

Re: Proc summary

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;

Frequent Contributor
Posts: 117

Re: Proc summary

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;

Solution
‎08-18-2014 09:36 AM
Super User
Posts: 5,085

Re: Proc summary

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.

Super User
Posts: 10,520

Re: Proc summary

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.

Super Contributor
Posts: 305

Re: Proc summary

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 448 views
  • 3 likes
  • 6 in conversation