@sasjs/core 3.11.0 → 4.1.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/README.md CHANGED
@@ -125,6 +125,7 @@ filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
125
125
  - macro names must be lowercase
126
126
  - one macro per file
127
127
  - prefixes:
128
+ - _mcf_ for macro compiled functions (proc fcmp)
128
129
  - _mf_ for macro functions (can be used in open code).
129
130
  - _ml_ for macros that are used to compile LUA modules
130
131
  - _mm_ for metadata macros (interface with the metadata server).
@@ -166,7 +167,7 @@ SAS code can contain one of two types of dependency - SAS Macros, and SAS Includ
166
167
  @li someprogram.sas FREFTWO
167
168
  ```
168
169
 
169
- The CLI can then extract all the dependencies and insert as precode (SAS Macros) or in a temp engine fileref (SAS Includes) when creating SAS Jobs and Services.
170
+ The CLI can then extract all the dependencies and insert as precode (SAS Macros) or in a temp engine fileref (SAS Includes) when creating SAS Jobs and Services (and Tests).
170
171
 
171
172
  When contributing to this library, it is therefore important to ensure that all dependencies are listed in the header in this format.
172
173
 
@@ -182,7 +183,9 @@ When contributing to this library, it is therefore important to ensure that all
182
183
  - Mandatory parameters should be positional, all optional parameters should be keyword (var=) style.
183
184
  - All dataset references must be 2 level (eg `work.blah`, not `blah`). This is to avoid contention when options [DATASTMTCHK](https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000279064.htm)=ALLKEYWORDS is in effect.
184
185
  - Avoid naming collisions! All macro variables should be local scope. Use system generated work tables where possible - eg `data ; set sashelp.class; run; data &output; set &syslast; run;`
186
+ - Where global macro variables are absolutely necessary, they should make use of `&sasjs_prefix` - see mp_init.sas
185
187
  - The use of `quit;` for `proc sql` is optional unless you are looking to benefit from the timing statistics.
188
+ - Use [sasjs lint](https://github.com/sasjs/lint)!
186
189
 
187
190
  ## General Notes
188
191
 
@@ -190,10 +193,9 @@ When contributing to this library, it is therefore important to ensure that all
190
193
 
191
194
  ## Breaking Changes
192
195
 
193
- We are currently on major release v3. The following changes are planned when the next major (breaking) release becomes necessary:
196
+ We are currently on major release v4. Breaking changes should be marked with the [deprecated](https://www.doxygen.nl/manual/commands.html#cmddeprecated) doxygen tag. The following changes are planned when the next major (breaking) release becomes necessary:
194
197
 
195
- * Remove `dbg` parameter from mp_jsonout.sas (implement mdebug instead)
196
- * Remove `END_DTTM` and `START_DTTM` from mx_webout JSON
198
+ * (None as yet)
197
199
 
198
200
  ## Star Gazing
199
201
 
package/base/mf_abort.sas CHANGED
@@ -11,7 +11,7 @@
11
11
  @cond
12
12
  **/
13
13
 
14
- %macro mf_abort(mac=mf_abort.sas, type=deprecated, msg=, iftrue=%str(1=1)
14
+ %macro mf_abort(mac=mf_abort.sas, msg=, iftrue=%str(1=1)
15
15
  )/*/STORE SOURCE*/;
16
16
 
17
17
  %if not(%eval(%unquote(&iftrue))) %then %return;
package/base/mf_dedup.sas CHANGED
@@ -23,7 +23,6 @@
23
23
 
24
24
  <h4> Related Macros </h4>
25
25
  @li mf_trimstr.sas
26
- @li mf_wordsinstr1butnotstr2.sas
27
26
 
28
27
  @version 9.2
29
28
  @author Allan Bowe
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  @file
3
3
  @brief Checks if a variable exists in a data set.
4
- @details Returns 0 if the variable does NOT exist, and return the position of
5
- the var if it does.
6
- Usage:
4
+ @details Returns 0 if the variable does NOT exist, and the position of the var
5
+ if it does.
6
+ Usage:
7
7
 
8
- %put %mf_existvar(work.someds, somevar)
8
+ %put %mf_existvar(work.someds, somevar)
9
9
 
10
10
  @param [in] libds 2 part dataset or view reference
11
11
  @param [in] var variable name
@@ -5,18 +5,19 @@
5
5
 
6
6
  %put %mf_getfilesize(fpath=C:\temp\myfile.txt);
7
7
 
8
- or
8
+ or, provide a libds value as follows:
9
9
 
10
10
  data x;do x=1 to 100000;y=x;output;end;run;
11
11
  %put %mf_getfilesize(libds=work.x,format=yes);
12
12
 
13
- gives:
13
+ Which gives:
14
14
 
15
- 2mb
15
+ > 2mb
16
+
17
+ @param [in] fpath= Full path and filename. Provide this OR the libds value.
18
+ @param [in] libds= (0) Library.dataset value (assumes library is BASE engine)
19
+ @param [in] format= (NO) Set to yes to apply sizekmg. format
16
20
 
17
- @param fpath= full path and filename. Provide this OR the libds value.
18
- @param libds= library.dataset value (assumes library is BASE engine)
19
- @param format= set to yes to apply sizekmg. format
20
21
  @returns bytes
21
22
 
22
23
  @version 9.2
@@ -26,16 +27,32 @@
26
27
  %macro mf_getfilesize(fpath=,libds=0,format=NO
27
28
  )/*/STORE SOURCE*/;
28
29
 
30
+ %local rc fid fref bytes dsid lib vnum;
31
+
29
32
  %if &libds ne 0 %then %do;
30
- %let fpath=%sysfunc(pathname(%scan(&libds,1,.)))/%scan(&libds,2,.).sas7bdat;
33
+ %let libds=%upcase(&libds);
34
+ %if %index(&libds,.)=0 %then %let lib=WORK;
35
+ %else %let lib=%scan(&libds,1,.);
36
+ %let dsid=%sysfunc(open(
37
+ sashelp.vtable(where=(libname="&lib" and memname="%scan(&libds,-1,.)")
38
+ keep=libname memname filesize
39
+ )
40
+ ));
41
+ %if (&dsid ^= 0) %then %do;
42
+ %let vnum=%sysfunc(varnum(&dsid,FILESIZE));
43
+ %let rc=%sysfunc(fetch(&dsid));
44
+ %let bytes=%sysfunc(getvarn(&dsid,&vnum));
45
+ %let rc= %sysfunc(close(&dsid));
46
+ %end;
47
+ %else %put &sysmacroname: &libds could not be opened! %sysfunc(sysmsg());
48
+ %end;
49
+ %else %do;
50
+ %let rc=%sysfunc(filename(fref,&fpath));
51
+ %let fid=%sysfunc(fopen(&fref));
52
+ %let bytes=%sysfunc(finfo(&fid,File Size (bytes)));
53
+ %let rc=%sysfunc(fclose(&fid));
54
+ %let rc=%sysfunc(filename(fref));
31
55
  %end;
32
-
33
- %local rc fid fref bytes;
34
- %let rc=%sysfunc(filename(fref,&fpath));
35
- %let fid=%sysfunc(fopen(&fref));
36
- %let bytes=%sysfunc(finfo(&fid,File Size (bytes)));
37
- %let rc=%sysfunc(fclose(&fid));
38
- %let rc=%sysfunc(filename(fref));
39
56
 
40
57
  %if &format=NO %then %do;
41
58
  &bytes
@@ -30,7 +30,6 @@
30
30
 
31
31
  %local count_base count_extr i i2 extr_word base_word match outvar;
32
32
  %if %length(&str1)=0 or %length(&str2)=0 %then %do;
33
- %put %str(WARN)ING: empty string provided!;
34
33
  %put base string (str1)= &str1;
35
34
  %put compare string (str2) = &str2;
36
35
  %return;
@@ -3,6 +3,9 @@
3
3
  @brief Returns words that are in string 1 but not in string 2
4
4
  @details Compares two space separated strings and returns the words that are
5
5
  in the first but not in the second.
6
+
7
+ Note - case sensitive!
8
+
6
9
  Usage:
7
10
 
8
11
  %let x= %mf_wordsInStr1ButNotStr2(
@@ -13,10 +16,8 @@
13
16
  returns:
14
17
  > sss bram boo
15
18
 
16
- @param str1= string containing words to extract
17
- @param str2= used to compare with the extract string
18
-
19
- @warning CASE SENSITIVE!
19
+ @param [in] str1= string containing words to extract
20
+ @param [in] str2= used to compare with the extract string
20
21
 
21
22
  @version 9.2
22
23
  @author Allan Bowe
@@ -30,7 +31,6 @@
30
31
 
31
32
  %local count_base count_extr i i2 extr_word base_word match outvar;
32
33
  %if %length(&str1)=0 or %length(&str2)=0 %then %do;
33
- %put %str(WARN)ING: empty string provided!;
34
34
  %put base string (str1)= &str1;
35
35
  %put compare string (str2) = &str2;
36
36
  %return;
package/base/mp_abort.sas CHANGED
@@ -173,7 +173,8 @@
173
173
  if symexist('_debug') then debug=quote(trim(symget('_debug')));
174
174
  else debug='""';
175
175
  put '>>weboutBEGIN<<';
176
- put '{"START_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '"';
176
+ put '{"SYSDATE" : "' "&SYSDATE" '"';
177
+ put ',"SYSTIME" : "' "&SYSTIME" '"';
177
178
  put ',"sasjsAbort" : [{';
178
179
  put ' "MSG":' msg ;
179
180
  put ' ,"MAC": "' "&mac" '"}]';
@@ -202,7 +203,7 @@
202
203
  put ',"SYSVLONG" : ' sysvlong;
203
204
  syswarningtext=quote(trim(symget('syswarningtext')));
204
205
  put ",""SYSWARNINGTEXT"" : " syswarningtext;
205
- put ',"END_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '" ';
206
+ put ',"END_DTTM" : "' "%sysfunc(datetime(),E8601DT26.6)" '" ';
206
207
  put "}" @;
207
208
  put '>>weboutEND<<';
208
209
  run;
@@ -12,10 +12,6 @@
12
12
 
13
13
  %mp_assertdsobs(sashelp.class,test=ATMOST 20) %* pass if <21 obs present;
14
14
 
15
- <h4> SAS Macros </h4>
16
- @li mf_nobs.sas
17
- @li mp_abort.sas
18
-
19
15
 
20
16
  @param [in] inds input dataset to test for presence of observations
21
17
  @param [in] desc= (Testing observations) The user provided test description
@@ -33,6 +29,11 @@
33
29
  |---|---|---|
34
30
  |User Provided description|PASS|Dataset &inds has XX obs|
35
31
 
32
+ <h4> SAS Macros </h4>
33
+ @li mf_getuniquename.sas
34
+ @li mf_nobs.sas
35
+ @li mp_abort.sas
36
+
36
37
  <h4> Related Macros </h4>
37
38
  @li mp_assertcolvals.sas
38
39
  @li mp_assert.sas
@@ -49,9 +50,10 @@
49
50
  outds=work.test_results
50
51
  )/*/STORE SOURCE*/;
51
52
 
52
- %local nobs;
53
+ %local nobs ds;
53
54
  %let nobs=%mf_nobs(&inds);
54
55
  %let test=%upcase(&test);
56
+ %let ds=%mf_getuniquename(prefix=mp_assertdsobs);
55
57
 
56
58
  %if %substr(&test.xxxxx,1,6)=EQUALS %then %do;
57
59
  %let val=%scan(&test,2,%str( ));
@@ -84,7 +86,7 @@
84
86
  )
85
87
  %end;
86
88
 
87
- data;
89
+ data &ds;
88
90
  length test_description $256 test_result $4 test_comments $256;
89
91
  test_description=symget('desc');
90
92
  test_result='FAIL';
@@ -110,9 +112,6 @@
110
112
  %end;
111
113
  run;
112
114
 
113
- %local ds;
114
- %let ds=&syslast;
115
-
116
115
  proc append base=&outds data=&ds;
117
116
  run;
118
117
 
@@ -1,18 +1,25 @@
1
1
  /**
2
2
  @file
3
3
  @brief Used to capture scope leakage of macro variables
4
- @details A common 'difficult to detect' bug in macros is where a nested
5
- macro over-writes variables in a higher level macro.
4
+ @details
6
5
 
7
- This assertion takes a snapshot of the macro variables before and after
8
- a macro invocation. This makes it easy to detect whether any macro
9
- variables were modified or changed.
6
+ A common 'difficult to detect' bug in macros is where a nested macro
7
+ over-writes variables in a higher level macro.
10
8
 
11
- Currently, the macro only checks for global scope variables. In the future
12
- it may be extended to work at multiple levels of nesting.
9
+ This assertion takes a snapshot of the macro variables before and after
10
+ a macro invocation. Differences are captured in the `&outds` table. This
11
+ makes it easy to detect whether any macro variables were modified or
12
+ changed.
13
13
 
14
- If you would like this feature, feel free to contribute / raise an issue /
15
- engage the SASjs team directly.
14
+ The following variables are NOT tested (as they are known, global variables
15
+ used in SASjs):
16
+
17
+ @li &sasjs_prefix._FUNCTIONS
18
+
19
+ Global variables are initialised in mp_init.sas - which will also trigger
20
+ "strict mode" in your SAS session. Whilst this is a default in SASjs
21
+ produced apps, if you prefer not to use this mode, simply instantiate the
22
+ following variable to prevent the macro from running: `SASJS_PREFIX`
16
23
 
17
24
  Example usage:
18
25
 
@@ -24,12 +31,17 @@
24
31
  desc=Checking macro variables against previous snapshot
25
32
  )
26
33
 
34
+ This macro is designed to work alongside `sasjs test` - for more information
35
+ about this facility, visit [cli.sasjs.io/test](https://cli.sasjs.io/test).
36
+
27
37
  @param [in] action (SNAPSHOT) The action to take. Valid values:
28
38
  @li SNAPSHOT - take a copy of the current macro variables
29
39
  @li COMPARE - compare the current macro variables against previous values
30
40
  @param [in] scope= (GLOBAL) The scope of the variables to be checked. This
31
41
  corresponds to the values in the SCOPE column in `sashelp.vmacro`.
32
- @param [in] desc= (Testing variable scope) The user provided test description
42
+ @param [in] desc= (Testing scope leakage) The user provided test description
43
+ @param [in] ignorelist= Provide a list of macro variable names to ignore from
44
+ the comparison
33
45
  @param [in,out] scopeds= (work.mp_assertscope) The dataset to contain the
34
46
  scope snapshot
35
47
  @param [out] outds= (work.test_results) The output dataset to contain the
@@ -38,6 +50,10 @@
38
50
  |---|---|---|
39
51
  |User Provided description|PASS|No out of scope variables created or modified|
40
52
 
53
+ <h4> SAS Macros </h4>
54
+ @li mf_getquotedstr.sas
55
+ @li mp_init.sas
56
+
41
57
  <h4> Related Macros </h4>
42
58
  @li mp_assert.sas
43
59
  @li mp_assertcols.sas
@@ -51,12 +67,21 @@
51
67
  **/
52
68
 
53
69
  %macro mp_assertscope(action,
54
- desc=0,
70
+ desc=Testing Scope Leakage,
55
71
  scope=GLOBAL,
56
72
  scopeds=work.mp_assertscope,
73
+ ignorelist=,
57
74
  outds=work.test_results
58
75
  )/*/STORE SOURCE*/;
59
- %local ds test_result test_comments del add mod;
76
+ %local ds test_result test_comments del add mod ilist;
77
+ %let ilist=%upcase(&sasjs_prefix._FUNCTIONS &ignorelist);
78
+
79
+ /**
80
+ * this sets up the global vars, it will also enter STRICT mode. If this
81
+ * behaviour is not desired, simply initiate the following global macro
82
+ * variable to prevent the macro from running: SASJS_PREFIX
83
+ */
84
+ %mp_init()
60
85
 
61
86
  /* get current variables */
62
87
  %if &action=SNAPSHOT %then %do;
@@ -64,7 +89,7 @@
64
89
  create table &scopeds as
65
90
  select name,offset,value
66
91
  from dictionary.macros
67
- where scope="&scope"
92
+ where scope="&scope" and name not in (%mf_getquotedstr(&ilist))
68
93
  order by name,offset;
69
94
  %end;
70
95
  %else %if &action=COMPARE %then %do;
@@ -73,7 +98,7 @@
73
98
  create table _data_ as
74
99
  select name,offset,value
75
100
  from dictionary.macros
76
- where scope="&scope"
101
+ where scope="&scope" and name not in (%mf_getquotedstr(&ilist))
77
102
  order by name,offset;
78
103
 
79
104
  %let ds=&syslast;
@@ -10,6 +10,8 @@
10
10
  %mp_coretable(LOCKTABLE,libds=work.locktable)
11
11
 
12
12
  @param [in] table_ref The type of table to create. Example values:
13
+ @li DIFFTABLE - Used to store changes to tables. Used by mp_storediffs.sas
14
+ and mp_stackdiffs.sas
13
15
  @li FILTER_DETAIL - For storing detailed filter values. Used by
14
16
  mp_filterstore.sas.
15
17
  @li FILTER_SUMMARY - For storing summary filter values. Used by
@@ -25,6 +27,8 @@
25
27
  @li mp_filterstore.sas
26
28
  @li mp_lockanytable.sas
27
29
  @li mp_retainedkey.sas
30
+ @li mp_storediffs.sas
31
+ @li mp_stackdiffs.sas
28
32
 
29
33
  @version 9.2
30
34
  @author Allan Bowe
@@ -36,7 +40,29 @@
36
40
  %local outds ;
37
41
  %let outds=%sysfunc(ifc(&libds=0,_data_,&libds));
38
42
  proc sql;
39
- %if &table_ref=LOCKTABLE %then %do;
43
+ %if &table_ref=DIFFTABLE %then %do;
44
+ create table &outds(
45
+ load_ref char(36) label='unique load reference',
46
+ processed_dttm num format=E8601DT26.6 label='Processed at timestamp',
47
+ libref char(8) label='Library Reference (8 chars)',
48
+ dsn char(32) label='Dataset Name (32 chars)',
49
+ key_hash char(32) label=
50
+ 'MD5 Hash of primary key values (pipe seperated)',
51
+ move_type char(1) label='Either (A)ppended, (D)eleted or (M)odified',
52
+ is_pk num label='Is Primary Key Field? (1/0)',
53
+ is_diff num label=
54
+ 'Did value change? (1/0/-1). Always -1 for appends and deletes.',
55
+ tgtvar_type char(1) label='Either (C)haracter or (N)umeric',
56
+ tgtvar_nm char(32) label='Target variable name (32 chars)',
57
+ oldval_num num format=best32. label='Old (numeric) value',
58
+ newval_num num format=best32. label='New (numeric) value',
59
+ oldval_char char(32765) label='Old (character) value',
60
+ newval_char char(32765) label='New (character) value',
61
+ constraint pk_mpe_audit
62
+ primary key(load_ref,libref,dsn,key_hash,tgtvar_nm)
63
+ );
64
+ %end;
65
+ %else %if &table_ref=LOCKTABLE %then %do;
40
66
  create table &outds(
41
67
  lock_lib char(8),
42
68
  lock_ds char(32),
@@ -49,10 +49,6 @@
49
49
  ,mac=&sysmacroname
50
50
  ,msg=%str(the BASEDS variable must be provided)
51
51
  )
52
- %mp_abort(iftrue=( &baseds=0 )
53
- ,mac=&sysmacroname
54
- ,msg=%str(the BASEDS variable must be provided)
55
- )
56
52
  %mp_abort(iftrue=( %mf_existds(&baseds)=0 )
57
53
  ,mac=&sysmacroname
58
54
  ,msg=%str(the BASEDS dataset (&baseds) needs to be assigned, and to exist)
@@ -0,0 +1,120 @@
1
+ /**
2
+ @file
3
+ @brief Create a smaller version of a dataset, without data loss
4
+ @details This macro will scan the input dataset and create a new one, that
5
+ has the minimum variable lengths needed to store the data without data loss.
6
+
7
+ Inspiration was taken from [How to Reduce the Disk Space Required by a
8
+ SAS® Data Set](https://www.lexjansen.com/nesug/nesug06/io/io18.pdf) by
9
+ Selvaratnam Sridharma. The end of the referenced paper presents a macro named
10
+ "squeeze", hence the nomenclature.
11
+
12
+ Usage:
13
+
14
+ data big;
15
+ length my big $32000;
16
+ do i=1 to 1e4;
17
+ my=repeat('oh my',100);
18
+ big='dawg';
19
+ special=._;
20
+ output;
21
+ end;
22
+ run;
23
+
24
+ %mp_ds2squeeze(work.big,outds=work.smaller)
25
+
26
+ The following will also be printed to the log (exact values may differ
27
+ depending on your OS and COMPRESS settings):
28
+
29
+ > MP_DS2SQUEEZE: work.big was 625MB
30
+
31
+ > MP_DS2SQUEEZE: work.smaller is 5MB
32
+
33
+ @param [in] libds The library.dataset to be squeezed
34
+ @param [out] outds= (work.mp_ds2squeeze) The squeezed dataset to create
35
+ @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
36
+
37
+ <h4> SAS Macros </h4>
38
+ @li mf_getfilesize.sas
39
+ @li mf_getuniquefileref.sas
40
+ @li mf_getuniquename.sas
41
+ @li mp_getmaxvarlengths.sas
42
+
43
+ <h4> Related Programs </h4>
44
+ @li mp_ds2squeeze.test.sas
45
+
46
+ @version 9.3
47
+ @author Allan Bowe
48
+ **/
49
+
50
+ %macro mp_ds2squeeze(
51
+ libds,
52
+ outds=work.mp_ds2squeeze,
53
+ mdebug=0
54
+ )/*/STORE SOURCE*/;
55
+ %local dbg source;
56
+ %if &mdebug=1 %then %do;
57
+ %put &sysmacroname entry vars:;
58
+ %put _local_;
59
+ %end;
60
+ %else %do;
61
+ %let dbg=*;
62
+ %let source=/source2;
63
+ %end;
64
+
65
+ %local optval ds fref startsize;
66
+ %let ds=%mf_getuniquename();
67
+ %let fref=%mf_getuniquefileref();
68
+ %let startsize=%mf_getfilesize(libds=&libds,format=yes);
69
+
70
+ %mp_getmaxvarlengths(&libds,outds=&ds)
71
+
72
+ data _null_;
73
+ set &ds end=last;
74
+ file &fref;
75
+ /* grab the types */
76
+ retain dsid;
77
+ if _n_=1 then dsid=open("&libds",'is');
78
+ if dsid le 0 then do;
79
+ msg=sysmsg();
80
+ put msg=;
81
+ stop;
82
+ end;
83
+ type=vartype(dsid,varnum(dsid, name));
84
+ if last then rc=close(dsid);
85
+ /* write out the length statement */
86
+ if _n_=1 then put 'length ';
87
+ length len $6;
88
+ if type='C' then do;
89
+ if maxlen=0 then len='$1';
90
+ else len=cats('$',maxlen);
91
+ end;
92
+ else do;
93
+ if maxlen=0 then len='3';
94
+ else len=cats(maxlen);
95
+ end;
96
+ put ' ' name ' ' len;
97
+ if last then put ';';
98
+ run;
99
+
100
+ /* configure varlenchk - as we are explicitly shortening the variables */
101
+ %let optval=%sysfunc(getoption(varlenchk));
102
+ options varlenchk=NOWARN;
103
+
104
+ data &outds;
105
+ %inc &fref &source;
106
+ set &libds;
107
+ run;
108
+
109
+ options varlenchk=&optval;
110
+
111
+ %if &mdebug=0 %then %do;
112
+ proc sql;
113
+ drop table &ds;
114
+ filename &fref clear;
115
+ %end;
116
+
117
+ %put &sysmacroname: &libds was &startsize;
118
+ %put &sysmacroname: &outds is %mf_getfilesize(libds=&outds,format=yes);
119
+
120
+ %mend mp_ds2squeeze;
@@ -1,28 +1,46 @@
1
1
  /**
2
- @file mp_getmaxvarlengths.sas
2
+ @file
3
3
  @brief Scans a dataset to find the max length of the variable values
4
4
  @details
5
5
  This macro will scan a base dataset and produce an output dataset with two
6
6
  columns:
7
7
 
8
8
  - NAME Name of the base dataset column
9
- - MAXLEN Maximum length of the data contained therein.
9
+ - MAXLEN Maximum length of the data contained therein.
10
10
 
11
- Character fields may be allocated very large widths (eg 32000) of which the
12
- maximum value is likely to be much narrower. This macro was designed to
13
- enable a HTML table to be appropriately sized however this could be used as
14
- part of a data audit to ensure we aren't over-sizing our tables in relation to
15
- the data therein.
11
+ Character fields are often allocated very large widths (eg 32000) of which the
12
+ maximum value is likely to be much narrower. Identifying such cases can be
13
+ helpful in the following scenarios:
14
+
15
+ @li Enabling a HTML table to be appropriately sized (`num2char=YES`)
16
+ @li Reducing the size of a dataset to save on storage (mp_ds2squeeze.sas)
17
+ @li Identifying columns containing nothing but missing values (`MAXLEN=0` in
18
+ the output table)
19
+
20
+ If the entire column is made up of (non-special) missing values then a value
21
+ of 0 is returned.
16
22
 
17
- Numeric fields are converted using the relevant format to determine the width.
18
23
  Usage:
19
24
 
20
25
  %mp_getmaxvarlengths(sashelp.class,outds=work.myds)
21
26
 
22
- @param libds Two part dataset (or view) reference.
23
- @param outds= The output dataset to create
27
+ @param [in] libds Two part dataset (or view) reference.
28
+ @param [in] num2char= (NO) When set to NO, numeric fields are sized according
29
+ to the number of bytes used (or set to zero in the case of non-special
30
+ missings). When YES, the numeric field is converted to character (using the
31
+ format, if available), and that is sized instead, using `lengthn()`.
32
+ @param [out] outds= The output dataset to create, eg:
33
+ |NAME:$8.|MAXLEN:best.|
34
+ |---|---|
35
+ |`Name `|`7 `|
36
+ |`Sex `|`1 `|
37
+ |`Age `|`3 `|
38
+ |`Height `|`8 `|
39
+ |`Weight `|`3 `|
24
40
 
25
41
  <h4> SAS Macros </h4>
42
+ @li mcf_length.sas
43
+ @li mf_getuniquename.sas
26
44
  @li mf_getvarlist.sas
27
45
  @li mf_getvartype.sas
28
46
  @li mf_getvarformat.sas
@@ -30,20 +48,32 @@
30
48
  @version 9.2
31
49
  @author Allan Bowe
32
50
 
51
+ <h4> Related Macros </h4>
52
+ @li mp_ds2squeeze.sas
53
+ @li mp_getmaxvarlengths.test.sas
54
+
33
55
  **/
34
56
 
35
57
  %macro mp_getmaxvarlengths(
36
- libds /* libref.dataset to analyse */
37
- ,outds=work.mp_getmaxvarlengths /* name of output dataset to create */
58
+ libds
59
+ ,num2char=NO
60
+ ,outds=work.mp_getmaxvarlengths
38
61
  )/*/STORE SOURCE*/;
39
62
 
40
- %local vars x var fmt;
63
+ %local vars prefix x var fmt;
41
64
  %let vars=%mf_getvarlist(libds=&libds);
65
+ %let prefix=%substr(%mf_getuniquename(),1,25);
66
+ %let num2char=%upcase(&num2char);
67
+
68
+ %if &num2char=NO %then %do;
69
+ /* compile length function for numeric fields */
70
+ %mcf_length(wrap=YES, insert_cmplib=YES)
71
+ %end;
42
72
 
43
73
  proc sql;
44
74
  create table &outds (rename=(
45
75
  %do x=1 %to %sysfunc(countw(&vars,%str( )));
46
- ________&x=%scan(&vars,&x)
76
+ &prefix.&x=%scan(&vars,&x)
47
77
  %end;
48
78
  ))
49
79
  as select
@@ -51,18 +81,21 @@ create table &outds (rename=(
51
81
  %let var=%scan(&vars,&x);
52
82
  %if &x>1 %then ,;
53
83
  %if %mf_getvartype(&libds,&var)=C %then %do;
54
- max(length(&var)) as ________&x
84
+ max(lengthn(&var)) as &prefix.&x
55
85
  %end;
56
- %else %do;
86
+ %else %if &num2char=YES %then %do;
57
87
  %let fmt=%mf_getvarformat(&libds,&var);
58
88
  %put fmt=&fmt;
59
89
  %if %str(&fmt)=%str() %then %do;
60
- max(length(cats(&var))) as ________&x
90
+ max(lengthn(cats(&var))) as &prefix.&x
61
91
  %end;
62
92
  %else %do;
63
- max(length(put(&var,&fmt))) as ________&x
93
+ max(lengthn(put(&var,&fmt))) as &prefix.&x
64
94
  %end;
65
95
  %end;
96
+ %else %do;
97
+ max(mcf_length(&var)) as &prefix.&x
98
+ %end;
66
99
  %end;
67
100
  from &libds;
68
101