Skip to content

Instantly share code, notes, and snippets.

@joosti
Created November 29, 2014 13:34
Show Gist options
  • Save joosti/576c9651126bf71585dc to your computer and use it in GitHub Desktop.
Save joosti/576c9651126bf71585dc to your computer and use it in GitHub Desktop.
SAS data step, example BY and RETAIN
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