-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUTILITIES.GENERAL.sas~
223 lines (176 loc) · 6.96 KB
/
UTILITIES.GENERAL.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
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
/*====================================================================================\
| UTILITIES |
| |
| sas -nodms -noterminal |
| |
| LOAD THE UTILITIES FILE ON THE SERVER: |
| |
| rsync -avR --progress --stats --human-readable |
| /Users/jankocizel/Documents/Dropbox/Projects/PhD\ |
| Thesis/SAS/* wrds-cloud:~/UTILITIES/ |
\====================================================================================*/
*rsync -avR --progress --stats --human-readable /Users/jankocizel/Documents/Dropbox/Projects/PhD\ Thesis/SAS/* wrds-cloud:~/UTILITIES/
options source nocenter ls=80 ps=max;
/*=========================================================================\
| PRODUCE A SUMMARY OF ALL DATASETS IN A LIBRARY |
\=========================================================================*/
%MACRO DOCLIST(LIBRARY);
/* DECLARE MACRO VARIABLES */
%LOCAL I NDATASET;
/* GET LIST OF DATASETS IN A LIBRARY */
ODS LISTING CLOSE;
ODS OUTPUT MEMBERS = DATASETLIST;
PROC DATASETS MT=DATA LIBRARY = &LIBRARY;
RUN;
QUIT;
ODS LISTING;
TITLE3 "DATASETS IN LIBRARY &LIBRARY";
PROC PRINT
DATA = DATASETLIST(DROP=MEMTYPE)
LABEL NOOBS;
FORMAT FILE_SIZE COMMA20.;
RUN;
/* GET TOTAL NUMBER OF DATASETS IN A MACRO VARIABLE */
DATA _NULL_;
IF 0 THEN SET DATASETLIST
NOBS = NOBS;
CALL SYMPUT('NDATASET',NOBS);
RUN;
/* DECLARE DATA SET MACRO VARIABLES LOCAL */
%DO I=1 %TO &NDATASET;
%LOCAL DATASET&I;
%END;
/* PUT DATA SET NAMES INTO MACRO VARIABLES */
DATA _NULL_;
SET DATASETLIST;
CALL SYMPUT
('DATASET'||LEFT(_N_),NAME);
RUN;
/* LOOP THROUGH EACH DATASET */
%DO I=1 %TO &NDATASET;
TITLE3 "STRUCTURE OF DATA SET &&DATASET&I";
PROC CONTENTS
DATA = &LIBRARY..&&DATASET&I
OUT = &&DATASET&I
/* DETAILS */
VARNUM;
RUN;
/* PROC EXPORT DBMS=CSV DATA=&&DATASET&I */
/* OUTFILE="/scratch/uvanl/LOOKUP.%trim(&&DATASET&I..).CSV" */
/* REPLACE; */
/* RUN; */
/* TITLE3 "PARTIAL LISTING OF DATA SET &DATASET&I"; */
/* PROC PRINT */
/* DATA = &LIBRARY..&&DATASET&I(OBS=5); */
/* RUN; */
%END;
/* CLEAN-UP */
TITLE3;
PROC DATASETS NOLIST LIBRARY = WORK;
DELETE DATASETLIST;
RUN;
QUIT;
%MEND;
/* /\*=============================================================================\ */
/* | TEST THE ABOVE MACRO | */
/* \=============================================================================*\/ */
/* LIBNAME CIQ "/wrds/capitaliq/sasdata/capstructure"; */
/* ODS PDF FILE = "/scratch/uvanl/CIQ.CAPITALSTRUCTURE.DESCR.pdf"; */
/* %DOCLIST(CIQ); */
/* ODS PDF CLOSE; */
/* ODS PDF FILE = "/scratch/uvanl/SASHELP.DESCR.pdf"; */
/* %DOCLIST(SASHELP); */
/* ODS PDF CLOSE; */
/*=========================================================================\
| EXPORT ALL DATASETS IN A LIBRARY |
\=========================================================================*/
%MACRO CONVERT2CSV(LIBNAME);
DATA MEMBERS;
SET SASHELP.VMEMBER(WHERE=(LIBNAME = "&LIBNAME"));
RETAIN OBS 0;
OBS = OBS+1;
KEEP MEMNAME OBS;
RUN;
PROC SQL;
SELECT MIN(OBS) INTO :MIN
FROM MEMBERS;
QUIT;
PROC SQL;
SELECT MAX(OBS) INTO :MAX
FROM MEMBERS;
QUIT;
%Local D;
%DO D = &MIN %TO &MAX;
PROC SQL;
SELECT COMPRESS(MEMNAME) INTO: TABLE
FROM MEMBERS
WHERE OBS=&D;
QUIT;
%LET TABLE = &TABLE;
PROC EXPORT DBMS=CSV DATA=&LIBNAME..&TABLE
OUTFILE="/scratch/uvanl/\&TABLE..CSV"
REPLACE;
RUN;
%END;
%MEND;
/*=============================================================================\
| EXPORT TO EXCEL |
\=============================================================================*/
%MACRO DOCLIST2EXCEL(LIBRARY,FILE);
/* DECLARE MACRO VARIABLES */
%LOCAL I NDATASET;
/* GET LIST OF DATASETS IN A LIBRARY */
ODS LISTING CLOSE;
ODS OUTPUT MEMBERS = DATASETLIST;
PROC DATASETS MT=DATA LIBRARY = &LIBRARY;
RUN;
QUIT;
ODS LISTING;
TITLE3 "DATASETS IN LIBRARY &LIBRARY";
PROC PRINT
DATA = DATASETLIST(DROP=MEMTYPE)
LABEL NOOBS;
FORMAT FILE_SIZE COMMA20.;
RUN;
/* GET TOTAL NUMBER OF DATASETS IN A MACRO VARIABLE */
DATA _NULL_;
IF 0 THEN SET DATASETLIST
NOBS = NOBS;
CALL SYMPUT('NDATASET',NOBS);
RUN;
/* DECLARE DATA SET MACRO VARIABLES LOCAL */
%DO I=1 %TO &NDATASET;
%LOCAL DATASET&I;
%END;
/* PUT DATA SET NAMES INTO MACRO VARIABLES */
DATA _NULL_;
SET DATASETLIST;
CALL SYMPUT
('DATASET'||LEFT(_N_),NAME);
RUN;
/* LOOP THROUGH EACH DATASET */
%DO I=1 %TO &NDATASET;
TITLE3 "STRUCTURE OF DATA SET &&DATASET&I";
PROC CONTENTS
DATA = &LIBRARY..&&DATASET&I
OUT = &&DATASET&I
/* DETAILS */
VARNUM;
RUN;
PROC EXPORT DBMS=XLS DATA=&&DATASET&I
OUTFILE=&FILE
REPLACE;
SHEET = %trim(&&DATASET&I..);
RUN;
/* TITLE3 "PARTIAL LISTING OF DATA SET &DATASET&I"; */
/* PROC PRINT */
/* DATA = &LIBRARY..&&DATASET&I(OBS=5); */
/* RUN; */
%END;
/* CLEAN-UP */
TITLE3;
PROC DATASETS NOLIST LIBRARY = WORK;
DELETE DATASETLIST;
RUN;
QUIT;
%MEND;