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

Hi.  I have the following Proc SQL that sums variables at the active_description level.  Is there a way to also get an overall activity_description total by pay_data_month in the same query?  I seem to remember SQL has some sort up roll up functionality.  File attachment shows the current output.  So the desired output would have an additional row after each grouping of pay_data_month with a sub total for all activities.

 

Any help here would be great.  Thanks so much.

 

PROC SQL;
CREATE TABLE &SITE._HCR_PD_ACTVTY_SUM AS
SELECT PAY_DATA_MONTH
		, AREA
		, HCRID
		, ACTIVITY_DESCRIPTION
		, SUM(PAY_AMOUNT) FORMAT DOLLAR15.2 AS MNTHLY_PAY_AMNT
		, SUM(RATE) AS MNTHLY_RATE
		, SUM(UNITS) FORMAT 15.2 AS MNTHLY_UNITS
FROM &SITE._HCR_PD
WHERE HCRID='65036'
GROUP BY PAY_DATA_MONTH
		,AREA
		,HCRID
		,ACTIVITY_DESCRIPTION
;QUIT;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@buechler66 wrote:
Wow that's very cool. Would that be able to be altered to also get a grand total across all months?
types () pay_data_month pay_data_month*area*hcrid*activity_description;
--
Paige Miller

View solution in original post

7 REPLIES 7
Patrick
Opal | Level 21

All aggregate functions like sum() will be based on your grouping. You can't have different aggregation levels using a single Group By statement.

buechler66
Barite | Level 11

Hmm, could you share the syntax for doing it in a single sas step?

Patrick
Opal | Level 21

@buechler66 wrote:

Hmm, could you share the syntax for doing it in a single sas step?


Now you were faster than me deleting this comment from my post. It still would require two passes through the data as I guess you would want the "totals" added to all the rows with the sub-totals. So yes, one could have it in a single datastep using some "DOW" processing - but still two passes through the data.

PaigeMiller
Diamond | Level 26

This type of summing over multiple different groupings is very easy in one call to PROC SUMMARY.

 

proc summary data=&SITE._HCR_PD(where=(HCRID='65036'));
     class pay_data_month area hcrid activity_description;
     types pay_data_month pay_data_month*area*hcrid*activity_description;
     var pay_amount rate units;
     output out=_sums_ sum=;
run;
--
Paige Miller
buechler66
Barite | Level 11
Wow that's very cool. Would that be able to be altered to also get a grand total across all months?
PaigeMiller
Diamond | Level 26

@buechler66 wrote:
Wow that's very cool. Would that be able to be altered to also get a grand total across all months?
types () pay_data_month pay_data_month*area*hcrid*activity_description;
--
Paige Miller
buechler66
Barite | Level 11
Awesome. Much simpler than Proc SQL (with no Rollup functionality). Thanks so much for taking the time to with this. I appreciate it very much!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 7014 views
  • 3 likes
  • 3 in conversation