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

Hello,

 

I have:

 

x      y      z

10   1      67

10   1     430

10   2     122

10   2     51

10   2     219

 

I want

 

x      y      z     z_sum

10   1      67    .

10   1     430   497

10   2     122    .

10   2     51      .

10   2     219    392

 

I am having trouble summing it by two variables. Any ideas? Thanks for any help!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@Ethid wrote:

 

 

I am having trouble summing it by two variables. Any ideas? Thanks for any help!


What troubles are you having? What approach are you taking?

 

Is that what you want your final output to be, or are you looking for just the summary statistics. 

How to summarize data:

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic.sas

 

How to add a statistic to your main data set:

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

 


@Ethid wrote:

Hello,

 

I have:

 

x      y      z

10   1      67

10   1     430

10   2     122

10   2     51

10   2     219

 

I want

 

x      y      z     z_sum

10   1      67    .

10   1     430   497

10   2     122    .

10   2     51      .

10   2     219    392

 

I am having trouble summing it by two variables. Any ideas? Thanks for any help!


 

View solution in original post

5 REPLIES 5
Reeza
Super User

@Ethid wrote:

 

 

I am having trouble summing it by two variables. Any ideas? Thanks for any help!


What troubles are you having? What approach are you taking?

 

Is that what you want your final output to be, or are you looking for just the summary statistics. 

How to summarize data:

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic.sas

 

How to add a statistic to your main data set:

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

 


@Ethid wrote:

Hello,

 

I have:

 

x      y      z

10   1      67

10   1     430

10   2     122

10   2     51

10   2     219

 

I want

 

x      y      z     z_sum

10   1      67    .

10   1     430   497

10   2     122    .

10   2     51      .

10   2     219    392

 

I am having trouble summing it by two variables. Any ideas? Thanks for any help!


 

novinosrin
Tourmaline | Level 20
data have;
input cx      y      z;
cards;
10   1      67
10   1     430
10   2     122
10   2     51
10   2     219
;
data want;
set have;
by cx      y;
retain t;
if first.y then t=z;
else t+z;
if last.y then z_sum=t;
drop t;
run;
Ethid
Fluorite | Level 6

Thank you Novinosrin, this should work!

KDG
SAS Employee KDG
SAS Employee

Note that for your real data, you'll have to add in a proc sort and sort by (in this case) cx and y: 

proc sort data=have; by cx y; run; 

 

In this case you didn't get an error because your data was already sorted, but your real data probably isn't and you'll get an error about by variables not being properly sorted. 


Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF

View now: on-demand content for SAS users

PaigeMiller
Diamond | Level 26

I just don't see the point of writing your own data step code to do summation, when SAS already has a PROC that does this, and handles missings and does error checking, all at no extra charge.

 

proc summary nway data=have;
    class cx y;
    var z;
    output out=want sum=sum_z;
run;
--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2068 views
  • 1 like
  • 5 in conversation