@sasjs/core 2.54.4 → 2.56.2

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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2020 (Allan Bowe)
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
- [![npm](https://img.shields.io/npm/dt/@sasjs/core)]()
4
+ ![npm](https://img.shields.io/npm/dt/@sasjs/core)
5
5
  ![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@sasjs/core)
6
6
  [![License](https://img.shields.io/apm/l/atomic-design-ui.svg)](/LICENSE)
7
7
  ![GitHub top language](https://img.shields.io/github/languages/top/sasjs/core)
@@ -31,21 +31,21 @@ Documentation: https://core.sasjs.io
31
31
 
32
32
  ## Components
33
33
 
34
- ### **base** library (SAS9/Viya)
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
- #### **fcmp** library (SAS9/Viya)
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
- ### **meta** library (SAS9 only)
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
- ### **server** library (@sasjs/server only)
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
- ### **viya** library (Viya only)
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
- ### **metax** library (SAS9 only)
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
- ### **lua** library
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
 
@@ -12,6 +12,10 @@
12
12
 
13
13
  @param libds library.dataset
14
14
  @return output returns 1 or 0
15
+
16
+ <h4> Related Macros </h4>
17
+ @li mf_existds.test.sas
18
+
15
19
  @warning Untested on tables registered in metadata but not physically present
16
20
  @version 9.2
17
21
  @author Allan Bowe
@@ -15,12 +15,12 @@
15
15
  for:
16
16
  > "these","words","are","double","quoted"
17
17
 
18
- @param in_str the unquoted, spaced delimited string to transform
19
- @param dlm= the delimeter to be applied to the output (default comma)
20
- @param indlm= the delimeter used for the input (default is space)
21
- @param quote= the quote mark to apply (S=Single, D=Double). If any other value
22
- than uppercase S or D is supplied, then that value will be used as the
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
- %if &quote=S %then %let quote=%str(%');
34
- %else %if &quote=D %then %let quote=%str(%");
35
- %else %let quote=%str();
33
+ /* credit Rowland Hale - byte34 is double quote, 39 is single quote */
34
+ %if &quote=S %then %let quote=%qsysfunc(byte(39));
35
+ %else %if &quote=D %then %let quote=%qsysfunc(byte(34));
36
+ %else %if &quote=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() ) ;
@@ -10,6 +10,9 @@
10
10
  <h4> SAS Macros </h4>
11
11
  @li mf_getattrn.sas
12
12
 
13
+ <h4> Related Macros </h4>
14
+ @li mp_setkeyvalue.sas
15
+
13
16
  @param libds dataset to query
14
17
  @param variable the variable which contains the value to return.
15
18
  @param filter contents of where clause
@@ -8,6 +8,10 @@
8
8
 
9
9
  %mp_assertdsobs(sashelp.class) %* tests if any observations are present;
10
10
 
11
+ %mp_assertdsobs(sashelp.class,test=ATLEAST 10) %* pass if >9 obs present;
12
+
13
+ %mp_assertdsobs(sashelp.class,test=ATMOST 20) %* pass if <21 obs present;
14
+
11
15
  <h4> SAS Macros </h4>
12
16
  @li mf_nobs.sas
13
17
  @li mp_abort.sas
@@ -19,9 +23,9 @@
19
23
  @li HASOBS - Test is a PASS if the input dataset has any observations
20
24
  @li EMPTY - Test is a PASS if input dataset is empty
21
25
  @li EQUALS [integer] - Test passes if row count matches the provided integer
22
- @LI ATLEAST [integer] - Test passes if row count is more than or equal to
26
+ @li ATLEAST [integer] - Test passes if row count is more than or equal to
23
27
  the provided integer
24
- @LI ATMOST [integer] - Test passes if row count is less than or equal to
28
+ @li ATMOST [integer] - Test passes if row count is less than or equal to
25
29
  the provided integer
26
30
  @param [out] outds= (work.test_results) The output dataset to contain the
27
31
  results. If it does not exist, it will be created, with the following format:
@@ -17,8 +17,9 @@
17
17
  @li mf_isblank.sas
18
18
 
19
19
 
20
- @param list space separated list of datasets / views, WITHOUT libref
21
- @param libref= (WORK) Note - you can only drop from a single library at a time
20
+ @param [in] list space separated list of datasets / views, WITHOUT libref
21
+ @param [in] libref= (WORK) Note - you can only drop from one library at a time
22
+ @param [in] iftrue= (1=1) Conditionally drop tables, eg if &debug=N
22
23
 
23
24
  @version 9.2
24
25
  @author Allan Bowe
@@ -28,8 +29,11 @@
28
29
  %macro mp_dropmembers(
29
30
  list /* space separated list of datasets / views */
30
31
  ,libref=WORK /* can only drop from a single library at a time */
32
+ ,iftrue=%str(1=1)
31
33
  )/*/STORE SOURCE*/;
32
34
 
35
+ %if not(%eval(%unquote(&iftrue))) %then %return;
36
+
33
37
  %if %mf_isblank(&list) %then %do;
34
38
  %put NOTE: nothing to drop!;
35
39
  %return;
@@ -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'
@@ -4,24 +4,27 @@
4
4
  @details Useful for capturing constraints before they are dropped / reapplied
5
5
  during an update.
6
6
 
7
- proc sql;
8
- create table work.example(
9
- TX_FROM float format=datetime19.,
10
- DD_TYPE char(16),
11
- DD_SOURCE char(2048),
12
- DD_SHORTDESC char(256),
13
- constraint pk primary key(tx_from, dd_type,dd_source),
14
- constraint unq unique(tx_from, dd_type),
15
- constraint nnn not null(DD_SHORTDESC)
16
- );
7
+ proc sql;
8
+ create table work.example(
9
+ TX_FROM float format=datetime19.,
10
+ DD_TYPE char(16),
11
+ DD_SOURCE char(2048),
12
+ DD_SHORTDESC char(256),
13
+ constraint pk primary key(tx_from, dd_type,dd_source),
14
+ constraint unq unique(tx_from, dd_type),
15
+ constraint nnn not null(DD_SHORTDESC)
16
+ );
17
17
 
18
18
  %mp_getconstraints(lib=work,ds=example,outds=work.constraints)
19
19
 
20
- @param lib= The target library (default=WORK)
21
- @param ds= The target dataset. Leave blank (default) for all datasets.
22
- @param outds the output dataset
20
+ @param [in] lib= (WORK) The target library
21
+ @param [in] ds= The target dataset. Leave blank (default) for all datasets.
22
+ @param [in] mdebug= (0) Set to 1 to preserve temp tables, print var values etc
23
+ @param [out] outds= (mp_getconstraints) the output dataset
23
24
 
24
25
  <h4> SAS Macros </h4>
26
+ @li mf_getuniquename.sas
27
+ @li mp_dropmembers.sas
25
28
 
26
29
  @version 9.2
27
30
  @author Allan Bowe
@@ -31,11 +34,33 @@
31
34
  %macro mp_getconstraints(lib=WORK
32
35
  ,ds=
33
36
  ,outds=mp_getconstraints
37
+ ,mdebug=0
34
38
  )/*/STORE SOURCE*/;
35
39
 
36
40
  %let lib=%upcase(&lib);
37
41
  %let ds=%upcase(&ds);
38
42
 
43
+ /**
44
+ * Neither dictionary tables nor sashelp provides a constraint order column,
45
+ * however they DO arrive in the correct order. So, create the col.
46
+ **/
47
+ %local vw;
48
+ %let vw=%mf_getuniquename(prefix=mp_getconstraints_vw_);
49
+ data &vw /view=&vw;
50
+ set sashelp.vcncolu;
51
+ where TABLE_CATALOG="&lib";
52
+
53
+ /* use retain approach to reset the constraint order with each constraint */
54
+ length tmp $1000;
55
+ retain tmp;
56
+ drop tmp;
57
+ if tmp ne catx('|',libref,table_name,constraint_type,constraint_name) then do;
58
+ constraint_order=1;
59
+ end;
60
+ else constraint_order+1;
61
+ tmp=catx('|',libref, table_name, constraint_type,constraint_name);
62
+ run;
63
+
39
64
  /* must use SQL as proc datasets does not support length changes */
40
65
  proc sql noprint;
41
66
  create table &outds as
@@ -44,8 +69,9 @@ create table &outds as
44
69
  ,a.constraint_type
45
70
  ,a.constraint_name
46
71
  ,b.column_name
72
+ ,b.constraint_order
47
73
  from dictionary.TABLE_CONSTRAINTS a
48
- left join dictionary.constraint_column_usage b
74
+ left join &vw b
49
75
  on upcase(a.TABLE_CATALOG)=upcase(b.TABLE_CATALOG)
50
76
  and upcase(a.TABLE_NAME)=upcase(b.TABLE_NAME)
51
77
  and a.constraint_name=b.constraint_name
@@ -58,6 +84,13 @@ create table &outds as
58
84
  and upcase(a.TABLE_NAME)="&ds"
59
85
  and upcase(b.TABLE_NAME)="&ds"
60
86
  %end;
87
+ order by libref, table_name, constraint_name, constraint_order
61
88
  ;
62
89
 
90
+ /* tidy up */
91
+ %mp_dropmembers(
92
+ &vw,
93
+ iftrue=(&mdebug=0)
94
+ )
95
+
63
96
  %mend mp_getconstraints;
@@ -0,0 +1,259 @@
1
+ /**
2
+ @file
3
+ @brief Extract the primary key fields from a table or library
4
+ @details Examines the constraints to identify primary key fields - indicated
5
+ by an explicit PK constraint, or a unique index that is also NOT NULL.
6
+
7
+ Can be executed at both table and library level. Supports both BASE engine
8
+ libraries and SQL Server.
9
+
10
+ Usage:
11
+
12
+ proc sql;
13
+ create table work.example(
14
+ TX_FROM float format=datetime19.,
15
+ DD_TYPE char(16),
16
+ DD_SOURCE char(2048),
17
+ DD_SHORTDESC char(256),
18
+ constraint pk primary key(tx_from, dd_type,dd_source),
19
+ constraint unq unique(tx_from, dd_type),
20
+ constraint nnn not null(DD_SHORTDESC)
21
+ );
22
+ %mp_getpk(work,ds=example)
23
+
24
+ Returns:
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
+
30
+
31
+ @param [in] lib The libref to examine
32
+ @param [in] ds= (0) Select the dataset to examine, else use 0 for all tables
33
+ @param [in] mdebug= (0) Set to 1 to preserve temp tables, print var values etc
34
+ @param [out] outds= (work.mp_getpk) The name of the output table to create.
35
+
36
+ <h4> SAS Macros </h4>
37
+ @li mf_getengine.sas
38
+ @li mf_getschema.sas
39
+ @li mp_dropmembers.sas
40
+ @li mp_getconstraints.sas
41
+
42
+ <h4> Related Macros </h4>
43
+ @li mp_getpk.test.sas
44
+ @li mp_guesspk.sas
45
+
46
+ @version 9.3
47
+ @author Macro People Ltd
48
+ **/
49
+
50
+ %macro mp_getpk(
51
+ lib,
52
+ ds=0,
53
+ outds=work.mp_getpk,
54
+ mdebug=0
55
+ )/*/STORE SOURCE*/;
56
+
57
+
58
+ %local engine schema ds1 ds2 ds3 dsn tabs1 tabs2 sum pk4sure pkdefault finalpks;
59
+
60
+ %let lib=%upcase(&lib);
61
+ %let ds=%upcase(&ds);
62
+ %let engine=%mf_getengine(&lib);
63
+ %let schema=%mf_getschema(&lib);
64
+
65
+ %let ds1=%mf_getuniquename(prefix=getpk_ds1);
66
+ %let ds2=%mf_getuniquename(prefix=getpk_ds2);
67
+ %let ds3=%mf_getuniquename(prefix=getpk_ds3);
68
+ %let tabs1=%mf_getuniquename(prefix=getpk_tabs1);
69
+ %let tabs2=%mf_getuniquename(prefix=getpk_tabs2);
70
+ %let sum=%mf_getuniquename(prefix=getpk_sum);
71
+ %let pk4sure=%mf_getuniquename(prefix=getpk_pk4sure);
72
+ %let pkdefault=%mf_getuniquename(prefix=getpk_pkdefault);
73
+ %let finalpks=%mf_getuniquename(prefix=getpk_finalpks);
74
+
75
+ %local dbg;
76
+ %if &mdebug=1 %then %do;
77
+ %put &sysmacroname entry vars:;
78
+ %put _local_;
79
+ %end;
80
+ %else %let dbg=*;
81
+
82
+ proc sql;
83
+ create table &ds1 as
84
+ select libname as libref
85
+ ,upcase(memname) as dsn
86
+ ,memtype
87
+ ,upcase(name) as name
88
+ ,type
89
+ ,length
90
+ ,varnum
91
+ ,label
92
+ ,format
93
+ ,idxusage
94
+ ,notnull
95
+ from dictionary.columns
96
+ where upcase(libname)="&lib"
97
+ %if &ds ne 0 %then %do;
98
+ and upcase(memname)="&ds"
99
+ %end;
100
+ ;
101
+
102
+
103
+ %if &engine=SQLSVR %then %do;
104
+ proc sql;
105
+ connect using &lib;
106
+ create table work.&ds2 as
107
+ select * from connection to &lib(
108
+ select
109
+ s.name as SchemaName,
110
+ t.name as memname,
111
+ tc.name as name,
112
+ ic.key_ordinal as KeyOrderNr
113
+ from
114
+ sys.schemas s
115
+ inner join sys.tables t on s.schema_id=t.schema_id
116
+ inner join sys.indexes i on t.object_id=i.object_id
117
+ inner join sys.index_columns ic on i.object_id=ic.object_id
118
+ and i.index_id=ic.index_id
119
+ inner join sys.columns tc on ic.object_id=tc.object_id
120
+ and ic.column_id=tc.column_id
121
+ where i.is_primary_key=1
122
+ and s.name=%str(%')&schema%str(%')
123
+ order by t.name, ic.key_ordinal ;
124
+ );disconnect from &lib;
125
+ create table &ds3 as
126
+ select a.*
127
+ ,case when b.name is not null then 1 else 0 end as pk_ind
128
+ from work.&ds1 a
129
+ left join work.&ds2 b
130
+ on a.dsn=b.memname
131
+ and upcase(a.name)=upcase(b.name)
132
+ order by libref,dsn;
133
+ %end;
134
+ %else %do;
135
+
136
+ %if &ds = 0 %then %let dsn=;
137
+
138
+ /* get all constraints, in constraint order*/
139
+ %mp_getconstraints(lib=&lib,ds=&dsn,outds=work.&ds2)
140
+
141
+ /* extract cols that are clearly primary keys */
142
+ proc sql;
143
+ create table &pk4sure as
144
+ select libref
145
+ ,table_name
146
+ ,constraint_name
147
+ ,constraint_order
148
+ ,column_name as name
149
+ from work.&ds2
150
+ where constraint_type='PRIMARY'
151
+ order by 1,2,3,4;
152
+
153
+ /* extract unique constraints where every col is also NOT NULL */
154
+ proc sql;
155
+ create table &sum as
156
+ select a.libref
157
+ ,a.table_name
158
+ ,a.constraint_name
159
+ ,count(a.column_name) as unq_cnt
160
+ ,count(b.column_name) as nul_cnt
161
+ from work.&ds2(where=(constraint_type ='UNIQUE')) a
162
+ left join work.&ds2(where=(constraint_type ='NOT NULL')) b
163
+ on a.libref=b.libref
164
+ and a.table_name=b.table_name
165
+ and a.column_name=b.column_name
166
+ group by 1,2,3
167
+ having unq_cnt=nul_cnt;
168
+
169
+ /* extract cols from the relevant unique constraints */
170
+ create table &pkdefault as
171
+ select a.libref
172
+ ,a.table_name
173
+ ,a.constraint_name
174
+ ,b.constraint_order
175
+ ,b.column_name as name
176
+ from &sum a
177
+ left join &ds2(where=(constraint_type ='UNIQUE')) b
178
+ on a.libref=b.libref
179
+ and a.table_name=b.table_name
180
+ and a.constraint_name=b.constraint_name
181
+ order by 1,2,3,4;
182
+
183
+ /* create one table */
184
+ data &finalpks;
185
+ set &pkdefault &pk4sure ;
186
+ pk_ind=1;
187
+ /* if there are multiple unique constraints, take the first */
188
+ by libref table_name constraint_name;
189
+ retain keepme;
190
+ if first.table_name then keepme=1;
191
+ if first.constraint_name and not first.table_name then keepme=0;
192
+ if keepme=1;
193
+ run;
194
+
195
+ /* join back to starting table */
196
+ proc sql;
197
+ create table &ds3 as
198
+ select a.*
199
+ ,b.constraint_order
200
+ ,case when b.pk_ind=1 then 1 else 0 end as pk_ind
201
+ from work.&ds1 a
202
+ left join work.&finalpks b
203
+ on a.libref=b.libref
204
+ and a.dsn=b.table_name
205
+ and upcase(a.name)=upcase(b.name)
206
+ order by libref,dsn,constraint_order;
207
+ %end;
208
+
209
+
210
+ /* prepare tables */
211
+ proc sql;
212
+ create table work.&tabs1 as select
213
+ libname as libref
214
+ ,upcase(memname) as dsn
215
+ ,memtype
216
+ ,dbms_memtype
217
+ ,typemem
218
+ ,memlabel
219
+ ,nvar
220
+ ,compress
221
+ from dictionary.tables
222
+ where upcase(libname)="&lib"
223
+ %if &ds ne 0 %then %do;
224
+ and upcase(memname)="&ds"
225
+ %end;
226
+ ;
227
+ data &tabs2;
228
+ set &ds3;
229
+ length pk_fields $512;
230
+ retain pk_fields;
231
+ by libref dsn constraint_order;
232
+ if first.dsn then pk_fields='';
233
+ if pk_ind=1 then pk_fields=catx(' ',pk_fields,name);
234
+ if last.dsn then output;
235
+ run;
236
+
237
+ proc sql;
238
+ create table &outds as
239
+ select a.libref
240
+ ,a.dsn
241
+ ,a.memtype
242
+ ,a.dbms_memtype
243
+ ,a.typemem
244
+ ,a.memlabel
245
+ ,a.nvar
246
+ ,a.compress
247
+ ,b.pk_fields
248
+ from work.&tabs1 a
249
+ left join work.&tabs2 b
250
+ on a.libref=b.libref
251
+ and a.dsn=b.dsn;
252
+
253
+ /* tidy up */
254
+ %mp_dropmembers(
255
+ &ds1 &ds2 &ds3 &dsn &tabs1 &tabs2 &sum &pk4sure &pkdefault &finalpks,
256
+ iftrue=(&mdebug=0)
257
+ )
258
+
259
+ %mend mp_getpk;
@@ -1,32 +1,37 @@
1
1
  /**
2
- @file mp_guesspk.sas
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 logic:
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 include composite keys of 2 to 6 columns
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 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
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. A
21
- table is likely to have multiple unlikely PKs, so no need to list them all. Default=3.
22
- @param min_rows= The minimum number of rows a table should have in order to try
23
- and guess the PK. Default=5.
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) out=&tmpds noduprec;
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) out=&tmpds noduprec;
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 values("&lev1 &lev2 &lev3 &lev4 &lev5 &lev6");
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;
@@ -21,6 +21,7 @@
21
21
 
22
22
  @param [in] libds dataset to hash
23
23
  @param [in] salt= Provide a salt (could be, for instance, the dataset name)
24
+ @param [in] iftrue= A condition under which the macro should be executed.
24
25
  @param [out] outds= (work.mf_hashdataset) The output dataset to create. This
25
26
  will contain one column (hashkey) with one observation (a hex32.
26
27
  representation of the input hash)
@@ -35,10 +36,14 @@
35
36
  %macro mp_hashdataset(
36
37
  libds,
37
38
  outds=,
38
- salt=
39
+ salt=,
40
+ iftrue=%str(1=1)
39
41
  )/*/STORE SOURCE*/;
42
+
43
+ %if not(%eval(%unquote(&iftrue))) %then %return;
44
+
40
45
  %if %mf_getattrn(&libds,NLOBS)=0 %then %do;
41
- %put %str(WARN)ING: Dataset &libds is empty;, or is not a dataset;
46
+ %put %str(WARN)ING: Dataset &libds is empty, or is not a dataset;
42
47
  %end;
43
48
  %else %if %mf_getattrn(&libds,NLOBS)<0 %then %do;
44
49
  %put %str(ERR)OR: Dataset &libds is not a dataset;
@@ -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 err when a format cannot be found */
44
+ mergenoby=%str(ERR)OR /* Throw err when a merge has no BY variables */
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=%str(ERR)OR /* 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;
@@ -9,11 +9,14 @@
9
9
  <h4> SAS Macros </h4>
10
10
  @li mf_existds.sas
11
11
 
12
- @param key Provide a key on which to perform the lookup
13
- @param value Provide a value
14
- @param type= either C or N will populate valc and valn respectively. C is
15
- default.
16
- @param libds= define the target table to hold the parameters
12
+ <h4> Related Macros </h4>
13
+ @li mf_getvalue.sas
14
+
15
+ @param [in] key Provide a key on which to perform the lookup
16
+ @param [in] value Provide a value
17
+ @param [in] type= either C or N will populate valc and valn respectively.
18
+ C is default.
19
+ @param [out] libds= define the target table to hold the parameters
17
20
 
18
21
  @version 9.2
19
22
  @author Allan Bowe
@@ -0,0 +1,118 @@
1
+ /**
2
+ @file
3
+ @brief Sorts a SAS dataset in place, preserving constraints
4
+ @details Generally if a dataset contains indexes, then it is not necessary to
5
+ sort it before performing operations such as merges / joins etc.
6
+ That said, there are a few edge cases where it can be desirable:
7
+
8
+ @li To improve performance for particular scenarios
9
+ @li To allow adjacent records to be viewed directly in the dataset
10
+ @li To reduce dataset size (eg when there are deleted records)
11
+
12
+ This macro will only work for BASE (V9) engine libraries. It works by
13
+ creating a copy of the dataset (without data, WITH constraints) in the same
14
+ library, appending a sorted view into it, and finally - renaming it.
15
+
16
+ Example usage:
17
+
18
+ proc sql;
19
+ create table work.example as
20
+ select * from sashelp.class;
21
+ alter table work.example
22
+ add constraint pk primary key(name);
23
+ %mp_sortinplace(work.example)
24
+
25
+ @param [in] libds The libref.datasetname that needs to be sorted
26
+
27
+ <h4> SAS Macros </h4>
28
+ @li mf_existds.sas
29
+ @li mf_getengine.sas
30
+ @li mf_getquotedstr.sas
31
+ @li mf_getuniquename.sas
32
+ @li mf_nobs.sas
33
+ @li mp_abort.sas
34
+ @li mp_getpk.sas
35
+
36
+ <h4> Related Macros </h4>
37
+ @li mp_sortinplace.test.sas
38
+
39
+ @version 9.2
40
+ @author Allan Bowe
41
+ @source https://github.com/sasjs/core
42
+
43
+ **/
44
+
45
+ %macro mp_sortinplace(libds
46
+ )/*/STORE SOURCE*/;
47
+
48
+ %local lib ds tempds1 tempds2 tempvw sortkey;
49
+
50
+ /* perform validations */
51
+ %mp_abort(iftrue=(%sysfunc(countc(&libds,.)) ne 1)
52
+ ,mac=mp_sortinplace
53
+ ,msg=%str(LIBDS (&libds) should have LIBREF.DATASET format)
54
+ )
55
+ %mp_abort(iftrue=(%mf_existds(&libds)=0)
56
+ ,mac=mp_sortinplace
57
+ ,msg=%str(&libds does not exist)
58
+ )
59
+
60
+ %let lib=%scan(&libds,1,.);
61
+ %let ds=%scan(&libds,2,.);
62
+ %mp_abort(iftrue=(%mf_getengine(&lib) ne V9)
63
+ ,mac=mp_sortinplace
64
+ ,msg=%str(&lib is not a BASE engine library)
65
+ )
66
+
67
+ /* grab a copy of the constraints so we know what to sort by */
68
+ %let tempds1=%mf_getuniquename(prefix=&sysmacroname);
69
+ %mp_getpk(lib=&lib,ds=&ds,outds=work.&tempds1)
70
+
71
+ %if %mf_nobs(work.&tempds1)=0 %then %do;
72
+ %put &sysmacroname: No PK found in &lib..&ds;
73
+ %put Sorting will not take place;
74
+ %return;
75
+ %end;
76
+
77
+ data _null_;
78
+ set work.&tempds1;
79
+ call symputx('sortkey',pk_fields);
80
+ run;
81
+
82
+
83
+ /* create empty copy, with ALL constraints, in the same library */
84
+ %let tempds2=%mf_getuniquename(prefix=&sysmacroname);
85
+ proc append base=&lib..&tempds2 data=&libds(obs=0);
86
+ run;
87
+
88
+ /* create sorted view */
89
+ %let tempvw=%mf_getuniquename(prefix=&sysmacroname);
90
+ proc sql;
91
+ create view work.&tempvw as select * from &lib..&ds
92
+ order by %mf_getquotedstr(&sortkey,quote=N);
93
+
94
+ /* append sorted data */
95
+ proc append base=&lib..&tempds2 data=work.&tempvw;
96
+ run;
97
+
98
+ /* do validations */
99
+ %mp_abort(iftrue=(&syscc ne 0)
100
+ ,mac=mp_sortinplace
101
+ ,msg=%str(syscc=&syscc prior to replace operation)
102
+ )
103
+ %mp_abort(iftrue=(%mf_nobs(&lib..&tempds2) ne %mf_nobs(&lib..&ds))
104
+ ,mac=mp_sortinplace
105
+ ,msg=%str(new dataset has a different number of logical obs to the old)
106
+ )
107
+
108
+ /* drop old dataset */
109
+ proc sql;
110
+ drop table &lib..&ds;
111
+
112
+ /* rename the new dataset */
113
+ proc datasets library=&lib;
114
+ change &tempds2=&ds;
115
+ run;
116
+
117
+
118
+ %mend mp_sortinplace;
package/base/mp_unzip.sas CHANGED
@@ -7,19 +7,23 @@
7
7
 
8
8
  Usage:
9
9
 
10
- filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
10
+ filename mc url
11
+ "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
11
12
  %inc mc;
12
13
 
13
14
  %mp_unzip(ziploc="/some/file.zip",outdir=/some/folder)
14
15
 
15
- <h4> SAS Macros </h4>
16
- @li mf_mkdir.sas
17
- @li mf_getuniquefileref.sas
16
+ More info: https://blogs.sas.com/content/sasdummy/2015/05/11/using-filename-zip-to-unzip-and-read-data-files-in-sas/
18
17
 
19
18
  @param ziploc= Fileref or quoted full path to zip file ("/path/to/file.zip")
20
19
  @param outdir= (%sysfunc(pathname(work))) Directory in which to write the
21
20
  outputs (created if non existant)
22
21
 
22
+ <h4> SAS Macros </h4>
23
+ @li mf_mkdir.sas
24
+ @li mf_getuniquefileref.sas
25
+ @li mp_binarycopy.sas
26
+
23
27
  @version 9.4
24
28
  @author Allan Bowe
25
29
  @source https://github.com/sasjs/core
@@ -31,18 +35,20 @@
31
35
  ,outdir=%sysfunc(pathname(work))
32
36
  )/*/STORE SOURCE*/;
33
37
 
34
- %local fname1 fname2 fname3;
35
- %let fname1=%mf_getuniquefileref();
36
- %let fname2=%mf_getuniquefileref();
37
- %let fname3=%mf_getuniquefileref();
38
+ %local f1 f2 ;
39
+ %let f1=%mf_getuniquefileref();
40
+ %let f2=%mf_getuniquefileref();
38
41
 
39
42
  /* Macro variable &datazip would be read from the file */
40
- filename &fname1 ZIP &ziploc;
43
+ filename &f1 ZIP &ziploc;
44
+
45
+ /* create target folder */
46
+ %mf_mkdir(&outdir)
41
47
 
42
48
  /* Read the "members" (files) from the ZIP file */
43
49
  data _data_(keep=memname isFolder);
44
50
  length memname $200 isFolder 8;
45
- fid=dopen("&fname1");
51
+ fid=dopen("&f1");
46
52
  if fid=0 then stop;
47
53
  memcount=dnum(fid);
48
54
  do i=1 to memcount;
@@ -53,16 +59,32 @@ data _data_(keep=memname isFolder);
53
59
  end;
54
60
  rc=dclose(fid);
55
61
  run;
56
- filename &fname1 clear;
62
+
63
+ filename &f2 temp;
57
64
 
58
65
  /* loop through each entry and either create the subfolder or extract member */
59
66
  data _null_;
60
67
  set &syslast;
68
+ file &f2;
61
69
  if isFolder then call execute('%mf_mkdir(&outdir/'!!memname!!')');
62
- else call execute('filename &fname2 zip &ziploc member='
63
- !!quote(trim(memname))!!';filename &fname3 "&outdir/'
64
- !!trim(memname)!!'" recfm=n;data _null_; rc=fcopy("&fname2","&fname3");run;'
65
- !!'filename &fname2 clear; filename &fname3 clear;');
70
+ else do;
71
+ qname=quote(cats("&outdir/",memname));
72
+ bname=cats('(',memname,')');
73
+ put '/* hat tip: "data _null_" on SAS-L */';
74
+ put 'data _null_;';
75
+ put ' infile &f1 ' bname ' lrecl=256 recfm=F length=length eof=eof unbuf;';
76
+ put ' file ' qname ' lrecl=256 recfm=N;';
77
+ put ' input;';
78
+ put ' put _infile_ $varying256. length;';
79
+ put ' return;';
80
+ put 'eof:';
81
+ put ' stop;';
82
+ put 'run;';
83
+ end;
66
84
  run;
67
85
 
68
- %mend mp_unzip;
86
+ %inc &f2/source2;
87
+
88
+ filename &f2 clear;
89
+
90
+ %mend mp_unzip;
package/package.json CHANGED
@@ -35,5 +35,8 @@
35
35
  "devDependencies": {
36
36
  "@sasjs/cli": "^2.39.0"
37
37
  },
38
- "version": "2.54.4"
38
+ "dependencies": {
39
+ "ts-loader": "^9.2.6"
40
+ },
41
+ "version": "2.56.2"
39
42
  }