@sasjs/core 3.4.0 → 3.7.1

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
@@ -0,0 +1,92 @@
1
+ /**
2
+ @file
3
+ @brief Create the permanent Core tables
4
+ @details Several macros in the [core](https://github.com/sasjs/core) library
5
+ make use of permanent tables. To avoid duplication in definitions, this
6
+ macro provides a central location for managing the corresponding DDL.
7
+
8
+ Example usage:
9
+
10
+ %mp_coretable(LOCKTABLE,libds=work.locktable)
11
+
12
+ @param [in] table_ref The type of table to create. Example values:
13
+ @li FILTER_DETAIL - For storing detailed filter values. Used by
14
+ mp_filterstore.sas.
15
+ @li FILTER_SUMMARY - For storing summary filter values. Used by
16
+ mp_filterstore.sas.
17
+ @li LOCKANYTABLE - For "locking" tables prior to multipass loads. Used by
18
+ mp_lockanytable.sas
19
+ @li MAXKEYTABLE - For storing the maximum retained key information. Used
20
+ by mp_retainedkey.sas
21
+ @param [in] libds= (0) The library.dataset reference used to create the table.
22
+ If not provided, then the DDL is simply printed to the log.
23
+
24
+ <h4> Related Macros </h4>
25
+ @li mp_filterstore.sas
26
+ @li mp_lockanytable.sas
27
+ @li mp_retainedkey.sas
28
+
29
+ @version 9.2
30
+ @author Allan Bowe
31
+
32
+ **/
33
+
34
+ %macro mp_coretable(table_ref,libds=0
35
+ )/*/STORE SOURCE*/;
36
+ %local outds ;
37
+ %let outds=%sysfunc(ifc(&libds=0,_data_,&libds));
38
+ proc sql;
39
+ %if &table_ref=LOCKTABLE %then %do;
40
+ create table &outds(
41
+ lock_lib char(8),
42
+ lock_ds char(32),
43
+ lock_status_cd char(10) not null,
44
+ lock_user_nm char(100) not null ,
45
+ lock_ref char(200),
46
+ lock_pid char(10),
47
+ lock_start_dttm num format=E8601DT26.6,
48
+ lock_end_dttm num format=E8601DT26.6,
49
+ constraint pk_mp_lockanytable primary key(lock_lib,lock_ds));
50
+ %end;
51
+ %else %if &table_ref=FILTER_SUMMARY %then %do;
52
+ create table &outds(
53
+ filter_rk num not null,
54
+ filter_hash char(32) not null,
55
+ filter_table char(41) not null,
56
+ processed_dttm num not null format=E8601DT26.6,
57
+ constraint pk_mpe_filteranytable
58
+ primary key(filter_rk));
59
+ %end;
60
+ %else %if &table_ref=FILTER_DETAIL %then %do;
61
+ create table &outds(
62
+ filter_hash char(32) not null,
63
+ filter_line num not null,
64
+ group_logic char(3) not null,
65
+ subgroup_logic char(3) not null,
66
+ subgroup_id num not null,
67
+ variable_nm varchar(32) not null,
68
+ operator_nm varchar(12) not null,
69
+ raw_value varchar(4000) not null,
70
+ processed_dttm num not null format=E8601DT26.6,
71
+ constraint pk_mpe_filteranytable
72
+ primary key(filter_hash,filter_line));
73
+ %end;
74
+ %else %if &table_ref=MAXKEYTABLE %then %do;
75
+ create table &outds(
76
+ keytable varchar(41) label='Base table in libref.dataset format',
77
+ keycolumn char(32) format=$32.
78
+ label='The Retained key field containing the key values.',
79
+ max_key num label=
80
+ 'Integer representing current max RK or SK value in the KEYTABLE',
81
+ processed_dttm num format=E8601DT26.6
82
+ label='Datetime this value was last updated',
83
+ constraint pk_mpe_maxkeyvalues
84
+ primary key(keytable));
85
+ %end;
86
+
87
+
88
+ %if &libds=0 %then %do;
89
+ describe table &syslast;
90
+ drop table &syslast;
91
+ %end;
92
+ %mend mp_coretable;
@@ -0,0 +1,213 @@
1
+ /**
2
+ @file
3
+ @brief Checks & Stores an input filter table and returns the Filter Key
4
+ @details Used to generate a FILTER_RK from an input query dataset. This
5
+ process requires several permanent tables (names are configurable). The
6
+ benefit of storing query values at backend is to enable stored 'views' of
7
+ filtered tables at frontend (ie, when building [SAS-Powered Apps](
8
+ https://sasapps.io)). This macro is also used in [Data Controller for SAS](
9
+ https://datacontroller.io).
10
+
11
+
12
+ @param [in] libds= The target dataset to be filtered (lib should be assigned)
13
+ @param [in] queryds= (WORK.FILTERQUERY) The temporary input query dataset to
14
+ be validated. Has the following format:
15
+ |GROUP_LOGIC:$3|SUBGROUP_LOGIC:$3|SUBGROUP_ID:8.|VARIABLE_NM:$32|OPERATOR_NM:$10|RAW_VALUE:$32767|
16
+ |---|---|---|---|---|---|
17
+ |AND|AND|1|SOME_BESTNUM|>|1|
18
+ |AND|AND|1|SOME_TIME|=|77333|
19
+ @param [in] filter_summary= (PERM.FILTER_SUMMARY) Permanent table containing
20
+ summary filter values. The definition is available by running
21
+ mp_coretable.sas as follows: `mp_coretable(FILTER_SUMMARY)`. Example
22
+ values:
23
+ |FILTER_RK:best.|FILTER_HASH:$32.|FILTER_TABLE:$41.|PROCESSED_DTTM:datetime19.|
24
+ |---|---|---|---|
25
+ |`1 `|`540E96F566D194AB58DD4C413C99C9DB `|`VIYA6014.MPE_TABLES `|`1956084246 `|
26
+ |`2 `|`87737DB9EEE2650F5C89956CEAD0A14F `|`VIYA6014.MPE_X_TEST `|`1956084452.1`|
27
+ |`3 `|`8048BD908DBBD83D013560734E90D394 `|`VIYA6014.MPE_TABLES `|`1956093620.6`|
28
+ @param [in] filter_detail= (PERM.FILTER_DETAIL) Permanent table containing
29
+ detailed (raw) filter values. The definition is available by running
30
+ mp_coretable.sas as follows: `mp_coretable(FILTER_DETAIL)`. Example
31
+ values:
32
+ |FILTER_HASH:$32.|FILTER_LINE:best.|GROUP_LOGIC:$3.|SUBGROUP_LOGIC:$3.|SUBGROUP_ID:best.|VARIABLE_NM:$32.|OPERATOR_NM:$12.|RAW_VALUE:$4000.|PROCESSED_DTTM:datetime19.|
33
+ |---|---|---|---|---|---|---|---|---|
34
+ |`540E96F566D194AB58DD4C413C99C9DB `|`1 `|`AND `|`AND `|`1 `|`LIBREF `|`CONTAINS `|`DC`|`1956084245.8 `|
35
+ |`540E96F566D194AB58DD4C413C99C9DB `|`2 `|`AND `|`OR `|`2 `|`DSN `|`= `|` MPE_LOCK_ANYTABLE `|`1956084245.8 `|
36
+ |`87737DB9EEE2650F5C89956CEAD0A14F `|`1 `|`AND `|`AND `|`1 `|`PRIMARY_KEY_FIELD `|`IN `|`(1,2,3) `|`1956084451.9 `|
37
+ @param [in] lock_table= (PERM.LOCK_TABLE) Permanent locking table. Used to
38
+ manage concurrent access. The definition is available by running
39
+ mp_coretable.sas as follows: `mp_coretable(LOCKTABLE)`.
40
+ @param [in] maxkeytable= (0) Optional permanent reference table used for
41
+ retained key tracking. Described in mp_retainedkey.sas.
42
+ @param [in] mdebug= set to 1 to enable DEBUG messages
43
+ @param [out] outresult= The result table with the FILTER_RK
44
+ @param [out] outquery= The original query, taken as extract after table load
45
+
46
+
47
+ <h4> SAS Macros </h4>
48
+ @li mf_getuniquename.sas
49
+ @li mf_getvalue.sas
50
+ @li mf_islibds.sas
51
+ @li mf_nobs.sas
52
+ @li mp_abort.sas
53
+ @li mp_filtercheck.sas
54
+ @li mp_hashdataset.sas
55
+ @li mp_retainedkey.sas
56
+
57
+ <h4> Related Macros </h4>
58
+ @li mp_filtercheck.sas
59
+ @li mp_filtergenerate.sas
60
+ @li mp_filtervalidate.sas
61
+ @li mp_filterstore.test.sas
62
+
63
+ @version 9.2
64
+ @author [Allan Bowe](https://www.linkedin.com/in/allanbowe)
65
+
66
+ **/
67
+
68
+ %macro mp_filterstore(libds=,
69
+ queryds=work.filterquery,
70
+ filter_summary=PERM.FILTER_SUMMARY,
71
+ filter_detail=PERM.FILTER_DETAIL,
72
+ lock_table=PERM.LOCK_TABLE,
73
+ maxkeytable=PERM.MAXKEYTABLE,
74
+ outresult=work.result,
75
+ outquery=work.query,
76
+ mdebug=1
77
+ );
78
+ %put &sysmacroname entry vars:;
79
+ %put _local_;
80
+
81
+ %local ds1 ds2 ds3 ds4 filter_hash;
82
+ %mp_abort(iftrue= (&syscc ne 0)
83
+ ,mac=mp_filterstore
84
+ ,msg=%str(syscc=&syscc on macro entry)
85
+ )
86
+ %mp_abort(iftrue= (%mf_islibds(&filter_summary)=0)
87
+ ,mac=mp_filterstore
88
+ ,msg=%str(Invalid filter_summary value: &filter_summary)
89
+ )
90
+ %mp_abort(iftrue= (%mf_islibds(&filter_detail)=0)
91
+ ,mac=mp_filterstore
92
+ ,msg=%str(Invalid filter_detail value: &filter_detail)
93
+ )
94
+ %mp_abort(iftrue= (%mf_islibds(&lock_table)=0)
95
+ ,mac=mp_filterstore
96
+ ,msg=%str(Invalid lock_table value: &lock_table)
97
+ )
98
+
99
+ /* validate query */
100
+ %mp_filtercheck(&queryds,targetds=&libds,abort=YES)
101
+
102
+ /* hash the result */
103
+ %let ds1=%mf_getuniquename(prefix=hashds);
104
+ %mp_hashdataset(&queryds,outds=&ds1,salt=&libds)
105
+ %let filter_hash=%upcase(%mf_getvalue(&ds1,hashkey));
106
+ %if &mdebug=1 %then %do;
107
+ data _null_;
108
+ putlog "filter_hash=&filter_hash";
109
+ set &ds1;
110
+ putlog (_all_)(=);
111
+ run;
112
+ %end;
113
+
114
+ /* check if data already exists for this hash */
115
+ data &outresult;
116
+ set &filter_summary;
117
+ where filter_hash="&filter_hash";
118
+ run;
119
+
120
+ %mp_abort(iftrue= (&syscc ne 0)
121
+ ,mac=mp_filterstore
122
+ ,msg=%str(syscc=&syscc after hash check)
123
+ )
124
+ %mp_abort(iftrue= ("&filter_hash"=" ")
125
+ ,mac=mp_filterstore
126
+ ,msg=%str(problem with filter_hash generation)
127
+ )
128
+
129
+ %if %mf_nobs(&outresult)=0 %then %do;
130
+
131
+ /* update detail table first */
132
+ %let ds2=%mf_getuniquename(prefix=filterdetail);
133
+ data &ds2;
134
+ if 0 then set &filter_detail;
135
+ set &queryds;
136
+ format filter_hash $hex32. filter_line 8.;
137
+ filter_hash="&filter_hash";
138
+ filter_line=_n_;
139
+ PROCESSED_DTTM=%sysfunc(datetime());
140
+ run;
141
+ %mp_lockanytable(LOCK,
142
+ lib=%scan(&filter_detail,1,.)
143
+ ,ds=%scan(&filter_detail,2,.)
144
+ ,ref=MP_FILTERSTORE update - &filter_hash
145
+ ,ctl_ds=&lock_table
146
+ )
147
+ proc append base=&filter_detail data=&ds2;
148
+ run;
149
+
150
+ %mp_lockanytable(UNLOCK,
151
+ lib=%scan(&filter_detail,1,.)
152
+ ,ds=%scan(&filter_detail,2,.)
153
+ ,ref=MP_FILTERSTORE update - &filter_hash
154
+ ,ctl_ds=&lock_table
155
+ )
156
+
157
+ /* now update summary table */
158
+ %let ds3=%mf_getuniquename(prefix=filtersum);
159
+ data &ds3;
160
+ if 0 then set &filter_summary;
161
+ filter_table=symget('libds');
162
+ filter_hash="&filter_hash";
163
+ PROCESSED_DTTM=%sysfunc(datetime());
164
+ output;
165
+ stop;
166
+ run;
167
+
168
+ %mp_lockanytable(LOCK,
169
+ lib=%scan(&filter_summary,1,.)
170
+ ,ds=%scan(&filter_summary,2,.)
171
+ ,ref=MP_FILTERSTORE update - &filter_hash
172
+ ,ctl_ds=&lock_table
173
+ )
174
+
175
+ %let ds4=%mf_getuniquename(prefix=filtersumappend);
176
+ %mp_retainedkey(
177
+ base_lib=%scan(&filter_summary,1,.)
178
+ ,base_dsn=%scan(&filter_summary,2,.)
179
+ ,append_lib=work
180
+ ,append_dsn=&ds3
181
+ ,retained_key=filter_rk
182
+ ,business_key=filter_hash
183
+ ,maxkeytable=&maxkeytable
184
+ ,locktable=&lock_table
185
+ ,outds=work.&ds4
186
+ )
187
+ proc append base=&filter_summary data=&ds4;
188
+ run;
189
+
190
+ %mp_lockanytable(UNLOCK,
191
+ lib=%scan(&filter_summary,1,.)
192
+ ,ds=%scan(&filter_summary,2,.)
193
+ ,ref=MP_FILTERSTORE update - &filter_hash
194
+ ,ctl_ds=&lock_table
195
+ )
196
+
197
+ data &outresult;
198
+ set &filter_summary;
199
+ where filter_hash="&filter_hash";
200
+ run;
201
+
202
+ %end;
203
+
204
+ proc sort data=&filter_detail(where=(filter_hash="&filter_hash")) out=&outquery;
205
+ by filter_line;
206
+ run;
207
+
208
+ %mp_abort(iftrue= (&syscc ne 0)
209
+ ,mac=mp_filterstore
210
+ ,msg=%str(syscc=&syscc on macro exit)
211
+ )
212
+
213
+ %mend mp_filterstore;
@@ -5,19 +5,18 @@
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. The definition is available by
18
+ running mp_coretable.sas as follows: `mp_coretable(LOCKTABLE)`.
19
+
21
20
  @param [in] loops= (25) Number of times to check for a lock.
22
21
  @param [in] loop_secs= (1) Seconds to wait between each lock attempt
23
22
 
@@ -221,19 +220,6 @@ run;
221
220
  %let abortme=1;
222
221
  %end;
223
222
  %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
223
  %else %do;
238
224
  %let msg=lock_anytable given unsupported action (&action);
239
225
  %let abortme=1;
@@ -0,0 +1,246 @@
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 definition is available by running mp_coretable.sas as follows:
28
+ `mp_coretable(MAXKEYTABLE)`.
29
+ @li permlib.locktable - Necessary if maxkeytable is being populated. The
30
+ definition is available by running mp_coretable.sas as follows:
31
+ `mp_coretable(LOCKTABLE)`.
32
+
33
+
34
+ @param [in] base_lib= (WORK) Libref of the base (target) table.
35
+ @param [in] base_dsn= (BASETABLE) Name of the base (target) table.
36
+ @param [in] append_lib= (WORK) Libref of the staging table
37
+ @param [in] append_dsn= (APPENDTABLE) Name of the staging table
38
+ @param [in] retained_key= (DEFAULT_RK) Name of RK to generate (should exist on
39
+ base table)
40
+ @param [in] business_key= (PK1 PK2) Business key against which to generate
41
+ RK values. Should be unique and not null on the staging table.
42
+ @param [in] check_uniqueness=(NO) Set to yes to perform a uniqueness check.
43
+ Recommended if there is a chance that the staging data is not unique on the
44
+ business key.
45
+ @param [in] maxkeytable= (0) Provide a maxkeytable libds reference here, to
46
+ store load metadata (maxkey val, load time). Set to zero if metadata is not
47
+ required, eg, when preparing a 'dummy' load. Structure is described above.
48
+ See below for sample data.
49
+ |KEYTABLE:$32.|KEYCOLUMN:$32.|MAX_KEY:best.|PROCESSED_DTTM:E8601DT26.6|
50
+ |---|---|---|---|
51
+ |`DC487173.MPE_SELECTBOX `|`SELECTBOX_RK `|`55 `|`1950427787.8 `|
52
+ |`DC487173.MPE_FILTERANYTABLE `|`filter_rk `|`14 `|`1951053886.8 `|
53
+ @param [in] locktable= (0) If updating the maxkeytable, provide the libds
54
+ reference to the lock table (per mp_lockanytable.sas macro)
55
+ @param [in] filter_str= Apply a filter - useful for SCD2 or BITEMPORAL loads.
56
+ Example: `filter_str=%str( (where=( &now < &tech_to)) )`
57
+ @param [out] outds= (WORK.APPEND) Output table (staging table + retained key)
58
+
59
+ <h4> SAS Macros </h4>
60
+ @li mf_existvar.sas
61
+ @li mf_getquotedstr.sas
62
+ @li mf_getuniquename.sas
63
+ @li mf_nobs.sas
64
+ @li mp_abort.sas
65
+ @li mp_lockanytable.sas
66
+
67
+ <h4> Related Macros </h4>
68
+ @li mp_filterstore.sas
69
+ @li mp_retainedkey.test.sas
70
+
71
+ @version 9.2
72
+
73
+ **/
74
+
75
+ %macro mp_retainedkey(
76
+ base_lib=WORK
77
+ ,base_dsn=BASETABLE
78
+ ,append_lib=WORK
79
+ ,append_dsn=APPENDTABLE
80
+ ,retained_key=DEFAULT_RK
81
+ ,business_key= PK1 PK2
82
+ ,check_uniqueness=NO
83
+ ,maxkeytable=0
84
+ ,locktable=0
85
+ ,outds=WORK.APPEND
86
+ ,filter_str=
87
+ );
88
+ %put &sysmacroname entry vars:;
89
+ %put _local_;
90
+
91
+ %local base_libds app_libds key_field check maxkey idx_pk newkey_cnt iserr
92
+ msg x tempds1 tempds2 comma_pk appnobs checknobs dropvar tempvar idx_val;
93
+ %let base_libds=%upcase(&base_lib..&base_dsn);
94
+ %let app_libds=%upcase(&append_lib..&append_dsn);
95
+ %let tempds1=%mf_getuniquename();
96
+ %let tempds2=%mf_getuniquename();
97
+ %let comma_pk=%mf_getquotedstr(in_str=%str(&business_key),dlm=%str(,),quote=);
98
+ %let outds=%sysfunc(ifc(%index(&outds,.)=0,work.&outds,&outds));
99
+ /* validation checks */
100
+ %let iserr=0;
101
+ %if &syscc>0 %then %do;
102
+ %let iserr=1;
103
+ %let msg=%str(SYSCC=&syscc on macro entry);
104
+ %end;
105
+ %else %if %sysfunc(exist(&base_libds))=0 %then %do;
106
+ %let iserr=1;
107
+ %let msg=%str(Base LIBDS (&base_libds) expected but NOT FOUND);
108
+ %end;
109
+ %else %if %sysfunc(exist(&app_libds))=0 %then %do;
110
+ %let iserr=1;
111
+ %let msg=%str(Append LIBDS (&app_libds) expected but NOT FOUND);
112
+ %end;
113
+ %else %if &maxkeytable ne 0 and %sysfunc(exist(&maxkeytable))=0 %then %do;
114
+ %let iserr=1;
115
+ %let msg=%str(Maxkeytable (&maxkeytable) expected but NOT FOUND);
116
+ %end;
117
+ %else %if &maxkeytable ne 0 and %sysfunc(exist(&locktable))=0 %then %do;
118
+ %let iserr=1;
119
+ %let msg=%str(Locktable (&locktable) expected but NOT FOUND);
120
+ %end;
121
+ %else %if %length(&business_key)=0 %then %do;
122
+ %let iserr=1;
123
+ %let msg=%str(Business key (&business_key) expected but NOT FOUND);
124
+ %end;
125
+
126
+ %do x=1 %to %sysfunc(countw(&business_key));
127
+ /* check business key values exist */
128
+ %let key_field=%scan(&business_key,&x,%str( ));
129
+ %if not %mf_existvar(&app_libds,&key_field) %then %do;
130
+ %let iserr=1;
131
+ %let msg=Business key (&key_field) not found on &app_libds!;
132
+ %goto err;
133
+ %end;
134
+ %else %if not %mf_existvar(&base_libds,&key_field) %then %do;
135
+ %let iserr=1;
136
+ %let msg=Business key (&key_field) not found on &base_libds!;
137
+ %goto err;
138
+ %end;
139
+ %end;
140
+ %err:
141
+ %if &iserr=1 %then %do;
142
+ /* err case so first perform an unlock of the base table before exiting */
143
+ %mp_lockanytable(
144
+ UNLOCK,lib=&base_lib,ds=&base_dsn,ref=%superq(msg),ctl_ds=&locktable
145
+ )
146
+ %end;
147
+ %mp_abort(iftrue=(&iserr=1),mac=mp_retainedkey,msg=%superq(msg))
148
+
149
+ proc sql noprint;
150
+ select sum(max(&retained_key),0) into: maxkey from &base_libds;
151
+
152
+ /**
153
+ * get base table RK and bus field values for lookup
154
+ */
155
+ proc sql noprint;
156
+ create table &tempds1 as
157
+ select distinct &comma_pk,&retained_key
158
+ from &base_libds &filter_str
159
+ order by &comma_pk,&retained_key;
160
+
161
+ %if &check_uniqueness=YES %then %do;
162
+ select count(*) into:checknobs
163
+ from (select distinct &comma_pk from &app_libds);
164
+ select count(*) into: appnobs from &app_libds; /* might be view */
165
+ %if &checknobs ne &appnobs %then %do;
166
+ %let msg=Source table &app_libds is not unique on (&business_key);
167
+ %let iserr=1;
168
+ %end;
169
+ %end;
170
+ %if &iserr=1 %then %do;
171
+ /* err case so first perform an unlock of the base table before exiting */
172
+ %mp_lockanytable(
173
+ UNLOCK,lib=&base_lib,ds=&base_dsn,ref=%superq(msg),ctl_ds=&locktable
174
+ )
175
+ %end;
176
+ %mp_abort(iftrue= (&iserr=1),mac=mp_retainedkey,msg=%superq(msg))
177
+
178
+ %if %mf_existvar(&app_libds,&retained_key)
179
+ %then %let dropvar=(drop=&retained_key);
180
+
181
+ /* prepare interim table with retained key populated for matching keys */
182
+ proc sql noprint;
183
+ create table &tempds2 as
184
+ select b.&retained_key, a.*
185
+ from &app_libds &dropvar a
186
+ left join &tempds1 b
187
+ on 1
188
+ %do idx_pk=1 %to %sysfunc(countw(&business_key));
189
+ %let idx_val=%scan(&business_key,&idx_pk);
190
+ and a.&idx_val=b.&idx_val
191
+ %end;
192
+ order by &retained_key;
193
+
194
+ /* identify the number of entries without retained keys (new records) */
195
+ select count(*) into: newkey_cnt
196
+ from &tempds2
197
+ where missing(&retained_key);
198
+ quit;
199
+
200
+ /**
201
+ * Update maxkey table if link provided
202
+ */
203
+ %if &maxkeytable ne 0 %then %do;
204
+ proc sql noprint;
205
+ select count(*) into: check from &maxkeytable
206
+ where upcase(keytable)="&base_libds";
207
+
208
+ %mp_lockanytable(LOCK
209
+ ,lib=%scan(&maxkeytable,1,.)
210
+ ,ds=%scan(&maxkeytable,2,.)
211
+ ,ref=Updating maxkeyvalues with mp_retainedkey
212
+ ,ctl_ds=&locktable
213
+ )
214
+ proc sql;
215
+ %if &check=0 %then %do;
216
+ insert into &maxkeytable
217
+ set keytable="&base_libds"
218
+ ,keycolumn="&retained_key"
219
+ ,max_key=%eval(&maxkey+&newkey_cnt)
220
+ ,processed_dttm="%sysfunc(datetime(),E8601DT26.6)"dt;
221
+ %end;
222
+ %else %do;
223
+ update &maxkeytable
224
+ set max_key=%eval(&maxkey+&newkey_cnt)
225
+ ,processed_dttm="%sysfunc(datetime(),E8601DT26.6)"dt
226
+ where keytable="&base_libds";
227
+ %end;
228
+ %mp_lockanytable(UNLOCK
229
+ ,lib=%scan(&maxkeytable,1,.)
230
+ ,ds=%scan(&maxkeytable,2,.)
231
+ ,ref=Updating maxkeyvalues with maxkey=%eval(&maxkey+&newkey_cnt)
232
+ ,ctl_ds=&locktable
233
+ )
234
+ %end;
235
+
236
+ /* fill in the missing retained key values */
237
+ %let tempvar=%mf_getuniquename();
238
+ data &outds(drop=&tempvar);
239
+ retain &tempvar %eval(&maxkey+1);
240
+ set &tempds2;
241
+ if &retained_key =. then &retained_key=&tempvar;
242
+ &tempvar=&tempvar+1;
243
+ run;
244
+
245
+ %mend mp_retainedkey;
246
+
@@ -20,10 +20,13 @@
20
20
  ;;;;
21
21
  run;
22
22
 
23
+ For more examples, see mp_validatecol.test.sas
24
+
23
25
  Tip - when contributing, use https://regex101.com to test the regex validity!
24
26
 
25
27
  @param [in] incol The column to be validated
26
28
  @param [in] rule The rule to apply. Current rules:
29
+ @li ISINT - checks if the variable is an integer
27
30
  @li ISNUM - checks if the variable is numeric
28
31
  @li LIBDS - matches LIBREF.DATASET format
29
32
  @li FORMAT - checks if the provided format is syntactically valid
@@ -32,6 +35,9 @@
32
35
  <h4> SAS Macros </h4>
33
36
  @li mf_getuniquename.sas
34
37
 
38
+ <h4> Related Macros </h4>
39
+ @li mp_validatecol.test.sas
40
+
35
41
  @version 9.3
36
42
  **/
37
43
 
@@ -41,7 +47,13 @@
41
47
  %local tempcol;
42
48
  %let tempcol=%mf_getuniquename();
43
49
 
44
- %if &rule=ISNUM %then %do;
50
+ %if &rule=ISINT %then %do;
51
+ &tempcol=input(&incol,?? best32.);
52
+ &outcol=0;
53
+ if not missing(&tempcol) then if mod(&incol,1)=0 then &outcol=1;
54
+ drop &tempcol;
55
+ %end;
56
+ %else %if &rule=ISNUM %then %do;
45
57
  /*
46
58
  credit SØREN LASSEN
47
59
  https://sasmacro.blogspot.com/2009/06/welcome-isnum-macro.html
package/package.json CHANGED
@@ -38,5 +38,5 @@
38
38
  "dependencies": {
39
39
  "ts-loader": "^9.2.6"
40
40
  },
41
- "version": "3.4.0"
41
+ "version": "3.7.1"
42
42
  }