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.

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