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;
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!
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.
Ready to level-up your skills? Choose your own adventure.