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)
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
Thank you in advance for any help.
Sincerely
Artur
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;
.
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;
.
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;
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!
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.