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

Dear Madam/Sir,

I would like to get the cumulative sum for five executives by firm and year as below.

                                                                                                        firm        year   executive id    shrown_tot_pct                              

949596979899100101102103
0010042009092493.861
0010042009092520.694
0010042009237810.458
0010042009339790.286
0010042009361990.121
0010042010092493.833
0010042010092520.897
0010042010339790.361
0010042010361990.177
0010042010417870.265

                   I used the following code, but I did not get the desired results (null).

 


proc sort data=y1; by gvkey year; run;
data y2; set y1;
by gvkey year;
retain sumown;
if first.gvkey then sumown=shrown_tot_pct;
else sumown=sumown+shrown_tot_pct; run;

 

Any advice will be highly appreciated.

 

Thanks

Joon1

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star
 wrote:

Dear Madam/Sir,
I would like to get the cumulative sum for five executives by firm and year as below.
                                                                                                        firm        year   executive id    shrown_tot_pct                              


949596979899100101102103
001004
2009
09249
3.861
001004
2009
09252
0.694
001004
2009
23781
0.458
001004
2009
33979
0.286
001004
2009
36199
0.121
001004
2010
09249
3.833
001004
2010
09252
0.897
001004
2010
33979
0.361
001004
2010
36199
0.177
001004
2010
41787
0.265
                   I used the following code, but I did not get the desired results (null).
 

proc sort data=y1; by gvkey year; run;
data y2; set y1;
by gvkey year;
retain sumown;
if first.gvkey then sumown=shrown_tot_pct;
else sumown=sumown+shrown_tot_pct; run;
 
Any advice will be highly appreciated.
 
Thanks
Joon1
 

It is not apparent why you got nulls for SUMOWN.  Please show the log, and a sample of the resulting data.
 
BUT, even if you didn't get nulls, you would NOT get want you expect.  Your program only resets SUMOWN when you start a gvkey.  You presumably want to do this every time you start a new year within a gvkey.   So instead of "if first.gvkey ...", use "if first.year".  
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Description, table and code do not match.

Please post data in usable form and take care that you are using the same names in description, data and code.

mkeintz
PROC Star
 wrote:

Dear Madam/Sir,
I would like to get the cumulative sum for five executives by firm and year as below.
                                                                                                        firm        year   executive id    shrown_tot_pct                              


949596979899100101102103
001004
2009
09249
3.861
001004
2009
09252
0.694
001004
2009
23781
0.458
001004
2009
33979
0.286
001004
2009
36199
0.121
001004
2010
09249
3.833
001004
2010
09252
0.897
001004
2010
33979
0.361
001004
2010
36199
0.177
001004
2010
41787
0.265
                   I used the following code, but I did not get the desired results (null).
 

proc sort data=y1; by gvkey year; run;
data y2; set y1;
by gvkey year;
retain sumown;
if first.gvkey then sumown=shrown_tot_pct;
else sumown=sumown+shrown_tot_pct; run;
 
Any advice will be highly appreciated.
 
Thanks
Joon1
 

It is not apparent why you got nulls for SUMOWN.  Please show the log, and a sample of the resulting data.
 
BUT, even if you didn't get nulls, you would NOT get want you expect.  Your program only resets SUMOWN when you start a gvkey.  You presumably want to do this every time you start a new year within a gvkey.   So instead of "if first.gvkey ...", use "if first.year".  
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
joon1
Quartz | Level 8

Thank you! I appreciate it.

Joon1

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1091 views
  • 0 likes
  • 3 in conversation