@sasjs/core 3.5.0 → 3.6.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.
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  @file
3
3
  @brief Apply a set of formats to a table
4
- @details Applies a set of formats to one or more SAS datasets. Can be used
5
- to migrate formats from one table to another. The input table must contain
6
- the following columns:
4
+ @details Applies a set of formats to the metadata of one or more SAS datasets.
5
+ Can be used to migrate formats from one table to another. The input table
6
+ must contain the following columns:
7
7
 
8
8
  @li lib - the libref of the table to be updated
9
9
  @li ds - the dataset to be updated
@@ -5,19 +5,29 @@
5
5
  Only useful if every update uses the macro! Used heavily within
6
6
  [Data Controller for SAS](https://datacontroller.io).
7
7
 
8
- The underlying table is structured as per the MAKETABLE action.
9
-
10
8
  @param [in] action The action to be performed. Valid values:
11
9
  @li LOCK - Sets the lock flag, also confirms if a SAS lock is available
12
10
  @li UNLOCK - Unlocks the table
13
- @li MAKETABLE - creates the control table (ctl_ds)
14
11
  @param [in] lib= (WORK) The libref of the table to lock. Should already be
15
12
  assigned.
16
13
  @param [in] ds= The dataset to lock
17
14
  @param [in] ref= A meaningful reference to enable the lock to be traced. Max
18
15
  length is 200 characters.
19
16
  @param [out] ctl_ds= (0) The control table which controls the actual locking.
20
- Should already be assigned and available.
17
+ Should already be assigned and available. Definition as follows:
18
+
19
+ proc sql;
20
+ create table &ctl_ds(
21
+ lock_lib char(8),
22
+ lock_ds char(32),
23
+ lock_status_cd char(10) not null,
24
+ lock_user_nm char(100) not null ,
25
+ lock_ref char(200),
26
+ lock_pid char(10),
27
+ lock_start_dttm num format=E8601DT26.6,
28
+ lock_end_dttm num format=E8601DT26.6,
29
+ constraint pk_mp_lockanytable primary key(lock_lib,lock_ds));
30
+
21
31
  @param [in] loops= (25) Number of times to check for a lock.
22
32
  @param [in] loop_secs= (1) Seconds to wait between each lock attempt
23
33
 
@@ -221,19 +231,6 @@ run;
221
231
  %let abortme=1;
222
232
  %end;
223
233
  %end;
224
- %else %if &action=MAKETABLE %then %do;
225
- proc sql;
226
- create table &ctl_ds(
227
- lock_lib char(8),
228
- lock_ds char(32),
229
- lock_status_cd char(10) not null,
230
- lock_user_nm char(100) not null ,
231
- lock_ref char(200),
232
- lock_pid char(10),
233
- lock_start_dttm num format=E8601DT26.6,
234
- lock_end_dttm num format=E8601DT26.6,
235
- constraint pk_mp_lockanytable primary key(lock_lib,lock_ds));
236
- %end;
237
234
  %else %do;
238
235
  %let msg=lock_anytable given unsupported action (&action);
239
236
  %let abortme=1;
@@ -0,0 +1,248 @@
1
+ /**
2
+ @file
3
+ @brief Generate and apply retained key values to a staging table
4
+ @details This macro will populate a staging table with a Retained Key based on
5
+ a business key and a base (target) table.
6
+
7
+ Definition of retained key ([source](
8
+ http://bukhantsov.org/2012/04/what-is-data-vault/)):
9
+
10
+ > The retained key is a key which is mapped to business key one-to-one. In
11
+ > comparison, the surrogate key includes time and there can be many surrogate
12
+ > keys corresponding to one business key. This explains the name of the key,
13
+ > it is retained with insertion of a new version of a row while surrogate key
14
+ > is increasing.
15
+
16
+ This macro is designed to be used as part of a wider load / ETL process (such
17
+ as the one in [Data Controller for SAS](https://datacontroller.io)).
18
+
19
+ Specifically, the macro assumes that the base table has already been 'locked'
20
+ (eg with the mp_lockanytable.sas macro) prior to invocation. Also, several
21
+ tables are assumed to exist (names are configurable):
22
+
23
+ @li work.staging_table - the staged data, minus the retained key element
24
+ @li permlib.base_table - the target table to be loaded (**not** loaded by this
25
+ macro)
26
+ @li permlib.maxkeytable - optional, used to store load metaadata.
27
+ The structure is as follows:
28
+
29
+ proc sql;
30
+ create table yourlib.maxkeytable(
31
+ keytable varchar(41) label='Base table in libref.dataset format',
32
+ keycolumn char(32) format=$32.
33
+ label='The Retained key field containing the key values.',
34
+ max_key num label=
35
+ 'Integer representing current max RK or SK value in the KEYTABLE',
36
+ processed_dttm num format=E8601DT26.6
37
+ label='Datetime this value was last updated',
38
+ constraint pk_mpe_maxkeyvalues
39
+ primary key(keytable));
40
+
41
+ @param [in] base_lib= (WORK) Libref of the base (target) table.
42
+ @param [in] base_dsn= (BASETABLE) Name of the base (target) table.
43
+ @param [in] append_lib= (WORK) Libref of the staging table
44
+ @param [in] append_dsn= (APPENDTABLE) Name of the staging table
45
+ @param [in] retained_key= (DEFAULT_RK) Name of RK to generate (should exist on
46
+ base table)
47
+ @param [in] business_key= (PK1 PK2) Business key against which to generate
48
+ RK values. Should be unique and not null on the staging table.
49
+ @param [in] check_uniqueness=(NO) Set to yes to perform a uniqueness check.
50
+ Recommended if there is a chance that the staging data is not unique on the
51
+ business key.
52
+ @param [in] maxkeytable= (0) Provide a maxkeytable libds reference here, to
53
+ store load metadata (maxkey val, load time). Set to zero if metadata is not
54
+ required, eg, when preparing a 'dummy' load. Structure is described above.
55
+ See below for sample data.
56
+ |KEYTABLE:$32.|KEYCOLUMN:$32.|MAX_KEY:best.|PROCESSED_DTTM:E8601DT26.6|
57
+ |---|---|---|---|
58
+ |`DC487173.MPE_SELECTBOX `|`SELECTBOX_RK `|`55 `|`1950427787.8 `|
59
+ |`DC487173.MPE_FILTERANYTABLE `|`filter_rk `|`14 `|`1951053886.8 `|
60
+ @param [in] locktable= (0) If updating the maxkeytable, provide the libds
61
+ reference to the lock table (per mp_lockanytable.sas macro)
62
+ @param [in] filter_str= Apply a filter - useful for SCD2 or BITEMPORAL loads.
63
+ Example: `filter_str=%str( (where=( &now < &tech_to)) )`
64
+ @param [out] outds= (WORK.APPEND) Output table (staging table + retained key)
65
+
66
+ <h4> SAS Macros </h4>
67
+ @li mf_existvar.sas
68
+ @li mf_getquotedstr.sas
69
+ @li mf_getuniquename.sas
70
+ @li mf_nobs.sas
71
+ @li mp_abort.sas
72
+ @li mp_lockanytable.sas
73
+
74
+ <h4> Related Macros </h4>
75
+ @li mp_retainedkey.test.sas
76
+
77
+ @version 9.2
78
+
79
+ **/
80
+
81
+ %macro mp_retainedkey(
82
+ base_lib=WORK
83
+ ,base_dsn=BASETABLE
84
+ ,append_lib=WORK
85
+ ,append_dsn=APPENDTABLE
86
+ ,retained_key=DEFAULT_RK
87
+ ,business_key= PK1 PK2
88
+ ,check_uniqueness=NO
89
+ ,maxkeytable=0
90
+ ,locktable=0
91
+ ,outds=WORK.APPEND
92
+ ,filter_str=
93
+ );
94
+ %put &sysmacroname entry vars:;
95
+ %put _local_;
96
+
97
+ %local base_libds app_libds key_field check maxkey idx_pk newkey_cnt iserr
98
+ msg x tempds1 tempds2 comma_pk appnobs checknobs dropvar tempvar idx_val;
99
+ %let base_libds=%upcase(&base_lib..&base_dsn);
100
+ %let app_libds=%upcase(&append_lib..&append_dsn);
101
+ %let tempds1=%mf_getuniquename();
102
+ %let tempds2=%mf_getuniquename();
103
+ %let comma_pk=%mf_getquotedstr(in_str=%str(&business_key),dlm=%str(,),quote=);
104
+
105
+ /* validation checks */
106
+ %let iserr=0;
107
+ %if &syscc>0 %then %do;
108
+ %let iserr=1;
109
+ %let msg=%str(SYSCC=&syscc on macro entry);
110
+ %end;
111
+ %else %if %sysfunc(exist(&base_libds))=0 %then %do;
112
+ %let iserr=1;
113
+ %let msg=%str(Base LIBDS (&base_libds) expected but NOT FOUND);
114
+ %end;
115
+ %else %if %sysfunc(exist(&app_libds))=0 %then %do;
116
+ %let iserr=1;
117
+ %let msg=%str(Append LIBDS (&app_libds) expected but NOT FOUND);
118
+ %end;
119
+ %else %if &maxkeytable ne 0 and %sysfunc(exist(&maxkeytable))=0 %then %do;
120
+ %let iserr=1;
121
+ %let msg=%str(Maxkeytable (&maxkeytable) expected but NOT FOUND);
122
+ %end;
123
+ %else %if &maxkeytable ne 0 and %sysfunc(exist(&locktable))=0 %then %do;
124
+ %let iserr=1;
125
+ %let msg=%str(Locktable (&locktable) expected but NOT FOUND);
126
+ %end;
127
+ %else %if %length(&business_key)=0 %then %do;
128
+ %let iserr=1;
129
+ %let msg=%str(Business key (&business_key) expected but NOT FOUND);
130
+ %end;
131
+
132
+ %do x=1 %to %sysfunc(countw(&business_key));
133
+ /* check business key values exist */
134
+ %let key_field=%scan(&business_key,&x,%str( ));
135
+ %if (not %mf_existvar(&app_libds,&key_field))
136
+ or (not %mf_existvar(&base_libds,&key_field))
137
+ %then %do;
138
+ %let iserr=1;
139
+ %let msg=Business key (&key_field) not found!;
140
+ %end;
141
+ %end;
142
+
143
+ %if &iserr=1 %then %do;
144
+ /* err case so first perform an unlock of the base table before exiting */
145
+ %mp_lockanytable(
146
+ UNLOCK,lib=&base_lib,ds=&base_dsn,ref=%superq(msg),ctl_ds=&locktable
147
+ )
148
+ %end;
149
+ %mp_abort(iftrue=(&iserr=1),mac=mp_retainedkey,msg=%superq(msg))
150
+
151
+ proc sql noprint;
152
+ select sum(max(&retained_key),0) into: maxkey from &base_libds;
153
+
154
+ /**
155
+ * get base table RK and bus field values for lookup
156
+ */
157
+ proc sql noprint;
158
+ create table &tempds1 as
159
+ select distinct &comma_pk,&retained_key
160
+ from &base_libds &filter_str
161
+ order by &comma_pk,&retained_key;
162
+
163
+ %if &check_uniqueness=YES %then %do;
164
+ select count(*) into:checknobs
165
+ from (select distinct &comma_pk from &app_libds);
166
+ select count(*) into: appnobs from &app_libds; /* might be view */
167
+ %if &checknobs ne &appnobs %then %do;
168
+ %let msg=Source table &app_libds is not unique on (&business_key);
169
+ %let iserr=1;
170
+ %end;
171
+ %end;
172
+ %if &iserr=1 %then %do;
173
+ /* err case so first perform an unlock of the base table before exiting */
174
+ %mp_lockanytable(
175
+ UNLOCK,lib=&base_lib,ds=&base_dsn,ref=%superq(msg),ctl_ds=&locktable
176
+ )
177
+ %end;
178
+ %mp_abort(iftrue= (&iserr=1),mac=mp_retainedkey,msg=%superq(msg))
179
+
180
+ %if %mf_existvar(&app_libds,&retained_key)
181
+ %then %let dropvar=(drop=&retained_key);
182
+
183
+ /* prepare interim table with retained key populated for matching keys */
184
+ proc sql noprint;
185
+ create table &tempds2 as
186
+ select b.&retained_key, a.*
187
+ from &app_libds &dropvar a
188
+ left join &tempds1 b
189
+ on 1
190
+ %do idx_pk=1 %to %sysfunc(countw(&business_key));
191
+ %let idx_val=%scan(&business_key,&idx_pk);
192
+ and a.&idx_val=b.&idx_val
193
+ %end;
194
+ order by &retained_key;
195
+
196
+ /* identify the number of entries without retained keys (new records) */
197
+ select count(*) into: newkey_cnt
198
+ from &tempds2
199
+ where missing(&retained_key);
200
+ quit;
201
+
202
+ /**
203
+ * Update maxkey table if link provided
204
+ */
205
+ %if &maxkeytable ne 0 %then %do;
206
+ proc sql;
207
+ select count(*) into: check from &maxkeytable
208
+ where upcase(keytable)="&base_libds";
209
+
210
+ %mp_lockanytable(LOCK
211
+ ,lib=%scan(&maxkeytable,1,.)
212
+ ,ds=%scan(&maxkeytable,2,.)
213
+ ,ref=Updating maxkeyvalues with mp_retainedkey
214
+ ,ctl_ds=&locktable
215
+ )
216
+ proc sql;
217
+ %if &check=0 %then %do;
218
+ insert into &maxkeytable
219
+ set keytable="&base_libds"
220
+ ,keycolumn="&retained_key"
221
+ ,max_key=%eval(&maxkey+&newkey_cnt)
222
+ ,processed_dttm="%sysfunc(datetime(),E8601DT26.6)"dt;
223
+ %end;
224
+ %else %do;
225
+ update &maxkeytable
226
+ set max_key=%eval(&maxkey+&newkey_cnt)
227
+ ,processed_dttm="%sysfunc(datetime(),E8601DT26.6)"dt
228
+ where keytable="&base_libds";
229
+ %end;
230
+ %mp_lockanytable(UNLOCK
231
+ ,lib=%scan(&maxkeytable,1,.)
232
+ ,ds=%scan(&maxkeytable,2,.)
233
+ ,ref=Updating maxkeyvalues with maxkey=%eval(&maxkey+&newkey_cnt)
234
+ ,ctl_ds=&locktable
235
+ )
236
+ %end;
237
+
238
+ /* fill in the missing retained key values */
239
+ %let tempvar=%mf_getuniquename();
240
+ data &outds(drop=&tempvar);
241
+ retain &tempvar %eval(&maxkey+1);
242
+ set &tempds2;
243
+ if &retained_key =. then &retained_key=&tempvar;
244
+ &tempvar=&tempvar+1;
245
+ run;
246
+
247
+ %mend mp_retainedkey;
248
+
package/package.json CHANGED
@@ -38,5 +38,5 @@
38
38
  "dependencies": {
39
39
  "ts-loader": "^9.2.6"
40
40
  },
41
- "version": "3.5.0"
41
+ "version": "3.6.0"
42
42
  }