DATA Step, Macro, Functions and more

macro and proc summary question

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

macro and proc summary question

Howdy

I have a very basic macro I am trying use with proc summary.  I can see the problem but cannot figure out a solution.  Here is my code:

%macro supersum;

    proc summary

        data=occ_haves;

        by cluster;

        %do i=1 %to 12;

            var base&i;

        output out=occ_category sum=sbase&i;

        %end;

    run;

%mend supersum;

%supersum

I would like the loop to create one data set named occ_category containing 12 summation variables ranging from sbase1 to sbase12.  My code is creating a separate data set per summation variable created.  So, I get errors that say:

ERROR: Data set WORK.OCC_CATEGORY is already open for output

What I do not know is how or if I can create all 12 variables in the same data set.

Any help would be appreciated. 


Accepted Solutions
Solution
‎12-13-2012 07:01 PM
PROC Star
Posts: 7,356

Re: macro and proc summary question

I don't think you need a macro.  Does the following approximate what you want to accomplish?:

/*create some test data*/

data occ_haves;

  set sashelp.class (rename=(sex=cluster));

  array sbase(14);

  do i=1 to 14;

    sbase(i)=height;

  end;

run;

proc summary

  data=occ_haves nway;

  class cluster;

  var sbase1-sbase12;

  output out=occ_category sum= /autoname;

run;

View solution in original post


All Replies
Solution
‎12-13-2012 07:01 PM
PROC Star
Posts: 7,356

Re: macro and proc summary question

I don't think you need a macro.  Does the following approximate what you want to accomplish?:

/*create some test data*/

data occ_haves;

  set sashelp.class (rename=(sex=cluster));

  array sbase(14);

  do i=1 to 14;

    sbase(i)=height;

  end;

run;

proc summary

  data=occ_haves nway;

  class cluster;

  var sbase1-sbase12;

  output out=occ_category sum= /autoname;

run;

Contributor
Posts: 33

Re: macro and proc summary question

That is some handy code.  I am getting used to macros and think i unnecessarily use them at times.  Why did you use a 'class cluster' in the summary versus a 'by cluster'?  The /autoname is very cool as well.

thanks

PROC Star
Posts: 7,356

Re: macro and proc summary question

When you use class, as opposed to by, you don't have to presort the dataset.

Contributor
Posts: 33

Re: macro and proc summary question

oh man, I feel so slow to have not known this but also your answer is a total score!

PROC Star
Posts: 7,356

Re: macro and proc summary question

Don't feel slow! I've likely been using SAS for a bit longer than you have!

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 879 views
  • 3 likes
  • 2 in conversation