forked from rpardee/sas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sas
More file actions
366 lines (297 loc) · 7.77 KB
/
test.sas
File metadata and controls
366 lines (297 loc) · 7.77 KB
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
/* From RPardee sample */
options
linesize = 150
msglevel = i
formchar = '|-++++++++++=|-/|<>*'
dsoptions = note2err
nocenter
noovp
nosqlremerge
;
options orientation = landscape ;
data bobbity ;
run;
data s.gnu ;
set b.old
;
* message ;
* foobar; * foobar;
x1 = a * b ;
x2 = 8*n ;
x = 1.2**2-1;
x = 1.2**(2-1);
z = "&date."d;
/* foobar */
x = a *
b ;
run ;
%let out_folder = /C/Documents and Settings/pardre1/Application Data/Sublime Text 2/Packages/SAS/ ;
%let out_folder = C:\Documents and Settings/pardre1/Application Data/Sublime Text 2/Packages/SAS/test.sas ;
ods html path = "&out_folder" (URL=NONE)
body = "test.html"
(title = "test output")
;
proc sql outobs = 20 nowarn feedback noprint;
create table blah as
select *
from some.other_table
;
quit;
data bob;
set sashelp.class;
if height = 4 then do;
weight = 40;
end ;
if weight = 400 then do;
category = 'fatty';
end;
else do;
something;
end;
run;
data gnu;
set old;
run;
data work.blah;
set work.blah;
put _all_;
run;
%macro test(lib=,tableName=);
%let lib = %upcase(&lib);
%let tableName = %upcase(&tableName);
%if %sysfunc(exist(&lib..&tableName))
%then %do;
proc sql;
SELECT count(*) as N_Item
INTO : N_Item
FROM dictionary.columns
WHERE
libname = %upcase("&lib")
AND memname = %upcase("&tableName");
%let N_Item = %left(&N_Item);
SELECT ColumnName
INTO :ColName1 - :ColName&N_Item
FROM dictionary.columns
WHERE
libname = "&lib"
AND memname = "&tableName";
quit;
%do i = 1 %to &N_Item %by 1;
%let currItem = Blahblah&&ColName.&i;
%put &i: &currItem;
/* do something */
%end;
%end;
%else %do;
%put %str(E)RROR: &lib..&tableName table does not exist;
%end;
%mend test;
%test(lib=WORK,tableName=Testme);
proc sql feedback stimer;
connect to olebdb as DBRef
( user id= psw=);
SELECT *
FROM connection to DBRef (
SELECT a, b, count(*) as total
FROM cmq.dim_dlr
WHERE blah = x
GROUP BY blah
);
quit;
%TestMe(var=1);
%macro ETLSnippet(RptType=EXAMPLE2, SrcTable=work.Metric_Trans, dbmsAlias=MsSqlDB, inList=, exList=, TestMode=1, haltRun=1, UseCurrProcSql=1, ParamRC=);
/* For ETL, only do insert/update because this operates on the data source's
subset of table columns. Calling macro/process calls a separate macro to
delete inactive records
*/
%if &UseCurrProcSql = 0
%then %do;
proc sql;
%connectToDBMS(&dbmsAlias);
%end;
reset feedback exec noerrorstop noprint;
/* 1. Set macro-wide parameters: Status and column lists for input SrcTable
======================================================================================== */
/* Column lists must be space delimited */
%local locRC currMacroName dbmsTable upmExList;
%let locRC = -10;
%let currMacroName = %sysfunc(compbl(&RptType ETL Process));
%let dbmsTable = %sysfunc(strip(Report_&RptType.));
%let upmExList =;
%let RptType = %upcase(&RptType);
%if &TestMode
%then %do;
%put %str(N)OTE: Started &currMacroName;
%end;
%else %if &RptType = OVERALL
%then %do;
%let upmExList = Report_Date &exList;
%end;
%else %if &RptType = EXAMPLE2
%then %do;
%let upmExList = Group_Var_A Group_Var_B &exList;
%end;
%else %do;
%let locRC = 1;
%put %str(W)ARNING: Report Type not recognized in &currMacroName. Halting;
%goto EOM;
%end;
%if not %ObjectExists(&SrcTable,DATA)
%then %do;
%let locRC = 2;
%put %str(W)ARNING: Source table &SrcTable not found. Halting;
%goto EOM;
%end;
/* 2. Create temporary SQL Server table and populate with the input SrcTable
======================================================================================== */
execute (
SELECT
CAST(NULL AS CHAR(4)) AS Load_Type
,CAST(NULL AS INT) AS RptId
,%ListSqlColumns(columns=%bquote(&upmExList), delimiter=%str(, ))
/* ... In reality, use utility macros here and elsewhere to dynamically
generate the Metric name list*/
,Metric1
,Metric2
,Metric3
,MetricN
INTO ##&dbmsTable
FROM dbo.&dbmsTable src
WHERE 0 = 1
) by &dbmsAlias;
%if &sqlrc >= 8
%then %do;
%let locRC = &sqlrc;
%put %str(W)ARNING: %sysfunc(compbl(&currMacroName &locRC sqlRc at 2.Loading));
%goto EOM;
%end;
INSERT INTO TEMPDB."##&dbmsTable"N (
%ListSqlColumns(columns=%bquote(&upmExList), delimiter=%str(, ))
,Metric1
,Metric2
,Metric3
,MetricN
)
SELECT
%ListSqlColumns(columns=%bquote(&upmExList), delimiter=%str(, ))
,Metric1
,Metric2
,Metric3
,MetricN
FROM
&SrcTable;
%if &sqlrc >= 8
%then %do;
%let locRC = &sqlrc;
%let line = %sysfunc(compbl(&currMacroName &locRC sqlRc at 2.Inserts));
%goto EOM;
%end;
/* 3. Classify load type for SrcTable rows (Insert, Update)
======================================================================================== */
EXECUTE (
UPDATE a
SET
a.RptId = b.RptId
,a.Load_Type =
CASE
WHEN b.RptId IS NULL THEN 'I'
ELSE 'U'
END
FROM
##&dbmsTable a
LEFT OUTER JOIN dbo.&dbmsTable b
%if &RptType = OVERALL
%then %do;
ON a.Report_Date = b.Report_Date
%end;
%else %if &RptType = EXAMPLE2
%then %do;
ON a.Group_Var_A = b.Group_Var_A
AND a.Group_Var_B = b.Group_Var_B
%end;
) BY &dbmsAlias;
%if &sqlrc >= 8
%then %do;
%let locRC = &sqlrc;
%put %str(W)ARNING: %sysfunc(compbl(&currMacroName &locRC sqlRc at 3.Classify));
%goto EOM;
%end;
/* 4. Do load process for SrcTable metric columns
* Null out all existing values for metrics (&ColListLoad) in report table
* Insert any new rows to report table
* Update all rows that match in SrcTable and report table
======================================================================================== */
EXECUTE (
UPDATE dbo.&dbmsTable
SET
Metric1 = NULL
,Metric2 = NULL
,Metric3 = NULL
,MetricN = NULL
INSERT INTO dbo.&dbmsTable (
%ListSqlColumns(columns=%bquote(&upmExList), delimiter=%str(, ))
,Metric1
,Metric2
,Metric3
,MetricN
,CreatedDate
,UpdatedDate
)
SELECT
%ListSqlColumns(columns=%bquote(&upmExList), delimiter=%str(, ))
,Metric1
,Metric2
,Metric3
,MetricN
,GETDATE() AS CreatedDate
,GETDATE() AS UpdatedDate
FROM
##&dbmsTable a
WHERE
a.Load_Type = 'I'
UPDATE src
SET
src.Metric1 = upd.Metric1
,src.Metric2 = upd.Metric2
,src.Metric3 = upd.Metric3
,src.MetricN = upd.MetricN
,src.UpdatedDate = getdate()
FROM
dbo.&dbmsTable src
INNER JOIN ##&dbmsTable upd
ON upd.RptId = src.RptId
WHERE
upd.Load_Type IN ('U')
) BY &dbmsAlias;
%if &sqlrc >= 8
%then %do;
%let locRC = &sqlrc;
%put %str(W)ARNING: %sysfunc(compbl(&currMacroName &locRC sqlRc 4.ETL));
%goto EOM;
%end;
%else %do;
%let locRC = 0;
%end;
/* 5. Do end steps for this macro
======================================================================================== */
%EOM:
reset exec noerrorstop print;
%put * Reached EOM for &currMacroName with &locRC RC;
%if %bquote(&ParamRC) ^= %str()
%then %do;
%let &ParamRC = &locRC;
%if &TestMode
%then %do;
%put %str(N)OTE: %sysfunc(compbl(&currMacroName set &ParamRC to &locRC));
%end;
%end;
%if &TestMode
%then %do;
%put %str(I)NFO: %sysfunc(compbl(%bquote(Ended &currMacroName with
&locRC RC at %sysfunc(datetime(), mdyampm19.) )));
%end;
%if &UseCurrProcSql = 0
%then %do;
quit;
%end;
%mend;