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

Hello SAS community,

 

I came up with a quick and simple question at first glance.

But I am struggling with optimal solution.

 

Here is the dataset (part of it)

dataset for question.PNG

 

I have 5 groups (colvar).

My question is, how I can summarize the variable 'n' by group, summing from ">=21" to ">=3", so that for last observation here I would have the summary of all the distributed values above it? (e.g. new 'sum' variable for obs=7 would be  393 (9+42+56+80+98+75+33)).

 

I have  tried something like this

data want;
	retain sum1 0;
	set adsl5_2;
		by colvar;
		sum1=sum1 + n;
run;

But it is processing whole dataset and not by group

webart999ARM_0-1668712665765.png

 

 

Thank you in advance for any help.

 

Sincerely

Artur

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this. Here, we set sum1 back to zero when we hit a new by-group. 

 

data want;
   set adsl5_2;
   by colvar;
   if first.colvar then sum1 = 0;
   sum1 + n;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Try this. Here, we set sum1 back to zero when we hit a new by-group. 

 

data want;
   set adsl5_2;
   by colvar;
   if first.colvar then sum1 = 0;
   sum1 + n;
run;

PaigeMiller
Diamond | Level 26

You are using the word "summary" and "sum" as if they are interchangeable and mean the same thing. I wouldn't do that, but assuming that is what you intended, then this should work:

 

data want;
    set have;
    by colvar;
    if first.colvar then summary=0;
    summary+n;
run;
--
Paige Miller
webart999ARM
Quartz | Level 8
This one works as well, thank you.
And I agree with your suggestion about "summary" and "sum", indeed, they are different things.

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
  • 4 replies
  • 434 views
  • 2 likes
  • 3 in conversation