-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.html
552 lines (492 loc) · 15.7 KB
/
index.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<!doctype html>
<html lang="en">
<head>
<title>SAS Macros</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="generator" content="Org-mode">
<meta name="author" content="Edwin Hu">
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<style type="text/css">
/* org mode styles on top of twbs */
html {
position: relative;
min-height: 100%;
}
body {
font-size: 18px;
margin-bottom: 105px;
}
footer {
position: absolute;
bottom: 0;
width: 100%;
height: 101px;
background-color: #f5f5f5;
}
footer > div {
padding: 10px;
}
footer p {
margin: 0 0 5px;
text-align: center;
font-size: 16px;
}
#table-of-contents {
margin-top: 20px;
margin-bottom: 20px;
}
blockquote p {
font-size: 18px;
}
pre {
font-size: 16px;
}
.footpara {
display: inline-block;
}
figcaption {
font-size: 16px;
color: #666;
font-style: italic;
padding-bottom: 15px;
}
/* from twbs docs */
.bs-docs-sidebar.affix {
position: static;
}
@media (min-width: 768px) {
.bs-docs-sidebar {
padding-left: 20px;
}
}
/* All levels of nav */
.bs-docs-sidebar .nav > li > a {
display: block;
padding: 4px 20px;
font-size: 14px;
font-weight: 500;
color: #999;
}
.bs-docs-sidebar .nav > li > a:hover,
.bs-docs-sidebar .nav > li > a:focus {
padding-left: 19px;
color: #A1283B;
text-decoration: none;
background-color: transparent;
border-left: 1px solid #A1283B;
}
.bs-docs-sidebar .nav > .active > a,
.bs-docs-sidebar .nav > .active:hover > a,
.bs-docs-sidebar .nav > .active:focus > a {
padding-left: 18px;
font-weight: bold;
color: #A1283B;
background-color: transparent;
border-left: 2px solid #A1283B;
}
/* Nav: second level (shown on .active) */
.bs-docs-sidebar .nav .nav {
display: none; /* Hide by default, but at >768px, show it */
padding-bottom: 10px;
}
.bs-docs-sidebar .nav .nav > li > a {
padding-top: 1px;
padding-bottom: 1px;
padding-left: 30px;
font-size: 12px;
font-weight: normal;
}
.bs-docs-sidebar .nav .nav > li > a:hover,
.bs-docs-sidebar .nav .nav > li > a:focus {
padding-left: 29px;
}
.bs-docs-sidebar .nav .nav > .active > a,
.bs-docs-sidebar .nav .nav > .active:hover > a,
.bs-docs-sidebar .nav .nav > .active:focus > a {
padding-left: 28px;
font-weight: 500;
}
/* Nav: third level (shown on .active) */
.bs-docs-sidebar .nav .nav .nav {
padding-bottom: 10px;
}
.bs-docs-sidebar .nav .nav .nav > li > a {
padding-top: 1px;
padding-bottom: 1px;
padding-left: 40px;
font-size: 12px;
font-weight: normal;
}
.bs-docs-sidebar .nav .nav .nav > li > a:hover,
.bs-docs-sidebar .nav .nav .nav > li > a:focus {
padding-left: 39px;
}
.bs-docs-sidebar .nav .nav .nav > .active > a,
.bs-docs-sidebar .nav .nav .nav > .active:hover > a,
.bs-docs-sidebar .nav .nav .nav > .active:focus > a {
padding-left: 38px;
font-weight: 500;
}
/* Show and affix the side nav when space allows it */
@media (min-width: 992px) {
.bs-docs-sidebar .nav > .active > ul {
display: block;
}
/* Widen the fixed sidebar */
.bs-docs-sidebar.affix,
.bs-docs-sidebar.affix-bottom {
width: 213px;
}
.bs-docs-sidebar.affix {
position: fixed; /* Undo the static from mobile first approach */
top: 20px;
}
.bs-docs-sidebar.affix-bottom {
position: absolute; /* Undo the static from mobile first approach */
}
.bs-docs-sidebar.affix .bs-docs-sidenav,.bs-docs-sidebar.affix-bottom .bs-docs-sidenav {
margin-top: 0;
margin-bottom: 0
}
}
@media (min-width: 1200px) {
/* Widen the fixed sidebar again */
.bs-docs-sidebar.affix-bottom,
.bs-docs-sidebar.affix {
width: 263px;
}
}
</style>
<script type="text/javascript">
$(function() {
'use strict';
$('.bs-docs-sidebar li').first().addClass('active');
$(document.body).scrollspy({target: '.bs-docs-sidebar'});
$('.bs-docs-sidebar').affix();
});
</script><script async src="https://www.googletagmanager.com/gtag/js?id=UA-67919104-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-67919104-1');
</script>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="content" class="container">
<div class="row"><div class="col-md-9"><h1 class="title">SAS Macros</h1>
<p>
A collection of SAS Macros that I frequently use and occasionally update.
</p>
<p>
The full repo can be found at <a href="http://github.com/edwinhu/sas">http://github.com/edwinhu/sas</a>
</p>
<div id="outline-container-crsp" class="outline-2">
<h2 id="crsp"><a id="sec-" name="sec-"></a>CRSP</h2>
<div class="outline-text-2" id="text-crsp">
</div>
<div id="outline-container-cclink" class="outline-3">
<h3 id="cclink"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/CC_LINK.sas">CC_LINK</a></h3>
<div class="outline-text-3" id="text-cclink">
<p>
Links COMPUSTAT GVKEYs to CRSP PERMNOs.
</p>
<p>
Takes a file which contains GVKEYs and dates and merges in the
appropriate PERMNOs. This handles a lot of silly merge issues.
</p>
<ul class="org-ul">
<li>dsetin: Input Dataset
</li>
<li>dsetout: Output Dataset Name, default compx
</li>
<li>datevar: date variable to use (datadate, rdq)
</li>
<li>keep_vars: variables to keep
</li>
</ul>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/CC_LINK.sas"</span>;
<span style="color: #D9DAA2;">%CC_LINK(</span>dsetin=&syslast.,
dsetout=compx,
datevar=datadate,
keep_vars=);
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-comp" class="outline-2">
<h2 id="comp"><a id="sec-" name="sec-"></a>COMPUSTAT</h2>
<div class="outline-text-2" id="text-comp">
</div>
<div id="outline-container-ead" class="outline-3">
<h3 id="ead"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/COMP_EAD.sas">COMP_EAD</a></h3>
<div class="outline-text-3" id="text-ead">
<p>
Gets quarterly earnings announcement dates from COMPUSTAT and fetches
CRSP PERMNOs. Makes a file <code>comp_ead_events</code>.
</p>
<ul class="org-ul">
<li>comp_vars: list of COMPUSTAT variables to fetch
</li>
<li>filter: filters to place on COMPUSTAT quarterly
</li>
</ul>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/COMP_EAD.sas"</span>;
<span style="color: #D9DAA2;">%COMP_EAD(</span>comp_vars=gvkey fyearq fqtr conm datadate rdq
epsfxq epspxq
prccq ajexq
spiq cshoq cshprq cshfdq
saleq atq
fyr datafqtr,
filter=<span style="color: #339CDB;">not</span> <span style="color: #339CDB;">missing</span>(saleq) or atq><span style="color: #B5CEA8; font-weight: bold;">0</span>
);
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-util" class="outline-2">
<h2 id="util"><a id="sec-" name="sec-"></a>Utilities</h2>
<div class="outline-text-2" id="text-util">
</div>
<div id="outline-container-ff93" class="outline-3">
<h3 id="ff93"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/FF93_FACTORS.sas">FF93_FACTORS</a></h3>
<div class="outline-text-3" id="text-ff93">
<p>
Replicates Fama-French (1993) SMB & HML Factors. Currently obtaining
correlations of 99.5% (SMB) 97.8% (HML) for sample period 1975 to 2019.
</p>
<p>
Also useful in case you want to get the breakpoints or portfolio compositions.
</p>
</div>
</div>
<div id="outline-container-event" class="outline-3">
<h3 id="event"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/EVENT_MACROS.sas">EVENT_MACROS</a></h3>
<div class="outline-text-3" id="text-event">
<p>
A collection of event study macros adapted from WRDS.
</p>
<p>
<code>%EVENT_SETUP</code> sets the global variables, including the start and end
dates of the estimation and event windows. It also sets the formula to
use for the abnormal returns.
</p>
<p>
<code>%EVENT_EXPAND</code> does the bulk of the merging of datasets, creating the
actual windows and ultimately the output file <code>&prefix._expand</code>.
</p>
<p>
<code>%EVENT_STATS</code> computes abnormal returns, and then the summary
statistics. The output file <code>&prefix._car</code> contains the event-window
data with abnormal returns.
</p>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/EVENT_MACROS.sas"</span>;
<span style="color: #D9DAA2;">%EVENT_SETUP(</span>pref=,
crsp_lib=crspm,
frequency=d,
date_var=event_date,
id_var=permno,
<span style="color: #339CDB;">model</span>=ffm,
ret_var=retx,
est_per=<span style="color: #B5CEA8; font-weight: bold;">252</span>, gap_win=<span style="color: #B5CEA8; font-weight: bold;">30</span>,
beg_win=-<span style="color: #B5CEA8; font-weight: bold;">30</span>, end_win=<span style="color: #B5CEA8; font-weight: bold;">120</span>
);
<span style="color: #D9DAA2;">%EVENT_EXPAND(</span>lib=user, debug = n);
<span style="color: #D9DAA2;">%EVENT_STATS(</span>prefix = ,
dsetin = ,
group = ,
filter = <span style="color: #339CDB;">and</span> shrcd in (<span style="color: #B5CEA8; font-weight: bold;">10</span>,<span style="color: #B5CEA8; font-weight: bold;">11</span>) <span style="color: #339CDB;">and</span> exchcd in (<span style="color: #B5CEA8; font-weight: bold;">1</span>,<span style="color: #B5CEA8; font-weight: bold;">2</span>,<span style="color: #B5CEA8; font-weight: bold;">3</span>),
debug= = n
);
</pre>
</div>
</div>
</div>
<div id="outline-container-asof" class="outline-3">
<h3 id="asof"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/MERGE_ASOF.sas">MERGE_ASOF</a></h3>
<div class="outline-text-3" id="text-asof">
<p>
Does an as-of or “window” merge, e.g., COMPUSTAT yearly with CRSP
monthly, or TAQ quotes with TAQ trades.
</p>
<p>
See, e.g., <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge_asof.html">pandas</a>, <a href="https://www.r-bloggers.com/understanding-data-table-rolling-joins/">rolling joins with <code>R data.table</code></a>, or <a href="https://code.kx.com/v2/ref/asof/">kdb+ asof</a>.
</p>
<ul class="org-ul">
<li>a: dataset a
</li>
<li>b: dataset b
</li>
<li>merged: output merged dataset
</li>
<li>idvar: firm identifier (permno)
</li>
<li>datevar: date variable to use (date)
</li>
<li>sort_statement: defaults to <code>idvar datevar</code> but can be customized
</li>
<li>num_vars: numeric variables from b to merge in
</li>
<li>char_vars: character variables from b to merge in
</li>
</ul>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/MERGE_ASOF.sas"</span>;
<span style="color: #D9DAA2;">%MERGE_ASOF(</span>a=,b=,
merged=,
num_vars=);
</pre>
</div>
</div>
</div>
<div id="outline-container-parfor" class="outline-3">
<h3 id="parfor"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/PARFOR.sas">PARFOR</a></h3>
<div class="outline-text-3" id="text-parfor">
<p>
A parallel FOR loop SAS Macro.
</p>
<p>
If you have huge files it is often better to use Split-Apply-Combine
processing. For example processing daily trades by year can be done
by splitting the dataset into yearly datasets and doing the processing
in a parallel FOR loop.
</p>
<p>
This Macro spawns multiple SAS processes in the background to make
parallel processing easy. The Macro waits until all processes are
complete before returning control to the user.
</p>
<p>
<b><b>WARNING:</b></b> There is no built-in resource control (RAM/CPU) so make
sure to test your code on one group at a time before spawning too many
concurrent processes!
</p>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/PARFOR"</span>;
<span style="color: #339CDB;">%LET</span> FUNC = <span style="color: #D9DAA2;">%STR(</span>
<span style="color: #339CDB;">proc print</span> <span style="color: #339CDB;">data=</span>perf_&yyyy.(obs=<span style="color: #B5CEA8; font-weight: bold;">25</span>);
<span style="color: #339CDB;">var</span> exret: ret:;
<span style="color: #339CDB;">run;</span>
);
<span style="color: #D9DAA2;">%PARFOR(</span>FUNC=&FUNC.);
</pre>
</div>
</div>
</div>
<div id="outline-container-resample" class="outline-3">
<h3 id="resample"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/RESAMPLE.sas">RESAMPLE</a></h3>
<div class="outline-text-3" id="text-resample">
<p>
Resample and forward-fill data from low to high frequency Commonly
used to sample low frequency COMPUSTAT data before merging with higher
frequency CRSP data. It is more efficient to use <a href="https://github.com/edwinhu/sas/blob/master/MERGE_ASOF.sas">MERGE_ASOF</a> for this
specific task.
</p>
<ul class="org-ul">
<li>lib: input dataset library
</li>
<li>dsetin: input dataset
</li>
<li>dsetout: output (resampled) dataset
</li>
<li>datevar: date variable to resample
</li>
<li>idvar: group by id variable
</li>
<li>infreq: input frequency
</li>
<li>outfreq: output (higher) frequency
</li>
<li>alignment: date alignment (E,S,B)
</li>
<li>debug: keep or delete temporary datasets
</li>
</ul>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/RESAMPLE.sas"</span>;
<span style="color: #D9DAA2;">%RESAMPLE(</span>lib=sashelp, dsetin=citiyr, outfreq=monthly, idvar=, datevar=date);
</pre>
</div>
</div>
</div>
<div id="outline-container-roll" class="outline-3">
<h3 id="roll"><a id="sec-" name="sec-"></a><a href="https://github.com/edwinhu/sas/blob/master/ROLL_REG.sas">ROLL_REG</a></h3>
<div class="outline-text-3" id="text-roll">
<p>
Runs rolling regressions in a computationally efficient way, taking
advantage of SAS SSCP matrices.
</p>
<ul class="org-ul">
<li>dsetin: input dataset
</li>
<li>id: id variable
</li>
<li>date: date variable
</li>
<li>y: dependent variable
</li>
<li>x: independent variable
</li>
<li>ws: window size
</li>
<li>debug: debug mode (n)
</li>
</ul>
<div class="org-src-container">
<pre class="src src-sas"><span style="color: #339CDB;">%INCLUDE</span> <span style="color: #DB8E73;">"~/git/sas/ROLL_REG.sas"</span>;
<span style="color: #D9DAA2;">%ROLL_REG(</span>dsetin=,
<span style="color: #339CDB;">id</span>=permno,
date=date,
y=exret,
x=mktrf,
ws=<span style="color: #B5CEA8; font-weight: bold;">60</span>,
debug=n);
</pre>
</div>
</div>
</div>
</div>
</div><div class="col-md-3"><nav id="table-of-contents">
<div id="text-table-of-contents" class="bs-docs-sidebar">
<ul class="nav">
<li><a href="#crsp">CRSP</a>
<ul class="nav">
<li><a href="#cclink">CC_LINK</a></li>
</ul>
</li>
<li><a href="#comp">COMPUSTAT</a>
<ul class="nav">
<li><a href="#ead">COMP_EAD</a></li>
</ul>
</li>
<li><a href="#util">Utilities</a>
<ul class="nav">
<li><a href="#ff93">FF93_FACTORS</a></li>
<li><a href="#event">EVENT_MACROS</a></li>
<li><a href="#asof">MERGE_ASOF</a></li>
<li><a href="#parfor">PARFOR</a></li>
<li><a href="#resample">RESAMPLE</a></li>
<li><a href="#roll">ROLL_REG</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div></div></div>
<footer id="postamble" class="">
<div><p class="date">Date: 2020-04-27</p>
<p class="author">Author: Edwin Hu</p>
<p class="creator"></p>
</div>
</footer>
</body>
</html>