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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2688 views
  • 3 likes
  • 2 in conversation