-
Notifications
You must be signed in to change notification settings - Fork 22
/
ROLL_REG.sas
99 lines (84 loc) · 2.32 KB
/
ROLL_REG.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Author: Edwin Hu
Date: 2013-05-24
# ROLL_REG #
## Summary ##
Runs rolling regressions.
## Variables ##
- dsetin: input dataset
- id: id variable
- date: date variable
- y: dependent variable
- x: independent variable
- ws: window size
- debug: debug mode (n)
## Usage ##
```
%IMPORT "~/git/sas/ROLL_REG.sas";
%ROLL_REG(dsetin=,
id=permno,
date=date,
y=exret,
x=mktrf,
ws=60,
debug=n);
```
*/
%MACRO ROLL_REG(dsetin=,
dsetout=,
id=permno,
date=date,
y=exret,
x=mktrf,
ws=60,
debug=n);
** Step 1: Generate Squares and Cross-Products **;
data _roll_in / view=_roll_in;
set &dsetin.(rename=(&id.=id &date.=date &y.=y &x.=x));
xy=x*y; xx=x*x; yy=y*y;
run;
** Step 2: Sum the squares and cross-products **;
proc expand data=_roll_in out=_roll_sscp (where=(_n=&ws.)) method=none;
by id ;
id date;
convert Y= _n / transformin=(*0) transformout=(+1 MOVSUM &ws.);
convert x y xy xx yy / transformout=( MOVSUM &ws.);
run;
** Step 3: Reshape the data into a TYPE=SSCP data set **;
data _roll_rsscp (type=SSCP keep = id date _TYPE_ _NAME_ intercept x y)
/ view=_roll_rsscp;
retain id date _TYPE_ _NAME_ intercept x y;
set _roll_sscp;
length _TYPE_ $8 _NAME_ $32 ;
** Store for later use **;
_sumy=y; _sumx=x;
_TYPE_="SSCP"; /* For the record type, not the data set type*/
** First output record is just N, and sums already in each original variable **;
_NAME_='Intercept';
Intercept=_n;
y=_sumy; x=_sumx;
output;
_name_="X";
intercept=_sumx; x=xx; y=xy;
output;
_name_="Y";
intercept=_sumy; x=xy; y=yy;
output;
_TYPE_='N';
_NAME_=' ';
Intercept = _n; Y=_N; X=_N;
output;
run;
proc reg data=_roll_rsscp(type=sscp obs=MAX) noprint
outest=&dsetout.(rename=(id=&id. date=&date. x=&x.)
keep=id date Intercept x);
by id date;
model y=x;
quit;
%put;%put ### DONE! ###;
%if %substr(%lowcase(&debug),1,1) = n %then %do;
proc datasets memtype=view noprint;
delete _roll:;
quit;
%end;
%MEND ROLL_REG;