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

Hi all,

 

I have a dataset wich looks like this;

 

id      quarter1       quarter2         quarter3

1            1                  0                    0

1            1                  0                    0

1            0                  1                    0

2            0                  0                    1

2            0                  0                    1

2            0                  0                    1

 

And I want to sum up each row based on the id so that output looks like this;

 

id         quarter1        quarter2          quarter3

1             2                     1                       0

2             0                     0                       3

 

 

Thank you!!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Proc summary data=have nway;

    class id;

    var var1 var2 var3;

    output out = want sum=;

run;

Will generate an output data set. There will be two additional variables _type_ and _freq_ that provide information about the combinations of class variables (_type_) and number of records used (_freq_).

 

 Also Proc Report and Proc tabulate will generate tables with prettier output.

 

And from @RW9:

Also you could do:

 

 

proc sql;
  create table WANT as
  select ID, 
            sum(QUARTER1) as QUARTER1,
            sum(QUARTER2) as QUARTER2,
            sum(QUARTER3) as QUARTER3
  from   HAVE
  group by ID;
quit;

View solution in original post

3 REPLIES 3
ballardw
Super User

Proc summary data=have nway;

    class id;

    var var1 var2 var3;

    output out = want sum=;

run;

Will generate an output data set. There will be two additional variables _type_ and _freq_ that provide information about the combinations of class variables (_type_) and number of records used (_freq_).

 

 Also Proc Report and Proc tabulate will generate tables with prettier output.

 

And from @RW9:

Also you could do:

 

 

proc sql;
  create table WANT as
  select ID, 
            sum(QUARTER1) as QUARTER1,
            sum(QUARTER2) as QUARTER2,
            sum(QUARTER3) as QUARTER3
  from   HAVE
  group by ID;
quit;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Also you could do:

proc sql;

  create table WANT as

  select ID, 

            sum(QUARTER1) as QUARTER1,

            sum(QUARTER2) as QUARTER2,

            sum(QUARTER3) as QUARTER3

  from   HAVE

  group by ID;

quit;

Steelers_In_DC
Barite | Level 11

Here is a solutions:

 

data have;
input id quarter1 quarter2 quarter3;
cards;
1            1                  0                    0
1            1                  0                    0
1            0                  1                    0
2            0                  0                    1
2            0                  0                    1
2            0                  0                    1
;

proc sql;
create table want as
select distinct
id,
sum(quarter1) as quarter1,
sum(quarter2) as quarter2,
sum(quarter3) as quarter3
from have
group by id;

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
  • 3 replies
  • 30707 views
  • 1 like
  • 4 in conversation