BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
r4321
Pyrite | Level 9

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 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
Accumulating data should only be done for reporting purposes. How will the users consume this data?
Data never sleeps
PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller
r4321
Pyrite | Level 9
Yeah, I don't believe such a rule exists either. But to let you in on my intent, it's to model the incrementally increasing threat posed by a particular effect across time. Thanks.
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 5 replies
  • 808 views
  • 1 like
  • 3 in conversation