Skip to content

Instantly share code, notes, and snippets.

@joosti
Last active August 29, 2015 14:10
Show Gist options
  • Save joosti/2d62ef9a72948e677664 to your computer and use it in GitHub Desktop.
Save joosti/2d62ef9a72948e677664 to your computer and use it in GitHub Desktop.
SAS proc means, computing standard deviation of 5-year ROA
/*
compute standard deviation of ROA over last 5 most recent years (including current)
requires at least 3 years of data
dsin: dataset with gvkey, fyear, firmyear (gvkey || fyear) and roa
dsout: dataset generated: dsin with roa_stddev appended */
%macro roaStdev(dsin=, dsout=);
proc sql;
create table comp_roa1 as
select a.firmyear, b.roa, b.firmyear as prev_firmyear
from
&dsin a, &dsin b
where
a.gvkey = b.gvkey
and a.fyear -4<= b.fyear <= a.fyear -0; /* this year and 4 years before */
quit;
proc sort data = comp_roa1; by firmyear;run;
/* compute stddev */
proc means data=comp_roa1 NOPRINT;
OUTPUT OUT=comp_roa2 STD=/autoname;
var roa;
by firmyear ;
run;
/* dataset with standard deviation of ROA:
firmyear, ROA_StdDev
require at least 3 obs
*/
data comp_roa2;
set comp_roa2;
if _FREQ_ >=3;
run;
/* create output dataset */
proc sql;
create table &dsout as
select a.*, b.roa_StdDev from &dsin a left join comp_roa2 b on a.firmyear = b.firmyear;
quit;
%mend;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment