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

Hello,

 

I want to calculate cumulative sum by three variables. The following data give some ideas about what I want:

 

ISINFyearGroupValuesWant
AAA201011010
AAA201021121
AAA201031233
AAA201111515
AAA201121833
AAA201131952
BBB201012020
BBB201023050
BBB201034090
BBB201111010
BBB201121525
BBB201132045

 

Basically, I want a variable "want"  which is the cumulative sum of "values" by ISIN, Fyear and group. I found some SAS codes from SAS community, but those codes are used to calculate the cumulative sum by one or two variables. I tried to modify and test those codes, but they did not work correctly. I think I need a new code. I wonder if anyone can help me a little bit. Thank you very much for your help!

 

Cheers, Thierry.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you examine the results you are seeking, you are actually getting a cumulative sum by 2 variables, not 3.  GROUP has nothing to do with the logic.  So assuming your data set is already sorted (making the BY statement legal):

 

data want;

set have;

by isin fyear;

if first.fyear then want=values;

else want + values;

run;

View solution in original post

2 REPLIES 2
Astounding
PROC Star

If you examine the results you are seeking, you are actually getting a cumulative sum by 2 variables, not 3.  GROUP has nothing to do with the logic.  So assuming your data set is already sorted (making the BY statement legal):

 

data want;

set have;

by isin fyear;

if first.fyear then want=values;

else want + values;

run;

tritringuyen
Quartz | Level 8
Thank you very much! It is very useful! 🙂

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
  • 2 replies
  • 15766 views
  • 2 likes
  • 2 in conversation