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

Hi,

could anyone please tell me if is it possible to group over one variable and then use another group  in same table.

I would like to obtain results like that:

 

group variable.png

GroupVar1 - first level grouping variable

Var2 - sum of var2 in datasets grouped by GroupVar1

GroupVar3 - second level grouping variable

Var4, Var5 - sum of variables grouped by GroupVar1  and GroupVar3

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

This can be done with Proc REPORT, you do need to add a dummy content column for the first summarized var.

 

The code below shows an example, please note that there are some additional statements, that where added for verification of the result.

 


data cars;
  set sashelp.cars;
  hpg = 1;
run;
proc report data=cars spanrows;
  column origin hpg horsepower=hpsum type invoice cylinders _dummy;
  define origin / group style={textalign=center VERTICALALIGN=MIDDLE };
  define hpg / group style={textalign=center VERTICALALIGN=MIDDLE };
  define hpsum / analysis sum
/*    noprint*/
  ;
  define type / group;
  define cylinders / analysis;
  define invoice / analysis;
  define _dummy / computed
/*    noprint*/
  ;

  compute before origin;
     tempHpg = hpsum;
  endcomp;

  /* assign value to grouping var */
  compute _dummy;
    /* just for verification */
    _dummy = tempHpg;
    hpg = tempHpg;
  endcomp;

  /* just for validation */
  break after origin / summarize;
run;

View solution in original post

1 REPLY 1
BrunoMueller
SAS Super FREQ

This can be done with Proc REPORT, you do need to add a dummy content column for the first summarized var.

 

The code below shows an example, please note that there are some additional statements, that where added for verification of the result.

 


data cars;
  set sashelp.cars;
  hpg = 1;
run;
proc report data=cars spanrows;
  column origin hpg horsepower=hpsum type invoice cylinders _dummy;
  define origin / group style={textalign=center VERTICALALIGN=MIDDLE };
  define hpg / group style={textalign=center VERTICALALIGN=MIDDLE };
  define hpsum / analysis sum
/*    noprint*/
  ;
  define type / group;
  define cylinders / analysis;
  define invoice / analysis;
  define _dummy / computed
/*    noprint*/
  ;

  compute before origin;
     tempHpg = hpsum;
  endcomp;

  /* assign value to grouping var */
  compute _dummy;
    /* just for verification */
    _dummy = tempHpg;
    hpg = tempHpg;
  endcomp;

  /* just for validation */
  break after origin / summarize;
run;

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
  • 1 reply
  • 1305 views
  • 0 likes
  • 2 in conversation