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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

5 REPLIES 5
art297
Opal | Level 21

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;

jdub
Calcite | Level 5

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

art297
Opal | Level 21

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

jdub
Calcite | Level 5

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

art297
Opal | Level 21

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

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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