-
Notifications
You must be signed in to change notification settings - Fork 22
/
RESAMPLE.sas
87 lines (74 loc) · 2.39 KB
/
RESAMPLE.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
/*
Author: Edwin Hu
Date: 2013-05-24
# RESAMPLE #
## Summary ##
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.
## Variables ##
- lib: input dataset library
- dsetin: input dataset
- dsetout: output (resampled) dataset
- datevar: date variable to resample
- idvar: group by id variable
- infreq: input frequency
- outfreq: output (higher) frequency
- alignment: date alignment (E,S,B)
- debug: keep or delete temporary datasets
## Usage ##
```
%IMPORT "~/git/sas/RESAMPLE.sas";
%RESAMPLE(lib=sashelp, dsetin=citiyr, outfreq=monthly, idvar=, datevar=date);
```
*/
%macro RESAMPLE(lib = USER,
dsetin = &syslast.,
dsetout = ,
datevar = datadate,
idvar = gvkey,
infreq = yearly,
outfreq = monthly,
alignment = E,
debug = n
);
%if %length(&dsetout) < 1 %then %do;
%let dsetout = &dsetin._&outfreq. ;
%end;
%let infreq = "&infreq.";
%let outfreq = "&outfreq.";
%let alignment = "&alignment.";
proc sort data=&lib..&dsetin. out=&lib.._RESAMPLE_SORTED;
by &idvar. &datevar.;
proc printto log=junk;
proc expand data=&lib.._RESAMPLE_SORTED out=&lib.._RESAMPLE_DIFF(drop=TIME);
by &idvar.;
convert &datevar. = &datevar._next / transform = (lead 1);
proc printto;run;
data &lib.._RESAMPLE_DIFF;
set &lib.._RESAMPLE_DIFF;
&datevar._diff = min(
intck(&outfreq.,&datevar.,intnx(&infreq.,&datevar.,1,'S')),
intck(&outfreq.,&datevar.,intnx(&outfreq.,&datevar._next,0,'E'))
);
* Example: yearly -> monthly
* compute the number of months that this data remains valid;
label &datevar._next='Next date' &datevar._diff='Number of periods to next date';
run;
data &lib..&dsetout.;
retain &idvar. date;
set &lib.._RESAMPLE_DIFF;
do i = 1 to &datevar._diff;
date = intnx(&outfreq.,&datevar.,i,&alignment.);
output;
end;
format date yymmdd10.;
drop i &datevar._next &datevar._diff;
label date='Resampled date';
run;
%if %SUBSTR(%LOWCASE(&debug),1,1) = n %then %do;
proc datasets lib=&lib. nolist;
delete _RESAMPLE_: ;
run;quit;
%end;
%mend RESAMPLE;