@sasjs/core 3.11.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,596 @@
1
+ /**
2
+ @file
3
+ @brief Prepares an audit table for stacking (re-applying) the changes.
4
+ @details WORK IN PROGRESS!!
5
+
6
+ When the underlying data from a Base Table is refreshed, it can be helpful
7
+ to have any previously-applied changes, re-applied.
8
+
9
+ Such situation might arise if you are applying those changes using a tool
10
+ like [Data Controller for SAS®](https://datacontroller.io) - which records
11
+ all such changes in an audit table.
12
+ It may also apply if you are preparing a series of specific cell-level
13
+ transactions, that you would like to apply to multiple sets of (similarly
14
+ structured) Base Tables.
15
+
16
+ In both cases, it is necessary that the transactions are stored using
17
+ the mp_storediffs.sas macro, or at least that the underlying table is
18
+ structured as per the definition in mp_coretable.sas (DIFFTABLE entry)
19
+
20
+ <b>This</b> macro is used to convert the stored changes (tall format) into
21
+ staged changes (wide format), with base table values incorporated (in the
22
+ case of modified rows), ready for the subsequent load process.
23
+
24
+ Essentially then, what this macro does, is turn a table like this:
25
+
26
+ |MOVE_TYPE:$1.|TGTVAR_NM:$32.|IS_PK:best.|IS_DIFF:best.|TGTVAR_TYPE:$1.|OLDVAL_NUM:best32.|NEWVAL_NUM:best32.|OLDVAL_CHAR:$32765.|NEWVAL_CHAR:$32765.|
27
+ |---|---|---|---|---|---|---|---|---|
28
+ |`A `|`NAME `|`1 `|`-1 `|`C `|`. `|`. `|` `|`Newbie `|
29
+ |`A `|`AGE `|`0 `|`-1 `|`N `|`. `|`13 `|` `|` `|
30
+ |`A `|`HEIGHT `|`0 `|`-1 `|`N `|`. `|`65.3 `|` `|` `|
31
+ |`A `|`SEX `|`0 `|`-1 `|`C `|`. `|`. `|` `|`F `|
32
+ |`A `|`WEIGHT `|`0 `|`-1 `|`N `|`. `|`98 `|` `|` `|
33
+ |`D `|`NAME `|`1 `|`-1 `|`C `|`. `|`. `|`Alfred `|` `|
34
+ |`D `|`AGE `|`0 `|`-1 `|`N `|`14 `|`. `|` `|` `|
35
+ |`D `|`HEIGHT `|`0 `|`-1 `|`N `|`69 `|`. `|` `|` `|
36
+ |`D `|`SEX `|`0 `|`-1 `|`C `|`. `|`. `|`M `|` `|
37
+ |`D `|`WEIGHT `|`0 `|`-1 `|`N `|`112.5 `|`. `|` `|` `|
38
+ |`M `|`NAME `|`1 `|`0 `|`C `|`. `|`. `|`Alice `|`Alice `|
39
+ |`M `|`AGE `|`0 `|`1 `|`N `|`13 `|`99 `|` `|` `|
40
+ |`M `|`HEIGHT `|`0 `|`0 `|`N `|`56.5 `|`56.5 `|` `|` `|
41
+ |`M `|`SEX `|`0 `|`0 `|`C `|`. `|`. `|`F `|`F `|
42
+ |`M `|`WEIGHT `|`0 `|`0 `|`N `|`84 `|`84 `|` `|` `|
43
+
44
+ Into three tables like this:
45
+
46
+ <b> `work.outmod`: </b>
47
+ |NAME:$8.|SEX:$1.|AGE:best.|HEIGHT:best.|WEIGHT:best.|
48
+ |---|---|---|---|---|
49
+ |`Alice `|`F `|`99 `|`56.5 `|`84 `|
50
+
51
+ <b> `work.outadd`: </b>
52
+ |NAME:$8.|SEX:$1.|AGE:best.|HEIGHT:best.|WEIGHT:best.|
53
+ |---|---|---|---|---|
54
+ |`Newbie `|`F `|`13 `|`65.3 `|`98 `|
55
+
56
+ <b> `work.outdel`: </b>
57
+ |NAME:$8.|SEX:$1.|AGE:best.|HEIGHT:best.|WEIGHT:best.|
58
+ |---|---|---|---|---|
59
+ |`Alfred `|`M `|`14 `|`69 `|`112.5 `|
60
+
61
+ As you might expect, there are a bunch of extra features and checks.
62
+
63
+ The macro supports both SCD2 (TXTEMPORAL) and UPDATE loadtypes. If the
64
+ base table contains a PROCESSED_DTTM column (or similar), this can be
65
+ ignored by declaring it in the `processed_dttm_var` parameter.
66
+
67
+ The macro is also flexible where columns have been added or removed from
68
+ the base table UNLESS there is a change to the primary key.
69
+
70
+ Changes to the primary key are NOT supported, and are likely to cause
71
+ unexpected results.
72
+
73
+ The following pre-flight checks are made:
74
+
75
+ @li All primary key columns exist on the base table
76
+ @li There is no change in variable TYPE for any of the columns
77
+ @li There is no reduction in variable LENGTH below the max-length of the
78
+ supplied values
79
+
80
+ Rules for stacking changes are as follows:
81
+
82
+ <table>
83
+ <tr>
84
+ <th>Transaction Type</th><th>Key Behaviour</th><th>Column Behaviour</th>
85
+ </tr>
86
+ <tr>
87
+ <td>Deletes</td>
88
+ <td>
89
+ The row is added to `&outDEL.` UNLESS it no longer exists
90
+ in the base table, in which case it is added to `&errDS.` instead.
91
+ </td>
92
+ <td>
93
+ Deletes are unaffected by the addition or removal of non Primary-Key
94
+ columns.
95
+ </td>
96
+ </tr>
97
+ <tr>
98
+ <td>Inserts</td>
99
+ <td>
100
+ Previously newly added rows are added to the `outADD` table UNLESS they
101
+ are present in the Base table.<br>In this case they are added to the
102
+ `&errDS.` table instead.
103
+ </td>
104
+ <td>
105
+ Inserts are unaffected by the addition of columns in the Base Table
106
+ (they are padded with blanks). Deleted columns are only a problem if
107
+ they appear on the previous insert - in which case the record is added
108
+ to `&errDS.`.
109
+ </td>
110
+ </tr>
111
+ <tr>
112
+ <td>Updates</td>
113
+ <td>
114
+ Previously modified rows are merged with base table values such that
115
+ only the individual cells that were _previously_ changed are re-applied.
116
+ Where the row contains cells that were not marked as having changed in
117
+ the prior transaction, the 'blanks' are filled with base table values in
118
+ the `outMOD` table.<br>
119
+ If the row no longer exists on the base table, then the row is added to
120
+ the `errDS` table instead.
121
+ </td>
122
+ <td>
123
+ Updates are unaffected by the addition of columns in the Base Table -
124
+ the new cells are simply populated with Base Table values. Deleted
125
+ columns are only a problem if they relate to a modified cell
126
+ (`is_diff=1`) - in which case the record is added to `&errDS.`.
127
+ </td>
128
+ </tr>
129
+ </table>
130
+
131
+ To illustrate the above with a diagram:
132
+
133
+ @dot
134
+ digraph {
135
+ rankdir="TB"
136
+ start[label="Transaction Type?" shape=Mdiamond]
137
+ del[label="Does Base Row exist?" shape=rectangle]
138
+ add [label="Does Base Row exist?" shape=rectangle]
139
+ mod [label="Does Base Row exist?" shape=rectangle]
140
+ chkmod [label="Do all modified\n(is_diff=1) cells exist?" shape=rectangle]
141
+ chkadd [label="Do all inserted cells exist?" shape=rectangle]
142
+ outmod [label="outMOD\nTable" shape=Msquare style=filled]
143
+ outadd [label="outADD\nTable" shape=Msquare style=filled]
144
+ outdel [label="outDEL\nTable" shape=Msquare style=filled]
145
+ outerr [label="ErrDS Table" shape=Msquare fillcolor=Orange style=filled]
146
+ start -> del [label="Delete"]
147
+ start -> add [label="Insert"]
148
+ start -> mod [label="Update"]
149
+
150
+ del -> outdel [label="Yes"]
151
+ del -> outerr [label="No" color="Red" fontcolor="Red"]
152
+ add -> chkadd [label="No"]
153
+ add -> outerr [label="Yes" color="Red" fontcolor="Red"]
154
+ mod -> outerr [label="No" color="Red" fontcolor="Red"]
155
+ mod -> chkmod [label="Yes"]
156
+ chkmod -> outerr [label="No" color="Red" fontcolor="Red"]
157
+ chkmod -> outmod [label="Yes"]
158
+ chkadd -> outerr [label="No" color="Red" fontcolor="Red"]
159
+ chkadd -> outadd [label="Yes"]
160
+
161
+ }
162
+ @enddot
163
+
164
+ For examples of usage, check out the mp_stackdiffs.test.sas program.
165
+
166
+
167
+ @param [in] baselibds Base Table against which the changes will be applied,
168
+ in libref.dataset format.
169
+ @param [in] auditlibds Dataset with previously applied transactions, to be
170
+ re-applied. Use libref.dataset format.
171
+ DDL as follows: %mp_coretable(DIFFTABLE)
172
+ @param [in] key Space seperated list of key variables
173
+ @param [in] mdebug= Set to 1 to enable DEBUG messages and preserve outputs
174
+ @param [in] processed_dttm_var= (0) If a variable is being used to mark
175
+ the processed datetime, put the name of the variable here. It will NOT
176
+ be included in the staged dataset (the load process is expected to
177
+ provide this)
178
+ @param [out] errds= (work.errds) Output table containing problematic records.
179
+ The columns of this table are:
180
+ @li PK_VARS - Space separated list of primary key variable names
181
+ @li PK_VALS - Slash separted list of PK variable values
182
+ @li ERR_MSG - Explanation of why this record is problematic
183
+ @param [out] outmod= (work.outmod) Output table containing modified records
184
+ @param [out] outadd= (work.outadd) Output table containing additional records
185
+ @param [out] outdel= (work.outdel) Output table containing deleted records
186
+
187
+
188
+ <h4> SAS Macros </h4>
189
+ @li mf_existvarlist.sas
190
+ @li mf_getquotedstr.sas
191
+ @li mf_getuniquefileref.sas
192
+ @li mf_getuniquename.sas
193
+ @li mf_islibds.sas
194
+ @li mf_nobs.sas
195
+ @li mf_wordsinstr1butnotstr2.sas
196
+ @li mp_abort.sas
197
+ @li mp_ds2squeeze.sas
198
+
199
+
200
+ <h4> Related Macros </h4>
201
+ @li mp_coretable.sas
202
+ @li mp_stackdiffs.test.sas
203
+ @li mp_storediffs.sas
204
+
205
+ @todo The current approach assumes that a variable called KEY_HASH is not on
206
+ the base table. This part will need to be refactored (eg using
207
+ mf_getuniquename.sas) when such a use case arises.
208
+
209
+ @version 9.2
210
+ @author Allan Bowe
211
+ **/
212
+ /** @cond */
213
+
214
+ %macro mp_stackdiffs(baselibds
215
+ ,auditlibds
216
+ ,key
217
+ ,mdebug=0
218
+ ,processed_dttm_var=0
219
+ ,errds=work.errds
220
+ ,outmod=work.outmod
221
+ ,outadd=work.outadd
222
+ ,outdel=work.outdel
223
+ )/*/STORE SOURCE*/;
224
+ %local dbg;
225
+ %if &mdebug=1 %then %do;
226
+ %put &sysmacroname entry vars:;
227
+ %put _local_;
228
+ %end;
229
+ %else %let dbg=*;
230
+
231
+ /* input parameter validations */
232
+ %mp_abort(iftrue= (%mf_islibds(&baselibds) ne 1)
233
+ ,mac=&sysmacroname
234
+ ,msg=%str(Invalid baselibds: &baselibds)
235
+ )
236
+ %mp_abort(iftrue= (%mf_islibds(&auditlibds) ne 1)
237
+ ,mac=&sysmacroname
238
+ ,msg=%str(Invalid auditlibds: &auditlibds)
239
+ )
240
+ %mp_abort(iftrue= (%length(&key)=0)
241
+ ,mac=&sysmacroname
242
+ ,msg=%str(Missing key variables!)
243
+ )
244
+ %mp_abort(iftrue= (
245
+ %mf_existVarList(&auditlibds,LIBREF DSN MOVE_TYPE KEY_HASH TGTVAR_NM IS_PK
246
+ IS_DIFF TGTVAR_TYPE OLDVAL_NUM NEWVAL_NUM OLDVAL_CHAR NEWVAL_CHAR)=0
247
+ )
248
+ ,mac=&sysmacroname
249
+ ,msg=%str(Input &auditlibds is missing required columns!)
250
+ )
251
+
252
+
253
+ /* set up macro vars */
254
+ %local prefix dslist x var keyjoin commakey keepvars missvars fref;
255
+ %let prefix=%substr(%mf_getuniquename(),1,25);
256
+ %let dslist=ds1d ds2d ds3d ds1a ds2a ds3a ds1m ds2m ds3m pks dups base
257
+ delrec delerr addrec adderr modrec moderr;
258
+ %do x=1 %to %sysfunc(countw(&dslist));
259
+ %let var=%scan(&dslist,&x);
260
+ %local &var;
261
+ %let &var=%upcase(&prefix._&var);
262
+ %end;
263
+
264
+ %let key=%upcase(&key);
265
+ %let commakey=%mf_getquotedstr(&key,quote=N);
266
+
267
+ %let keyjoin=1=1;
268
+ %do x=1 %to %sysfunc(countw(&key));
269
+ %let var=%scan(&key,&x);
270
+ %let keyjoin=&keyjoin and a.&var=b.&var;
271
+ %end;
272
+
273
+ data &errds;
274
+ length pk_vars $256 pk_vals $4098 err_msg $512;
275
+ call missing (of _all_);
276
+ stop;
277
+ run;
278
+
279
+ /**
280
+ * Prepare raw DELETE table
281
+ * Records are in the OLDVAL_xxx columns
282
+ */
283
+ %let keepvars=MOVE_TYPE KEY_HASH TGTVAR_NM TGTVAR_TYPE IS_PK
284
+ OLDVAL_NUM OLDVAL_CHAR
285
+ NEWVAL_NUM NEWVAL_CHAR;
286
+ proc sort data=&auditlibds(where=(move_type='D') keep=&keepvars)
287
+ out=&ds1d(drop=move_type);
288
+ by KEY_HASH TGTVAR_NM;
289
+ run;
290
+ proc transpose data=&ds1d(where=(tgtvar_type='N'))
291
+ out=&ds2d(drop=_name_);
292
+ by KEY_HASH;
293
+ id TGTVAR_NM;
294
+ var OLDVAL_NUM;
295
+ run;
296
+ proc transpose data=&ds1d(where=(tgtvar_type='C'))
297
+ out=&ds3d(drop=_name_);
298
+ by KEY_HASH;
299
+ id TGTVAR_NM;
300
+ var OLDVAL_CHAR;
301
+ run;
302
+ %mp_ds2squeeze(&ds2d,outds=&ds2d)
303
+ %mp_ds2squeeze(&ds3d,outds=&ds3d)
304
+ data &outdel;
305
+ if 0 then set &baselibds;
306
+ set &ds2d;
307
+ set &ds3d;
308
+ drop key_hash;
309
+ if not missing(%scan(&key,1));
310
+ run;
311
+ proc sort;
312
+ by &key;
313
+ run;
314
+
315
+ /**
316
+ * Prepare raw APPEND table
317
+ * Records are in the NEWVAL_xxx columns
318
+ */
319
+ proc sort data=&auditlibds(where=(move_type='A') keep=&keepvars)
320
+ out=&ds1a(drop=move_type);
321
+ by KEY_HASH TGTVAR_NM;
322
+ run;
323
+ proc transpose data=&ds1a(where=(tgtvar_type='N'))
324
+ out=&ds2a(drop=_name_);
325
+ by KEY_HASH;
326
+ id TGTVAR_NM;
327
+ var NEWVAL_NUM;
328
+ run;
329
+ proc transpose data=&ds1a(where=(tgtvar_type='C'))
330
+ out=&ds3a(drop=_name_);
331
+ by KEY_HASH;
332
+ id TGTVAR_NM;
333
+ var NEWVAL_CHAR;
334
+ run;
335
+ %mp_ds2squeeze(&ds2a,outds=&ds2a)
336
+ %mp_ds2squeeze(&ds3a,outds=&ds3a)
337
+ data &outadd;
338
+ if 0 then set &baselibds;
339
+ set &ds2a;
340
+ set &ds3a;
341
+ drop key_hash;
342
+ if not missing(%scan(&key,1));
343
+ run;
344
+ proc sort;
345
+ by &key;
346
+ run;
347
+
348
+ /**
349
+ * Prepare raw MODIFY table
350
+ * Keep only primary key - will add modified values later
351
+ */
352
+ proc sort data=&auditlibds(
353
+ where=(move_type='M' and is_pk=1) keep=&keepvars
354
+ ) out=&ds1m(drop=move_type);
355
+ by KEY_HASH TGTVAR_NM;
356
+ run;
357
+ proc transpose data=&ds1m(where=(tgtvar_type='N'))
358
+ out=&ds2m(drop=_name_);
359
+ by KEY_HASH ;
360
+ id TGTVAR_NM;
361
+ var NEWVAL_NUM;
362
+ run;
363
+ proc transpose data=&ds1m(where=(tgtvar_type='C'))
364
+ out=&ds3m(drop=_name_);
365
+ by KEY_HASH;
366
+ id TGTVAR_NM;
367
+ var NEWVAL_CHAR;
368
+ run;
369
+ %mp_ds2squeeze(&ds2m,outds=&ds2m)
370
+ %mp_ds2squeeze(&ds3m,outds=&ds3m)
371
+ data &outmod;
372
+ if 0 then set &baselibds;
373
+ set &ds2m;
374
+ set &ds3m;
375
+ if not missing(%scan(&key,1));
376
+ run;
377
+ proc sort;
378
+ by &key;
379
+ run;
380
+
381
+ /**
382
+ * Extract matching records from the base table
383
+ * Do this in one join for efficiency.
384
+ * At a later date, this should be optimised for large database tables by using
385
+ * passthrough and a temporary table.
386
+ */
387
+ data &pks;
388
+ if 0 then set &baselibds;
389
+ set &outadd &outmod &outdel;
390
+ keep &key;
391
+ run;
392
+
393
+ proc sort noduprec dupout=&dups;
394
+ by &key;
395
+ run;
396
+ data _null_;
397
+ set &dups;
398
+ putlog (_all_)(=);
399
+ run;
400
+ %mp_abort(iftrue= (%mf_nobs(&dups) ne 0)
401
+ ,mac=&sysmacroname
402
+ ,msg=%str(duplicates (%mf_nobs(&dups)) found on &auditlibds!)
403
+ )
404
+
405
+ proc sql;
406
+ create table &base as
407
+ select a.*
408
+ from &baselibds a, &pks b
409
+ where &keyjoin;
410
+
411
+ /**
412
+ * delete check
413
+ * This is straightforward as it relates to records only
414
+ */
415
+ proc sql;
416
+ create table &delrec as
417
+ select a.*
418
+ from &outdel a
419
+ left join &base b
420
+ on &keyjoin
421
+ where b.%scan(&key,1) is null
422
+ order by &commakey;
423
+
424
+ data &delerr;
425
+ if 0 then set &errds;
426
+ set &delrec;
427
+ PK_VARS="&key";
428
+ PK_VALS=catx('/',&commakey);
429
+ ERR_MSG="Rows cannot be deleted as they do not exist on the Base dataset";
430
+ keep PK_VARS PK_VALS ERR_MSG;
431
+ run;
432
+ proc append base=&errds data=&delerr;
433
+ run;
434
+
435
+ data &outdel;
436
+ merge &outdel (in=a) &delrec (in=b);
437
+ by &key;
438
+ if not b;
439
+ run;
440
+
441
+ /**
442
+ * add check
443
+ * Problems - where record already exists, or base table has columns missing
444
+ */
445
+ %let missvars=%mf_wordsinstr1butnotstr2(
446
+ Str1=%upcase(%mf_getvarlist(&outadd)),
447
+ Str2=%upcase(%mf_getvarlist(&baselibds))
448
+ );
449
+ %if %length(&missvars)>0 %then %do;
450
+ /* add them to the err table */
451
+ data &adderr;
452
+ if 0 then set &errds;
453
+ set &outadd;
454
+ PK_VARS="&key";
455
+ PK_VALS=catx('/',&commakey);
456
+ ERR_MSG="Rows cannot be added due to missing base vars: &missvars";
457
+ keep PK_VARS PK_VALS ERR_MSG;
458
+ run;
459
+ proc append base=&errds data=&adderr;
460
+ run;
461
+ proc sql;
462
+ delete * from &outadd;
463
+ %end;
464
+ %else %do;
465
+ proc sql;
466
+ /* find records that already exist on base table */
467
+ create table &addrec as
468
+ select a.*
469
+ from &outadd a
470
+ inner join &base b
471
+ on &keyjoin
472
+ order by &commakey;
473
+
474
+ /* add them to the err table */
475
+ data &adderr;
476
+ if 0 then set &errds;
477
+ set &addrec;
478
+ PK_VARS="&key";
479
+ PK_VALS=catx('/',&commakey);
480
+ ERR_MSG="Rows cannot be added as they already exist on the Base dataset";
481
+ keep PK_VARS PK_VALS ERR_MSG;
482
+ run;
483
+ proc append base=&errds data=&adderr;
484
+ run;
485
+
486
+ /* remove invalid rows from the outadd table */
487
+ data &outadd;
488
+ merge &outadd (in=a) &addrec (in=b);
489
+ by &key;
490
+ if not b;
491
+ run;
492
+ %end;
493
+
494
+ /**
495
+ * mod check
496
+ * Problems - where record does not exist or baseds has modified cols missing
497
+ */
498
+ proc sql noprint;
499
+ select distinct tgtvar_nm into: missvars separated by ' '
500
+ from &auditlibds
501
+ where move_type='M' and is_diff=1;
502
+ %let missvars=%mf_wordsinstr1butnotstr2(
503
+ Str1=&missvars,
504
+ Str2=%upcase(%mf_getvarlist(&baselibds))
505
+ );
506
+ %if %length(&missvars)>0 %then %do;
507
+ /* add them to the err table */
508
+ data &moderr;
509
+ if 0 then set &errds;
510
+ set &outmod;
511
+ PK_VARS="&key";
512
+ PK_VALS=catx('/',&commakey);
513
+ ERR_MSG="Rows cannot be modified due to missing base vars: &missvars";
514
+ keep PK_VARS PK_VALS ERR_MSG;
515
+ run;
516
+ proc append base=&errds data=&moderr;
517
+ run;
518
+ proc sql;
519
+ delete * from &outmod;
520
+ %end;
521
+ %else %do;
522
+ /* now check for records that do not exist (therefore cannot be modified) */
523
+ proc sql;
524
+ create table &modrec as
525
+ select a.*
526
+ from &outmod a
527
+ left join &base b
528
+ on &keyjoin
529
+ where b.%scan(&key,1) is null
530
+ order by &commakey;
531
+ data &moderr;
532
+ if 0 then set &errds;
533
+ set &modrec;
534
+ PK_VARS="&key";
535
+ PK_VALS=catx('/',&commakey);
536
+ ERR_MSG="Rows cannot be modified as they do not exist on the Base dataset";
537
+ keep PK_VARS PK_VALS ERR_MSG;
538
+ run;
539
+ proc append base=&errds data=&moderr;
540
+ run;
541
+ /* delete the above records from the outmod table */
542
+ data &outmod;
543
+ merge &outmod (in=a) &modrec (in=b);
544
+ by &key;
545
+ if not b;
546
+ run;
547
+ /* now - we can prepare the final MOD table (which is currently PK only) */
548
+ proc sql undo_policy=none;
549
+ create table &outmod as
550
+ select a.key_hash
551
+ ,b.*
552
+ from &outmod a
553
+ inner join &base b
554
+ on &keyjoin
555
+ order by &commakey;
556
+ /* now - to update outmod with modified (is_diff=1) values */
557
+ %let fref=%mf_getuniquefileref();
558
+ data _null_;
559
+ file &fref;
560
+ set &auditlibds(where=(move_type='M')) end=lastobs;
561
+ by key_hash;
562
+ retain comma 'N';
563
+ if _n_=1 then put 'proc sql;';
564
+ if first.key_hash then do;
565
+ comma='N';
566
+ put "update &outmod set " @@;
567
+ end;
568
+ if is_diff=1 then do;
569
+ if comma='N' then do;
570
+ put ' '@@;
571
+ comma='Y';
572
+ end;
573
+ else put ' ,'@@;
574
+ if tgtvar_type='C' then do;
575
+ length qstr $32767;
576
+ qstr=quote(trim(NEWVAL_CHAR));
577
+ put tgtvar_nm '=' qstr;
578
+ end;
579
+ else put tgtvar_nm '=' newval_num;
580
+ if comma=' ' then comma=' ,';
581
+ end;
582
+ if last.key_hash then put ' where key_hash=trim("' key_hash '");';
583
+ if lastobs then put "alter table &outmod drop key_hash;";
584
+ run;
585
+ %inc &fref/source2;
586
+ %end;
587
+
588
+ %if &mdebug=0 %then %do;
589
+ proc datasets lib=work;
590
+ delete &prefix:;
591
+ run;
592
+ %put &sysmacroname exit vars:;
593
+ %put _local_;
594
+ %end;
595
+ %mend mp_stackdiffs;
596
+ /** @endcond */
@@ -49,41 +49,23 @@
49
49
  @param [in] appds= (0) Dataset with appended records
50
50
  @param [in] modds= (0) Dataset with modified records
51
51
  @param [out] outds= (work.mp_storediffs) Output table containing stored data.
52
- Has the following format:
53
-
54
- proc sql;
55
- create table &outds(
56
- load_ref char(36) label='unique load reference',
57
- processed_dttm num format=E8601DT26.6 label='Processed at timestamp',
58
- libref char(8) label='Library Reference (8 chars)',
59
- dsn char(32) label='Dataset Name (32 chars)',
60
- key_hash char(32) label=
61
- 'MD5 Hash of primary key values (pipe seperated)',
62
- move_type char(1) label='Either (A)ppended, (D)eleted or (M)odified',
63
- is_pk num label='Is Primary Key Field? (1/0)',
64
- is_diff num label=
65
- 'Did value change? (1/0/-1). Always -1 for appends and deletes.',
66
- tgtvar_type char(1) label='Either (C)haracter or (N)umeric',
67
- tgtvar_nm char(32) label='Target variable name (32 chars)',
68
- oldval_num num format=best32. label='Old (numeric) value',
69
- newval_num num format=best32. label='New (numeric) value',
70
- oldval_char char(32765) label='Old (character) value',
71
- newval_char char(32765) label='New (character) value',
72
- constraint pk_mpe_audit
73
- primary key(load_ref,libref,dsn,key_hash,tgtvar_nm)
74
- );
75
-
76
- @param [in] processed_dttm= (0) Provide a datetime constant in relation to
77
- the actual load time. If not provided, current timestamp is used.
78
- @param [in] mdebug= set to 1 to enable DEBUG messages and preserve outputs
79
- @param [out] loadref= (0) Provide a unique key to reference the load,
80
- otherwise a UUID will be generated.
52
+ DDL as follows: %mp_coretable(DIFFTABLE)
53
+
54
+ @param [in] processed_dttm= (0) Provide a datetime constant in relation to
55
+ the actual load time. If not provided, current timestamp is used.
56
+ @param [in] mdebug= set to 1 to enable DEBUG messages and preserve outputs
57
+ @param [out] loadref= (0) Provide a unique key to reference the load,
58
+ otherwise a UUID will be generated.
81
59
 
82
60
  <h4> SAS Macros </h4>
83
61
  @li mf_getquotedstr.sas
84
62
  @li mf_getuniquename.sas
85
63
  @li mf_getvarlist.sas
86
64
 
65
+ <h4> Related Macros </h4>
66
+ @li mp_stackdiffs.sas
67
+ @li mp_storediffs.test.sas
68
+
87
69
  @version 9.2
88
70
  @author Allan Bowe
89
71
  **/
@@ -0,0 +1,44 @@
1
+ /**
2
+ @file
3
+ @brief Sets up the mcf_xx functions
4
+ @details
5
+ There is no (efficient) way to determine if an mcf_xx macro has already been
6
+ invoked. So, we make use of a global macro variable list to keep track.
7
+
8
+ Usage:
9
+
10
+ %mcf_init(MCF_LENGTH)
11
+
12
+ Returns:
13
+
14
+ > 1 (if already initialised) else 0
15
+
16
+ @param [in] func The function to be initialised
17
+
18
+ <h4> Related Macros </h4>
19
+ @li mcf_init.test.sas
20
+
21
+ **/
22
+
23
+ %macro mcf_init(func
24
+ )/*/STORE SOURCE*/;
25
+
26
+ %if not (%symexist(SASJS_PREFIX)) %then %do;
27
+ %global SASJS_PREFIX;
28
+ %let SASJS_PREFIX=SASJS;
29
+ %end;
30
+
31
+ %let func=%upcase(&func);
32
+
33
+ /* the / character is just a seperator */
34
+ %global &sasjs_prefix._FUNCTIONS;
35
+ %if %index(&&&sasjs_prefix._FUNCTIONS,&func/)>0 %then %do;
36
+ 1
37
+ %return;
38
+ %end;
39
+ %else %do;
40
+ %let &sasjs_prefix._FUNCTIONS=&&&sasjs_prefix._FUNCTIONS &func/;
41
+ 0
42
+ %end;
43
+
44
+ %mend mcf_init;