@sasjs/core 3.2.1 → 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.
- package/base/mf_existvar.sas +6 -2
- package/base/mf_getengine.sas +4 -3
- package/base/mf_writefile.sas +67 -0
- package/base/mp_applyformats.sas +181 -0
- package/base/mp_getformats.sas +1 -0
- package/base/mp_lockanytable.sas +14 -17
- package/base/mp_makedata.sas +1 -0
- package/base/mp_retainedkey.sas +248 -0
- package/base/mp_validatecol.sas +30 -1
- package/package.json +1 -1
package/base/mf_existvar.sas
CHANGED
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
|
|
8
8
|
%put %mf_existvar(work.someds, somevar)
|
|
9
9
|
|
|
10
|
-
@param libds
|
|
11
|
-
@param var
|
|
10
|
+
@param [in] libds 2 part dataset or view reference
|
|
11
|
+
@param [in] var variable name
|
|
12
|
+
|
|
13
|
+
<h4> Related Macros </h4>
|
|
14
|
+
@li mf_existvar.test.sas
|
|
15
|
+
|
|
12
16
|
@version 9.2
|
|
13
17
|
@author Allan Bowe
|
|
14
18
|
**/
|
package/base/mf_getengine.sas
CHANGED
|
@@ -12,9 +12,10 @@
|
|
|
12
12
|
contributors of Chris Hemedingers blog [post](
|
|
13
13
|
http://blogs.sas.com/content/sasdummy/2013/06/04/find-a-sas-library-engine/)
|
|
14
14
|
|
|
15
|
-
@param libref Library reference (also accepts a 2 level libds ref).
|
|
15
|
+
@param [in] libref Library reference (also accepts a 2 level libds ref).
|
|
16
16
|
|
|
17
|
-
@return output returns the library engine for the FIRST library
|
|
17
|
+
@return output returns the library engine (uppercase) for the FIRST library
|
|
18
|
+
encountered.
|
|
18
19
|
|
|
19
20
|
@warning will only return the FIRST library engine - for concatenated
|
|
20
21
|
libraries, with different engines, inconsistent results may be encountered.
|
|
@@ -46,7 +47,7 @@
|
|
|
46
47
|
%let rc= %sysfunc(close(&dsid));
|
|
47
48
|
%end;
|
|
48
49
|
|
|
49
|
-
&engine
|
|
50
|
+
%upcase(&engine)
|
|
50
51
|
|
|
51
52
|
%mend mf_getengine;
|
|
52
53
|
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Creates a text file using pure macro
|
|
4
|
+
@details Creates a text file of up to 10 lines. If further lines are
|
|
5
|
+
desired, feel free to [create an issue](
|
|
6
|
+
https://github.com/sasjs/core/issues/new), or make a pull request!
|
|
7
|
+
|
|
8
|
+
The use of PARMBUFF was considered for this macro, but it would have made
|
|
9
|
+
things problematic for writing lines containing commas.
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
|
|
13
|
+
%mf_writefile(&sasjswork/myfile.txt,l1=some content,l2=more content)
|
|
14
|
+
data _null_;
|
|
15
|
+
infile "&sasjswork/myfile.txt";
|
|
16
|
+
input;
|
|
17
|
+
list;
|
|
18
|
+
run;
|
|
19
|
+
|
|
20
|
+
@param [in] fpath Full path to file to be created or appended to
|
|
21
|
+
@param [in] mode= (O) Available options are A or O as follows:
|
|
22
|
+
@li A APPEND mode, writes new records after the current end of the file.
|
|
23
|
+
@li O OUTPUT mode, writes new records from the beginning of the file.
|
|
24
|
+
@param [in] l1= First line
|
|
25
|
+
@param [in] l2= Second line (etc through to l10)
|
|
26
|
+
|
|
27
|
+
<h4> Related Macros </h4>
|
|
28
|
+
@li mf_writefile.test.sas
|
|
29
|
+
|
|
30
|
+
@version 9.2
|
|
31
|
+
@author Allan Bowe
|
|
32
|
+
**/
|
|
33
|
+
/** @cond */
|
|
34
|
+
|
|
35
|
+
%macro mf_writefile(fpath,mode=O,l1=,l2=,l3=,l4=,l5=,l6=,l7=,l8=,l9=,l10=
|
|
36
|
+
)/*/STORE SOURCE*/;
|
|
37
|
+
%local fref rc fid i total_lines;
|
|
38
|
+
|
|
39
|
+
/* find number of lines by reference to first non-blank param */
|
|
40
|
+
%do i=10 %to 1 %by -1;
|
|
41
|
+
%if %str(&&l&i) ne %str() %then %goto continue;
|
|
42
|
+
%end;
|
|
43
|
+
%continue:
|
|
44
|
+
%let total_lines=&i;
|
|
45
|
+
|
|
46
|
+
%if %sysfunc(filename(fref,&fpath)) ne 0 %then %do;
|
|
47
|
+
%put &=fref &=fpath;
|
|
48
|
+
%put %str(ERR)OR: %sysfunc(sysmsg());
|
|
49
|
+
%return;
|
|
50
|
+
%end;
|
|
51
|
+
|
|
52
|
+
%let fid=%sysfunc(fopen(&fref,&mode));
|
|
53
|
+
|
|
54
|
+
%if &fid=0 %then %do;
|
|
55
|
+
%put %str(ERR)OR: %sysfunc(sysmsg());
|
|
56
|
+
%return;
|
|
57
|
+
%end;
|
|
58
|
+
|
|
59
|
+
%do i=1 %to &total_lines;
|
|
60
|
+
%let rc=%sysfunc(fput(&fid, &&l&i));
|
|
61
|
+
%let rc=%sysfunc(fwrite(&fid));
|
|
62
|
+
%end;
|
|
63
|
+
%let rc=%sysfunc(fclose(&fid));
|
|
64
|
+
%let rc=%sysfunc(filename(&fref));
|
|
65
|
+
|
|
66
|
+
%mend mf_writefile;
|
|
67
|
+
/** @endcond */
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Apply a set of formats to a table
|
|
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
|
+
|
|
8
|
+
@li lib - the libref of the table to be updated
|
|
9
|
+
@li ds - the dataset to be updated
|
|
10
|
+
@li var - the variable to be updated
|
|
11
|
+
@li fmt - the format to apply. Missing or default ($CHAR, 8.) formats are
|
|
12
|
+
ignored.
|
|
13
|
+
|
|
14
|
+
The macro will abort in the following scenarios:
|
|
15
|
+
|
|
16
|
+
@li Libref not assigned
|
|
17
|
+
@li Dataset does not exist
|
|
18
|
+
@li Input table contains null or invalid values
|
|
19
|
+
|
|
20
|
+
Example usage:
|
|
21
|
+
|
|
22
|
+
data work.example;
|
|
23
|
+
set sashelp.prdsale;
|
|
24
|
+
format _all_ clear;
|
|
25
|
+
run;
|
|
26
|
+
|
|
27
|
+
%mp_getcols(sashelp.prdsale,outds=work.cols)
|
|
28
|
+
|
|
29
|
+
data work.cols2;
|
|
30
|
+
set work.cols;
|
|
31
|
+
lib='WORK';
|
|
32
|
+
ds='EXAMPLE';
|
|
33
|
+
var=name;
|
|
34
|
+
fmt=format;
|
|
35
|
+
keep lib ds var fmt;
|
|
36
|
+
run;
|
|
37
|
+
|
|
38
|
+
%mp_applyformats(work.cols2)
|
|
39
|
+
|
|
40
|
+
@param [in] inds The input dataset containing the formats to apply (and where
|
|
41
|
+
to apply them). Example structure:
|
|
42
|
+
|LIB:$8.|DS:$32.|VAR:$32.|FMT:$49.|
|
|
43
|
+
|---|---|---|---|
|
|
44
|
+
|`WORK `|`EXAMPLE `|`ACTUAL `|`DOLLAR12.2 `|
|
|
45
|
+
|`WORK `|`EXAMPLE `|`COUNTRY `|`$CHAR10. `|
|
|
46
|
+
|`WORK `|`EXAMPLE `|`DIVISION `|`$CHAR10. `|
|
|
47
|
+
|`WORK `|`EXAMPLE `|`MONTH `|`MONNAME3. `|
|
|
48
|
+
|`WORK `|`EXAMPLE `|`PREDICT `|`DOLLAR12.2 `|
|
|
49
|
+
|`WORK `|`EXAMPLE `|`PRODTYPE `|`$CHAR10. `|
|
|
50
|
+
|`WORK `|`EXAMPLE `|`PRODUCT `|`$CHAR10. `|
|
|
51
|
+
|`WORK `|`EXAMPLE `|`QUARTER `|`8. `|
|
|
52
|
+
|`WORK `|`EXAMPLE `|`REGION `|`$CHAR10. `|
|
|
53
|
+
|`WORK `|`EXAMPLE `|`YEAR `|`8. `|
|
|
54
|
+
|
|
55
|
+
@param [out] errds= (0) Provide a libds reference here to export the
|
|
56
|
+
error messages to a table. In this case, they will not be printed to the
|
|
57
|
+
log.
|
|
58
|
+
|
|
59
|
+
<h4> SAS Macros </h4>
|
|
60
|
+
@li mf_getengine.sas
|
|
61
|
+
@li mf_getuniquefileref.sas
|
|
62
|
+
@li mf_getuniquename.sas
|
|
63
|
+
@li mf_nobs.sas
|
|
64
|
+
@li mp_validatecol.sas
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
<h4> Related Macros </h4>
|
|
68
|
+
@li mp_getformats.sas
|
|
69
|
+
|
|
70
|
+
@version 9.2
|
|
71
|
+
@author Allan Bowe
|
|
72
|
+
|
|
73
|
+
**/
|
|
74
|
+
|
|
75
|
+
%macro mp_applyformats(inds,errds=0
|
|
76
|
+
)/*/STORE SOURCE*/;
|
|
77
|
+
%local outds liblist i engine lib msg ;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Validations
|
|
81
|
+
*/
|
|
82
|
+
proc sort data=&inds;
|
|
83
|
+
by lib ds var fmt;
|
|
84
|
+
run;
|
|
85
|
+
|
|
86
|
+
%if &errds=0 %then %let outds=%mf_getuniquename(prefix=mp_applyformats);
|
|
87
|
+
%else %let outds=&errds;
|
|
88
|
+
|
|
89
|
+
data &outds;
|
|
90
|
+
set &inds;
|
|
91
|
+
where fmt not in ('','.', '$', '$CHAR.','8.');
|
|
92
|
+
length msg $128;
|
|
93
|
+
by lib ds var fmt;
|
|
94
|
+
if libref(lib) ne 0 then do;
|
|
95
|
+
msg=catx(' ','libref',lib,'is not assigned!');
|
|
96
|
+
%if &errds=0 %then %do;
|
|
97
|
+
putlog "%str(ERR)OR: " msg;
|
|
98
|
+
%end;
|
|
99
|
+
output;
|
|
100
|
+
return;
|
|
101
|
+
end;
|
|
102
|
+
if exist(cats(lib,'.',ds)) ne 1 then do;
|
|
103
|
+
msg=catx(' ','libds',lib,'.',ds,'does not exist!');
|
|
104
|
+
%if &errds=0 %then %do;
|
|
105
|
+
putlog "%str(ERR)OR: " msg;
|
|
106
|
+
%end;
|
|
107
|
+
output;
|
|
108
|
+
return;
|
|
109
|
+
end;
|
|
110
|
+
%mp_validatecol(fmt,FORMAT,is_fmt)
|
|
111
|
+
if is_fmt=0 then do;
|
|
112
|
+
msg=catx(' ','format',fmt,'on libds',lib,'.',ds,'.',var,'is not valid!');
|
|
113
|
+
%if &errds=0 %then %do;
|
|
114
|
+
putlog "%str(ERR)OR: " msg;
|
|
115
|
+
%end;
|
|
116
|
+
output;
|
|
117
|
+
return;
|
|
118
|
+
end;
|
|
119
|
+
|
|
120
|
+
if first.ds then do;
|
|
121
|
+
retain dsid;
|
|
122
|
+
dsid=open(cats(lib,'.',ds));
|
|
123
|
+
if dsid=0 then do;
|
|
124
|
+
msg=catx(' ','libds',lib,'.',ds,' could not be opened!');
|
|
125
|
+
%if &errds=0 %then %do;
|
|
126
|
+
putlog "%str(ERR)OR: " msg;
|
|
127
|
+
%end;
|
|
128
|
+
output;
|
|
129
|
+
return;
|
|
130
|
+
end;
|
|
131
|
+
if varnum(dsid,var)<1 then do;
|
|
132
|
+
msg=catx(' ','Variable',lib,'.',ds,'.',var,' was not found!');
|
|
133
|
+
%if &errds=0 %then %do;
|
|
134
|
+
putlog "%str(ERR)OR: " msg;
|
|
135
|
+
%end;
|
|
136
|
+
output;
|
|
137
|
+
end;
|
|
138
|
+
end;
|
|
139
|
+
if last.ds then rc=close(dsid);
|
|
140
|
+
run;
|
|
141
|
+
|
|
142
|
+
proc sql noprint;
|
|
143
|
+
select distinct lib into: liblist separated by ' ' from &inds;
|
|
144
|
+
%put &=liblist;
|
|
145
|
+
%do i=1 %to %sysfunc(countw(&liblist));
|
|
146
|
+
%let lib=%scan(&liblist,1);
|
|
147
|
+
%let engine=%mf_getengine(&lib);
|
|
148
|
+
%if &engine ne V9 and &engine ne BASE %then %do;
|
|
149
|
+
%let msg=&lib has &engine engine - formats cannot be applied;
|
|
150
|
+
proc sql;
|
|
151
|
+
insert into &outds set lib="&lib",ds="_all_",var="_all", msg="&msg" ;
|
|
152
|
+
%if &errds=0 %then %put %str(ERR)OR: &msg;
|
|
153
|
+
%end;
|
|
154
|
+
%end;
|
|
155
|
+
|
|
156
|
+
%if %mf_nobs(&outds)>0 %then %return;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Validations complete - now apply the actual formats!
|
|
160
|
+
*/
|
|
161
|
+
%let fref=%mf_getuniquefileref();
|
|
162
|
+
data _null_;
|
|
163
|
+
set &inds;
|
|
164
|
+
by lib ds var fmt;
|
|
165
|
+
where fmt not in ('','.', '$', '$CHAR.','8.');
|
|
166
|
+
file &fref;
|
|
167
|
+
if first.lib then put 'proc datasets nolist lib=' lib ';';
|
|
168
|
+
if first.ds then put ' modify ' ds ';';
|
|
169
|
+
put ' format ' var fmt ';';
|
|
170
|
+
if last.ds then put ' run;';
|
|
171
|
+
if last.lib then put 'quit;';
|
|
172
|
+
run;
|
|
173
|
+
|
|
174
|
+
%inc &fref/source2;
|
|
175
|
+
|
|
176
|
+
%if &errds=0 %then %do;
|
|
177
|
+
proc sql;
|
|
178
|
+
drop table &outds;
|
|
179
|
+
%end;
|
|
180
|
+
|
|
181
|
+
%mend mp_applyformats;
|
package/base/mp_getformats.sas
CHANGED
package/base/mp_lockanytable.sas
CHANGED
|
@@ -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;
|
package/base/mp_makedata.sas
CHANGED
|
@@ -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/base/mp_validatecol.sas
CHANGED
|
@@ -20,15 +20,24 @@
|
|
|
20
20
|
;;;;
|
|
21
21
|
run;
|
|
22
22
|
|
|
23
|
+
For more examples, see mp_validatecol.test.sas
|
|
24
|
+
|
|
25
|
+
Tip - when contributing, use https://regex101.com to test the regex validity!
|
|
26
|
+
|
|
23
27
|
@param [in] incol The column to be validated
|
|
24
28
|
@param [in] rule The rule to apply. Current rules:
|
|
29
|
+
@li ISINT - checks if the variable is an integer
|
|
25
30
|
@li ISNUM - checks if the variable is numeric
|
|
26
31
|
@li LIBDS - matches LIBREF.DATASET format
|
|
32
|
+
@li FORMAT - checks if the provided format is syntactically valid
|
|
27
33
|
@param [out] outcol The variable to create, with the results of the match
|
|
28
34
|
|
|
29
35
|
<h4> SAS Macros </h4>
|
|
30
36
|
@li mf_getuniquename.sas
|
|
31
37
|
|
|
38
|
+
<h4> Related Macros </h4>
|
|
39
|
+
@li mp_validatecol.test.sas
|
|
40
|
+
|
|
32
41
|
@version 9.3
|
|
33
42
|
**/
|
|
34
43
|
|
|
@@ -38,7 +47,13 @@
|
|
|
38
47
|
%local tempcol;
|
|
39
48
|
%let tempcol=%mf_getuniquename();
|
|
40
49
|
|
|
41
|
-
%if &rule=
|
|
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;
|
|
42
57
|
/*
|
|
43
58
|
credit SØREN LASSEN
|
|
44
59
|
https://sasmacro.blogspot.com/2009/06/welcome-isnum-macro.html
|
|
@@ -62,5 +77,19 @@
|
|
|
62
77
|
if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
|
|
63
78
|
else &outcol=0;
|
|
64
79
|
%end;
|
|
80
|
+
%else %if &rule=FORMAT %then %do;
|
|
81
|
+
/* match valid format - regex could probably be improved */
|
|
82
|
+
if _n_=1 then do;
|
|
83
|
+
retain &tempcol;
|
|
84
|
+
&tempcol=prxparse('/^[_a-z\$]\w{0,31}\.[0-9]*$/i');
|
|
85
|
+
if missing(&tempcol) then do;
|
|
86
|
+
putlog "%str(ERR)OR: Invalid expression for FORMAT";
|
|
87
|
+
stop;
|
|
88
|
+
end;
|
|
89
|
+
drop &tempcol;
|
|
90
|
+
end;
|
|
91
|
+
if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
|
|
92
|
+
else &outcol=0;
|
|
93
|
+
%end;
|
|
65
94
|
|
|
66
95
|
%mend mp_validatecol;
|
package/package.json
CHANGED