Created
November 29, 2014 13:34
-
-
Save joosti/576c9651126bf71585dc to your computer and use it in GitHub Desktop.
SAS data step, example BY and RETAIN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data dataOut; | |
set dataIn; | |
/* repeat for each firm */ | |
by gvkey; | |
/* remember totsale across observations */ | |
retain totsale; | |
/* set sum to zero for first obs for each firm */ | |
if first.gvkey then totsale = 0; | |
/* add sale to total, you may need to set sale to 0 if missing | |
e.g. if sale eq . then sale = 0; | |
*/ | |
totsale = totsale + sale; | |
/* output to dataset when last obs for each firm, omitting | |
this step will give all observations */ | |
if last.gvkey then output; | |
run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment