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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.