Hello all,
I'm looking for code so that I can aggregate a variable across observations by group in a cumulative fashion... See below for desired result:
Unitid TimePeriod Variable DesiredVariable
1 1 1 1
1 2 0 1
1 3 0 1
1 4 2 3
1 5 0 3
2 1 0 0
2 2 4 4
2 3 0 4
2 4 1 5
2 5 1 6
If someone could help me out, then you guidance would be greatly appreciated!
Thank you,
Rich
Assuming the data is sorted by UnitId and TimePeriod
UNTESTED CODE
data want; set have; by unitid; if first.unitid then sum=0; sum+variable; run;
@LinusH wrote:
Accumulating data should only be done for reporting purposes.
I have never heard such a rule. And furthermore, it doesn't seem like a rule I would follow anyway. Could you please explain?
Assuming the data is sorted by UnitId and TimePeriod
UNTESTED CODE
data want; set have; by unitid; if first.unitid then sum=0; sum+variable; 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!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.