@sasjs/core 2.55.0 → 2.56.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/LICENSE +1 -1
- package/README.md +8 -8
- package/base/mf_getquotedstr.sas +10 -9
- package/base/mp_getcols.sas +1 -1
- package/base/mp_getpk.sas +5 -0
- package/base/mp_guesspk.sas +19 -11
- package/base/mp_init.sas +54 -0
- package/base/mp_makedata.sas +87 -0
- package/base/mp_reseterror.sas +27 -0
- package/base/mp_sortinplace.sas +1 -1
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright 2021 (Allan Bowe)
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Macro Core
|
|
2
2
|
[![npm package][npm-image]][npm-url]
|
|
3
3
|
[![Github Workflow][githubworkflow-image]][githubworkflow-url]
|
|
4
|
-
|
|
4
|
+

|
|
5
5
|

|
|
6
6
|
[](/LICENSE)
|
|
7
7
|

|
|
@@ -31,21 +31,21 @@ Documentation: https://core.sasjs.io
|
|
|
31
31
|
|
|
32
32
|
## Components
|
|
33
33
|
|
|
34
|
-
###
|
|
34
|
+
### BASE library (SAS9/Viya)
|
|
35
35
|
|
|
36
36
|
- OS independent
|
|
37
37
|
- Not metadata aware
|
|
38
38
|
- No X command
|
|
39
39
|
- Prefixes: _mf_, _mp_
|
|
40
40
|
|
|
41
|
-
####
|
|
41
|
+
#### FCMP library (SAS9/Viya)
|
|
42
42
|
- Function and macro names are identical, except for special cases
|
|
43
43
|
- Prefixes: _mcf_
|
|
44
44
|
|
|
45
45
|
The fcmp macros are used to generate fcmp functions, and can be used with or
|
|
46
46
|
without the `proc fcmp` wrapper.
|
|
47
47
|
|
|
48
|
-
###
|
|
48
|
+
### META library (SAS9 only)
|
|
49
49
|
|
|
50
50
|
Macros used in SAS EBI, which connect to the metadata server.
|
|
51
51
|
|
|
@@ -54,7 +54,7 @@ Macros used in SAS EBI, which connect to the metadata server.
|
|
|
54
54
|
- No X command
|
|
55
55
|
- Prefixes: _mm_
|
|
56
56
|
|
|
57
|
-
###
|
|
57
|
+
### SERVER library (@sasjs/server only)
|
|
58
58
|
These macros are used for building applications using [@sasjs/server](https://server.sasjs.io) - an open source REST API for Desktop SAS.
|
|
59
59
|
|
|
60
60
|
- OS independent
|
|
@@ -62,7 +62,7 @@ These macros are used for building applications using [@sasjs/server](https://se
|
|
|
62
62
|
- No X command
|
|
63
63
|
- Prefixes: _ms_
|
|
64
64
|
|
|
65
|
-
###
|
|
65
|
+
### VIYA library (Viya only)
|
|
66
66
|
|
|
67
67
|
Macros used for interfacing with SAS Viya.
|
|
68
68
|
|
|
@@ -70,14 +70,14 @@ Macros used for interfacing with SAS Viya.
|
|
|
70
70
|
- No X command
|
|
71
71
|
- Prefixes: _mv_, _mvf_
|
|
72
72
|
|
|
73
|
-
###
|
|
73
|
+
### METAX library (SAS9 only)
|
|
74
74
|
|
|
75
75
|
- OS specific
|
|
76
76
|
- Metadata aware
|
|
77
77
|
- X command enabled
|
|
78
78
|
- Prefixes: _mmw_,_mmu_,_mmx_
|
|
79
79
|
|
|
80
|
-
###
|
|
80
|
+
### LUA library
|
|
81
81
|
|
|
82
82
|
Wait - this is a macro library - what is LUA doing here? Well, it is a little known fact that you CAN run LUA within a SAS Macro. It has to be written to a text file with a `.lua` extension, from where you can `%include` it. So, without using the `proc lua` wrapper.
|
|
83
83
|
|
package/base/mf_getquotedstr.sas
CHANGED
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
for:
|
|
16
16
|
> "these","words","are","double","quoted"
|
|
17
17
|
|
|
18
|
-
@param in_str
|
|
19
|
-
@param dlm=
|
|
20
|
-
@param indlm=
|
|
21
|
-
@param quote=
|
|
22
|
-
than uppercase S or D is supplied, then that value will
|
|
23
|
-
quoting character.
|
|
18
|
+
@param [in] in_str The unquoted, spaced delimited string to transform
|
|
19
|
+
@param [in] dlm= The delimeter to be applied to the output (default comma)
|
|
20
|
+
@param [in] indlm= (,) The delimeter used for the input (default is space)
|
|
21
|
+
@param [in] quote= (S) The quote mark to apply (S=Single, D=Double, N=None).
|
|
22
|
+
If any other value than uppercase S or D is supplied, then that value will
|
|
23
|
+
be used as the quoting character.
|
|
24
24
|
@return output returns a string with the newly quoted / delimited output.
|
|
25
25
|
|
|
26
26
|
@version 9.2
|
|
@@ -30,9 +30,10 @@
|
|
|
30
30
|
|
|
31
31
|
%macro mf_getquotedstr(IN_STR,DLM=%str(,),QUOTE=S,indlm=%str( )
|
|
32
32
|
)/*/STORE SOURCE*/;
|
|
33
|
-
|
|
34
|
-
%
|
|
35
|
-
%else %let quote=%
|
|
33
|
+
/* credit Rowland Hale - byte34 is double quote, 39 is single quote */
|
|
34
|
+
%if "e=S %then %let quote=%qsysfunc(byte(39));
|
|
35
|
+
%else %if "e=D %then %let quote=%qsysfunc(byte(34));
|
|
36
|
+
%else %if "e=N %then %let quote=;
|
|
36
37
|
%local i item buffer;
|
|
37
38
|
%let i=1;
|
|
38
39
|
%do %while (%qscan(&IN_STR,&i,%str(&indlm)) ne %str() ) ;
|
package/base/mp_getcols.sas
CHANGED
|
@@ -51,7 +51,7 @@ data &outds(keep=name type length varnum format label ddtype);
|
|
|
51
51
|
else if formatd=0 then format=cats(format2,formatl,'.');
|
|
52
52
|
else format=cats(format2,formatl,'.',formatd);
|
|
53
53
|
type='N';
|
|
54
|
-
if format=:'DATETIME' then ddtype='DATETIME';
|
|
54
|
+
if format=:'DATETIME' or format=:'E8601DT' then ddtype='DATETIME';
|
|
55
55
|
else if format=:'DATE' or format=:'DDMMYY' or format=:'MMDDYY'
|
|
56
56
|
or format=:'YYMMDD' or format=:'E8601DA' or format=:'B8601DA'
|
|
57
57
|
or format=:'MONYY'
|
package/base/mp_getpk.sas
CHANGED
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
|
|
24
24
|
Returns:
|
|
25
25
|
|
|
26
|
+
|libref:$8.|dsn:$32.|memtype:$8.|dbms_memtype:$32.|typemem:$8.|memlabel:$256.|nvar:best.|compress:$8.|pk_fields:$512.|
|
|
27
|
+
|---|---|---|---|---|---|---|---|---|
|
|
28
|
+
|WORK|EXAMPLE|DATA| |DATA| |4|NO|TX_FROM DD_TYPE DD_SOURCE|
|
|
29
|
+
|
|
26
30
|
|
|
27
31
|
@param [in] lib The libref to examine
|
|
28
32
|
@param [in] ds= (0) Select the dataset to examine, else use 0 for all tables
|
|
@@ -37,6 +41,7 @@
|
|
|
37
41
|
|
|
38
42
|
<h4> Related Macros </h4>
|
|
39
43
|
@li mp_getpk.test.sas
|
|
44
|
+
@li mp_guesspk.sas
|
|
40
45
|
|
|
41
46
|
@version 9.3
|
|
42
47
|
@author Macro People Ltd
|
package/base/mp_guesspk.sas
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
@file
|
|
2
|
+
@file
|
|
3
3
|
@brief Guess the primary key of a table
|
|
4
|
-
@details Tries to guess the primary key of a table based on the following
|
|
4
|
+
@details Tries to guess the primary key of a table based on the following
|
|
5
|
+
logic:
|
|
5
6
|
|
|
6
7
|
* Columns with nulls are ignored
|
|
7
8
|
* Return only column combinations that provide unique results
|
|
8
|
-
* Start from one column, then move out to
|
|
9
|
+
* Start from one column, then move out to composite keys of 2 to 6 columns
|
|
9
10
|
|
|
10
11
|
The library of the target should be assigned before using this macro.
|
|
11
12
|
|
|
12
13
|
Usage:
|
|
13
14
|
|
|
14
|
-
filename mc url
|
|
15
|
+
filename mc url
|
|
16
|
+
"https://raw.githubusercontent.com/sasjs/core/main/all.sas";
|
|
15
17
|
%inc mc;
|
|
16
18
|
%mp_guesspk(sashelp.class,outds=classpks)
|
|
17
19
|
|
|
18
20
|
@param baseds The dataset to analyse
|
|
19
21
|
@param outds= The output dataset to contain the possible PKs
|
|
20
|
-
@param max_guesses= The total number of possible primary keys to generate.
|
|
21
|
-
table
|
|
22
|
-
@param min_rows= The minimum number of rows a table should have in order
|
|
23
|
-
and guess the PK.
|
|
22
|
+
@param max_guesses= (3) The total number of possible primary keys to generate.
|
|
23
|
+
A table may have multiple unlikely PKs, so no need to list them all.
|
|
24
|
+
@param min_rows= (5) The minimum number of rows a table should have in order
|
|
25
|
+
to try and guess the PK.
|
|
24
26
|
|
|
25
27
|
<h4> SAS Macros </h4>
|
|
26
28
|
@li mf_getvarlist.sas
|
|
27
29
|
@li mf_getuniquename.sas
|
|
28
30
|
@li mf_nobs.sas
|
|
29
31
|
|
|
32
|
+
<h4> Related Macros </h4>
|
|
33
|
+
@li mp_getpk.sas
|
|
34
|
+
|
|
30
35
|
@version 9.3
|
|
31
36
|
@author Allan Bowe
|
|
32
37
|
|
|
@@ -196,7 +201,8 @@
|
|
|
196
201
|
%let lev4=%scan(&posspks,&l);
|
|
197
202
|
%if &lev1 ne &lev4 and &lev2 ne &lev4 and &lev3 ne &lev4 %then %do;
|
|
198
203
|
/* check for four level uniqueness */
|
|
199
|
-
proc sort data=&pkds(keep=&lev1 &lev2 &lev3 &lev4)
|
|
204
|
+
proc sort data=&pkds(keep=&lev1 &lev2 &lev3 &lev4)
|
|
205
|
+
out=&tmpds noduprec;
|
|
200
206
|
by _all_;
|
|
201
207
|
run;
|
|
202
208
|
%if %mf_nobs(&tmpds)=&rows %then %do;
|
|
@@ -233,7 +239,8 @@
|
|
|
233
239
|
%let lev5=%scan(&posspks,&m);
|
|
234
240
|
%if &lev1 ne &lev5 & &lev2 ne &lev5 & &lev3 ne &lev5 & &lev4 ne &lev5 %then %do;
|
|
235
241
|
/* check for four level uniqueness */
|
|
236
|
-
proc sort data=&pkds(keep=&lev1 &lev2 &lev3 &lev4 &lev5)
|
|
242
|
+
proc sort data=&pkds(keep=&lev1 &lev2 &lev3 &lev4 &lev5)
|
|
243
|
+
out=&tmpds noduprec;
|
|
237
244
|
by _all_;
|
|
238
245
|
run;
|
|
239
246
|
%if %mf_nobs(&tmpds)=&rows %then %do;
|
|
@@ -282,7 +289,8 @@
|
|
|
282
289
|
run;
|
|
283
290
|
%if %mf_nobs(&tmpds)=&rows %then %do;
|
|
284
291
|
proc sql;
|
|
285
|
-
insert into &outds
|
|
292
|
+
insert into &outds
|
|
293
|
+
values("&lev1 &lev2 &lev3 &lev4 &lev5 &lev6");
|
|
286
294
|
%if %mf_nobs(&outds) ge &max_guesses %then %do;
|
|
287
295
|
%put &sysmacroname: Max PKs reached at Level 6 for &baseds;
|
|
288
296
|
%return;
|
package/base/mp_init.sas
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Initialise session with useful settings and variables
|
|
4
|
+
@details Implements a set of recommended options for general SAS use. This
|
|
5
|
+
macro is NOT used elsewhere within the core library (other than in tests),
|
|
6
|
+
but it is used by the SASjs team when building web services for
|
|
7
|
+
SAS-Powered applications elsewhere.
|
|
8
|
+
|
|
9
|
+
If you have a good idea for an option, setting, or useful global variable -
|
|
10
|
+
feel free to [raise an issue](https://github.com/sasjs/core/issues/new)!
|
|
11
|
+
|
|
12
|
+
All global variables are prefixed with "SASJS_" (unless modfied with the
|
|
13
|
+
prefix parameter).
|
|
14
|
+
|
|
15
|
+
@param [in] prefix= (SASJS) The prefix to apply to the global macro variables
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@version 9.2
|
|
19
|
+
@author Allan Bowe
|
|
20
|
+
|
|
21
|
+
**/
|
|
22
|
+
|
|
23
|
+
%macro mp_init(prefix=
|
|
24
|
+
)/*/STORE SOURCE*/;
|
|
25
|
+
|
|
26
|
+
%global
|
|
27
|
+
&prefix._INIT_NUM /* initialisation time as numeric */
|
|
28
|
+
&prefix._INIT_DTTM /* initialisation time in E8601DT26.6 format */
|
|
29
|
+
;
|
|
30
|
+
%if %eval(&&&prefix._INIT_NUM>0) %then %return; /* only run once */
|
|
31
|
+
|
|
32
|
+
data _null_;
|
|
33
|
+
dttm=datetime();
|
|
34
|
+
call symputx("&prefix._init_num",dttm);
|
|
35
|
+
call symputx("&prefix._init_dttm",put(dttm,E8601DT26.6));
|
|
36
|
+
run;
|
|
37
|
+
|
|
38
|
+
options
|
|
39
|
+
autocorrect /* disallow mis-spelled procedure names */
|
|
40
|
+
compress=CHAR /* default is none so ensure we have something! */
|
|
41
|
+
datastmtchk=ALLKEYWORDS /* protection from overwriting input datasets */
|
|
42
|
+
errorcheck=STRICT /* catch errors in libname/filename statements */
|
|
43
|
+
fmterr /* ensure error when a format cannot be found */
|
|
44
|
+
mergenoby=ERROR /*
|
|
45
|
+
missing=. /* some sites change this which causes hard to detect errors */
|
|
46
|
+
noquotelenmax /* avoid warnings for long strings */
|
|
47
|
+
noreplace /* avoid overwriting permanent datasets */
|
|
48
|
+
ps=max /* reduce log size slightly */
|
|
49
|
+
validmemname=COMPATIBLE /* avoid special characters etc in table names */
|
|
50
|
+
validvarname=V7 /* avoid special characters etc in variable names */
|
|
51
|
+
varlenchk=ERROR /* fail hard if truncation (data loss) can result */
|
|
52
|
+
;
|
|
53
|
+
|
|
54
|
+
%mend mp_init;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Create sample data based on the structure of an empty table
|
|
4
|
+
@details Many SAS projects involve sensitive datasets. One way to _ensure_
|
|
5
|
+
the data is anonymised, is never to receive it in the first place! Often
|
|
6
|
+
consultants are provided with empty tables, and expected to create complex
|
|
7
|
+
ETL flows.
|
|
8
|
+
|
|
9
|
+
This macro can help by taking an empty table, and populating it with data
|
|
10
|
+
according to the variable types and formats.
|
|
11
|
+
|
|
12
|
+
TODO:
|
|
13
|
+
@li Respect PKs
|
|
14
|
+
@li Respect NOT NULLs
|
|
15
|
+
@li Consider dates, datetimes, times, integers etc
|
|
16
|
+
|
|
17
|
+
Usage:
|
|
18
|
+
|
|
19
|
+
proc sql;
|
|
20
|
+
create table work.example(
|
|
21
|
+
TX_FROM float format=datetime19.,
|
|
22
|
+
DD_TYPE char(16),
|
|
23
|
+
DD_SOURCE char(2048),
|
|
24
|
+
DD_SHORTDESC char(256),
|
|
25
|
+
constraint pk primary key(tx_from, dd_type,dd_source),
|
|
26
|
+
constraint nnn not null(DD_SHORTDESC)
|
|
27
|
+
);
|
|
28
|
+
%mp_makedata(work.example)
|
|
29
|
+
|
|
30
|
+
@param [in] libds The empty table in which to create data
|
|
31
|
+
@param [out] obs= (500) The number of records to create.
|
|
32
|
+
|
|
33
|
+
<h4> SAS Macros </h4>
|
|
34
|
+
@li mf_getuniquename.sas
|
|
35
|
+
@li mf_getvarlen.sas
|
|
36
|
+
@li mf_nobs.sas
|
|
37
|
+
@li mp_getcols.sas
|
|
38
|
+
@li mp_getpk.sas
|
|
39
|
+
|
|
40
|
+
@version 9.2
|
|
41
|
+
@author Allan Bowe
|
|
42
|
+
|
|
43
|
+
**/
|
|
44
|
+
|
|
45
|
+
%macro mp_makedata(libds
|
|
46
|
+
,obs=500
|
|
47
|
+
)/*/STORE SOURCE*/;
|
|
48
|
+
|
|
49
|
+
%local ds1 c1 n1 i col charvars numvars;
|
|
50
|
+
|
|
51
|
+
%if %mf_nobs(&libds)>0 %then %do;
|
|
52
|
+
%put &sysmacroname: &libds has data, it will not be recreated;
|
|
53
|
+
%return;
|
|
54
|
+
%end;
|
|
55
|
+
|
|
56
|
+
%local ds1 c1 n1;
|
|
57
|
+
%let ds1=%mf_getuniquename(prefix=mp_makedata);
|
|
58
|
+
%let c1=%mf_getuniquename(prefix=mp_makedatacol);
|
|
59
|
+
%let n1=%mf_getuniquename(prefix=mp_makedatacol);
|
|
60
|
+
data &ds1;
|
|
61
|
+
if 0 then set &libds;
|
|
62
|
+
do _n_=1 to &obs;
|
|
63
|
+
&c1=repeat(uuidgen(),10);
|
|
64
|
+
&n1=ranuni(1)*5000000;
|
|
65
|
+
drop &c1 &n1;
|
|
66
|
+
%let charvars=%mf_getvarlist(&libds,typefilter=C);
|
|
67
|
+
%do i=1 %to %sysfunc(countw(&charvars));
|
|
68
|
+
%let col=%scan(&charvars,&i);
|
|
69
|
+
&col=subpad(&c1,1,%mf_getvarlen(&libds,&col));
|
|
70
|
+
%end;
|
|
71
|
+
|
|
72
|
+
%let numvars=%mf_getvarlist(&libds,typefilter=N);
|
|
73
|
+
%do i=1 %to %sysfunc(countw(&numvars));
|
|
74
|
+
%let col=%scan(&numvars,&i);
|
|
75
|
+
&col=&n1;
|
|
76
|
+
%end;
|
|
77
|
+
output;
|
|
78
|
+
end;
|
|
79
|
+
run;
|
|
80
|
+
|
|
81
|
+
proc append base=&libds data=&ds1;
|
|
82
|
+
run;
|
|
83
|
+
|
|
84
|
+
proc sql;
|
|
85
|
+
drop table &ds1;
|
|
86
|
+
|
|
87
|
+
%mend mp_makedata;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Reset when an err condition occurs
|
|
4
|
+
@details When building apps, sometimes an operation must be attempted that
|
|
5
|
+
can cause an err condition. There is no try catch in SAS! So the err state
|
|
6
|
+
must be caught and reset.
|
|
7
|
+
|
|
8
|
+
This macro attempts to do that reset.
|
|
9
|
+
|
|
10
|
+
@version 9.2
|
|
11
|
+
@author Allan Bowe
|
|
12
|
+
|
|
13
|
+
**/
|
|
14
|
+
|
|
15
|
+
%macro mp_reseterror(
|
|
16
|
+
)/*/STORE SOURCE*/;
|
|
17
|
+
|
|
18
|
+
options obs=max replace nosyntaxcheck;
|
|
19
|
+
%let syscc=0;
|
|
20
|
+
|
|
21
|
+
%if "&sysprocessmode " = "SAS Stored Process Server " %then %do;
|
|
22
|
+
data _null_;
|
|
23
|
+
rc=stpsrvset('program error', 0);
|
|
24
|
+
run;
|
|
25
|
+
%end;
|
|
26
|
+
|
|
27
|
+
%mend mp_reseterror;
|
package/base/mp_sortinplace.sas
CHANGED
|
@@ -89,7 +89,7 @@ run;
|
|
|
89
89
|
%let tempvw=%mf_getuniquename(prefix=&sysmacroname);
|
|
90
90
|
proc sql;
|
|
91
91
|
create view work.&tempvw as select * from &lib..&ds
|
|
92
|
-
order by %mf_getquotedstr(&sortkey,quote
|
|
92
|
+
order by %mf_getquotedstr(&sortkey,quote=N);
|
|
93
93
|
|
|
94
94
|
/* append sorted data */
|
|
95
95
|
proc append base=&lib..&tempds2 data=work.&tempvw;
|
package/package.json
CHANGED