BookmarkSubscribeRSS Feed
MaxRoy
Calcite | Level 5

Hi,

 

I would like to add new rows (observations) at the end of my dataset that would be the sum of other rows.

Please note that I'm using aggregate data (not record-level).

 

Here's what my data looks like:

 

REGION           YEAR           GENDER     TOTAL

     1                   2017              M                 10

     2                   2017              M                 5

     3                   2017              M                 3

     1                   2017              F                  11

     2                   2017              F                  4

     3                   2017              F                  7

     1                   2018              M                 10

     2                   2018              M                 8

     3                   2018              M                 6

     1                   2018              F                  9

     2                   2018              F                  7

     3                   2018              F                  11

 

What I want is a row at the end combining all the regions together (by sex and year) with the TOTAL sum.

Something like this:

 

REGION           YEAR           GENDER    TOTAL

   ALL                  2017              M                18

   ALL                  2017              F                 22

   ALL                  2018              M                24

   ALL                  2018              F                 27

 

 

Thank you!

1 REPLY 1
PaigeMiller
Diamond | Level 26

Use PROC SUMMARY to compute the sums, and then append the results to the end of the first data set.

 

proc summary data=have;
    class year gender;
    var total;
    output out=sums(drop=_:) sum=;
run;

proc append base=have new=sums;
run;
--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1332 views
  • 1 like
  • 2 in conversation