@sasjs/core 4.45.12 → 4.46.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ /**
2
+ @file
3
+ @brief Apply leading blanks to align numbers vertically in a char variable
4
+ @details This is particularly useful when storing numbers (as character) that
5
+ need to be sorted.
6
+
7
+ It works by splitting the number left and right of the decimal place, and
8
+ aligning it accordingly. A temporary variable is created as part of this
9
+ process (which is automatically dropped)
10
+
11
+ The macro can be used only in data step, eg as follows:
12
+
13
+ data _null_;
14
+ length myvar $50;
15
+ do i=1 to 1000 by 50;
16
+ if mod(i,2)=0 then j=ranuni(0)*i*100;
17
+ else j=i*100;
18
+
19
+ %mp_aligndecimal(myvar,width=7)
20
+
21
+ leading_spaces=length(myvar)-length(cats(myvar));
22
+ putlog +leading_spaces myvar;
23
+ end;
24
+ run;
25
+
26
+ The generated code will look something like this:
27
+
28
+ length aligndp4e49996 $7;
29
+ if index(myvar,'.') then do;
30
+ aligndp4e49996=cats(scan(myvar,1,'.'));
31
+ aligndp4e49996=right(aligndp4e49996);
32
+ myvar=aligndp4e49996!!'.'!!cats(scan(myvar,2,'.'));
33
+ end;
34
+ else do;
35
+ aligndp4e49996=myvar;
36
+ aligndp4e49996=right(aligndp4e49996);
37
+ myvar=aligndp4e49996;
38
+ end;
39
+ drop aligndp4e49996;
40
+
41
+ Results (myvar variable):
42
+
43
+ 0.7683559324
44
+ 122.8232796
45
+ 99419.50552
46
+ 42938.5143414
47
+ 763.3799189
48
+ 15170.606073
49
+ 15083.285773
50
+ 85443.198707
51
+ 2022999.2251
52
+ 12038.658867
53
+ 1350582.6734
54
+ 52777.258221
55
+ 11723.347628
56
+ 33101.268376
57
+ 6181622.8603
58
+ 7390614.0669
59
+ 73384.537893
60
+ 1788362.1016
61
+ 2774586.2219
62
+ 7998580.8415
63
+
64
+
65
+ @param var The (data step, character) variable to modify
66
+ @param width= (8) The number of characters BEFORE the decimal point
67
+
68
+ <h4> SAS Macros </h4>
69
+ @li mf_getuniquename.sas
70
+
71
+ <h4> Related Programs </h4>
72
+ @li mp_aligndecimal.test.sas
73
+
74
+ @version 9.2
75
+ @author Allan Bowe
76
+ **/
77
+
78
+ %macro mp_aligndecimal(var,width=8);
79
+
80
+ %local tmpvar;
81
+ %let tmpvar=%mf_getuniquename(prefix=aligndp);
82
+ length &tmpvar $&width;
83
+ if index(&var,'.') then do;
84
+ &tmpvar=cats(scan(&var,1,'.'));
85
+ &tmpvar=right(&tmpvar);
86
+ &var=&tmpvar!!'.'!!cats(scan(&var,2,'.'));
87
+ end;
88
+ else do;
89
+ &tmpvar=cats(&var);
90
+ &tmpvar=right(&tmpvar);
91
+ &var=&tmpvar;
92
+ end;
93
+ drop &tmpvar;
94
+
95
+ %mend mp_aligndecimal;
@@ -42,6 +42,7 @@
42
42
  @param [in] test= (ALLVALS) The test to apply. Valid values are:
43
43
  @li ALLVALS - Test is a PASS if ALL values have a match in checkvals
44
44
  @li ANYVAL - Test is a PASS if at least 1 value has a match in checkvals
45
+ @li NOVAL - Test is a PASS if there are NO matches in checkvals
45
46
  @param [out] outds= (work.test_results) The output dataset to contain the
46
47
  results. If it does not exist, it will be created, with the following format:
47
48
  |TEST_DESCRIPTION:$256|TEST_RESULT:$4|TEST_COMMENTS:$256|
@@ -97,7 +98,7 @@
97
98
 
98
99
  %let test=%upcase(&test);
99
100
 
100
- %if &test ne ALLVALS and &test ne ANYVAL %then %do;
101
+ %if &test ne ALLVALS and &test ne ANYVAL and &test ne NOVAL %then %do;
101
102
  %mp_abort(
102
103
  mac=&sysmacroname,
103
104
  msg=%str(Invalid test - &test)
@@ -108,12 +109,12 @@
108
109
  %let result=-1;
109
110
  %let orig=-1;
110
111
  proc sql noprint;
111
- select count(*) into: result
112
+ select count(*) into: result trimmed
112
113
  from &lib..&ds
113
114
  where &col not in (
114
115
  select &ccol from &clib..&cds
115
116
  );
116
- select count(*) into: orig from &lib..&ds;
117
+ select count(*) into: orig trimmed from &lib..&ds;
117
118
  quit;
118
119
 
119
120
  %local notfound tmp1 tmp2;
@@ -145,7 +146,7 @@
145
146
  length test_description $256 test_result $4 test_comments $256;
146
147
  test_description=symget('desc');
147
148
  test_result='FAIL';
148
- test_comments="&sysmacroname: &lib..&ds..&col has &result values "
149
+ test_comments="&sysmacroname: &lib..&ds..&col has &result/&orig values "
149
150
  !!"not in &clib..&cds..&ccol.. First 10 vals:"!!symget('notfound');
150
151
  %if &test=ANYVAL %then %do;
151
152
  if &result < &orig then test_result='PASS';
@@ -153,6 +154,9 @@
153
154
  %else %if &test=ALLVALS %then %do;
154
155
  if &result=0 then test_result='PASS';
155
156
  %end;
157
+ %else %if &test=NOVAL %then %do;
158
+ if &result=&orig then test_result='PASS';
159
+ %end;
156
160
  %else %do;
157
161
  test_comments="&sysmacroname: Unsatisfied test condition - &test";
158
162
  %end;
@@ -25,6 +25,7 @@
25
25
  <h4> SAS Macros </h4>
26
26
  @li mddl_sas_cntlout.sas
27
27
  @li mf_getuniquename.sas
28
+ @li mp_aligndecimal.sas
28
29
 
29
30
  <h4> Related Macros </h4>
30
31
  @li mf_getvarformat.sas
@@ -69,13 +70,13 @@ run;
69
70
  data &cntlout;
70
71
  if 0 then set &ddlds;
71
72
  set &cntlds;
72
- if type="N" then do;
73
- start=cats(start);
74
- end=cats(end);
73
+ if type in ("I","N") then do; /* numeric (in)format */
74
+ %mp_aligndecimal(start,width=16)
75
+ %mp_aligndecimal(end,width=16)
75
76
  end;
76
77
  run;
77
78
  proc sort;
78
- by fmtname start;
79
+ by type fmtname start;
79
80
  run;
80
81
 
81
82
  proc sql;
@@ -38,6 +38,7 @@
38
38
  @li mf_getuniquename.sas
39
39
  @li mf_nobs.sas
40
40
  @li mp_abort.sas
41
+ @li mp_aligndecimal.sas
41
42
  @li mp_cntlout.sas
42
43
  @li mp_lockanytable.sas
43
44
  @li mp_storediffs.sas
@@ -134,7 +135,16 @@ run;
134
135
  * First, extract only relevant formats from the catalog
135
136
  */
136
137
  proc sql noprint;
137
- select distinct upcase(fmtname) into: fmtlist separated by ' ' from &libds;
138
+ select distinct
139
+ case
140
+ when type='N' then upcase(fmtname)
141
+ when type='C' then cats('$',upcase(fmtname))
142
+ when type='I' then cats('@',upcase(fmtname))
143
+ when type='J' then cats('@$',upcase(fmtname))
144
+ else "&sysmacroname:UNHANDLED"
145
+ end
146
+ into: fmtlist separated by ' '
147
+ from &libds;
138
148
 
139
149
  %mp_cntlout(libcat=&libcat,fmtlist=&fmtlist,cntlout=&base_fmts)
140
150
 
@@ -146,16 +156,24 @@ select distinct upcase(fmtname) into: fmtlist separated by ' ' from &libds;
146
156
  data &inlibds;
147
157
  length &delete_col $3;
148
158
  if 0 then set &template;
159
+ length start end $10000;
149
160
  set &libds;
150
161
  if &delete_col='' then &delete_col='No';
151
162
  fmtname=upcase(fmtname);
163
+ type=upcase(type);
152
164
  if missing(type) then do;
153
- if substr(fmtname,1,1)='$' then type='C';
154
- else type='N';
165
+ if substr(fmtname,1,1)='@' then do;
166
+ if substr(fmtname,2,1)='$' then type='J';
167
+ else type='I';
168
+ end;
169
+ else do;
170
+ if substr(fmtname,1,1)='$' then type='C';
171
+ else type='N';
172
+ end;
155
173
  end;
156
- if type='N' then do;
157
- start=cats(start);
158
- end=cats(end);
174
+ if type in ('N','I') then do;
175
+ %mp_aligndecimal(start,width=16)
176
+ %mp_aligndecimal(end,width=16)
159
177
  end;
160
178
  run;
161
179
 
@@ -169,9 +187,10 @@ create table &outds_add(drop=&delete_col) as
169
187
  left join &base_fmts b
170
188
  on a.fmtname=b.fmtname
171
189
  and a.start=b.start
190
+ and a.type=b.type
172
191
  where b.fmtname is null
173
192
  and upcase(a.&delete_col) ne "YES"
174
- order by fmtname, start;;
193
+ order by type, fmtname, start;
175
194
 
176
195
  /**
177
196
  * Identify deleted records
@@ -182,8 +201,9 @@ create table &outds_del(drop=&delete_col) as
182
201
  inner join &base_fmts b
183
202
  on a.fmtname=b.fmtname
184
203
  and a.start=b.start
204
+ and a.type=b.type
185
205
  where upcase(a.&delete_col)="YES"
186
- order by fmtname, start;
206
+ order by type, fmtname, start;
187
207
 
188
208
  /**
189
209
  * Identify modified records
@@ -194,8 +214,9 @@ create table &outds_mod (drop=&delete_col) as
194
214
  inner join &base_fmts b
195
215
  on a.fmtname=b.fmtname
196
216
  and a.start=b.start
217
+ and a.type=b.type
197
218
  where upcase(a.&delete_col) ne "YES"
198
- order by fmtname, start;
219
+ order by type, fmtname, start;
199
220
 
200
221
  options ibufsize=&ibufsize;
201
222
 
@@ -212,13 +233,13 @@ options ibufsize=&ibufsize;
212
233
  &outds_add(in=add)
213
234
  &outds_del(in=del);
214
235
  if not del and not mod;
215
- by fmtname start;
236
+ by type fmtname start;
216
237
  run;
217
238
  data &stagedata;
218
239
  set &ds1 &outds_mod;
219
240
  run;
220
241
  proc sort;
221
- by fmtname start;
242
+ by type fmtname start;
222
243
  run;
223
244
  %end;
224
245
  /* mp abort needs to run outside of conditional blocks */
@@ -266,7 +287,7 @@ options ibufsize=&ibufsize;
266
287
 
267
288
  %mp_storediffs(&libcat-FC
268
289
  ,&base_fmts
269
- ,FMTNAME START
290
+ ,TYPE FMTNAME START
270
291
  ,delds=&outds_del
271
292
  ,modds=&outds_mod
272
293
  ,appds=&outds_add
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  @file
3
3
  @brief The CNTLOUT table generated by proc format
4
- @details This table will actually change format depending on the data values,
5
- therefore the max possible lengths are described here to enable consistency
6
- when dealing with format data.
4
+ @details The actual CNTLOUT table may have varying variable lengths,
5
+ depending on the data values, therefore the max possible lengths
6
+ (given various practical restrictions) are described here to enable
7
+ consistency when dealing with format data.
7
8
 
8
9
  **/
9
10
 
@@ -12,14 +13,20 @@
12
13
 
13
14
  proc sql;
14
15
  create table &libds(
15
- FMTNAME char(32) label='Format name'
16
+ TYPE char(1) label='Type of format'
17
+ ,FMTNAME char(32) label='Format name'
16
18
  /*
17
19
  to accommodate larger START values, mp_loadformat.sas will need the
18
20
  SQL dependency removed (proc sql needs to accommodate 3 index values in
19
21
  a 32767 ibufsize limit)
20
22
  */
21
23
  ,START char(10000) label='Starting value for format'
22
- ,END char(32767) label='Ending value for format'
24
+ /*
25
+ Keep lengths of START and END the same to avoid this err:
26
+ "Start is greater than end: -<."
27
+ Similar usage note: https://support.sas.com/kb/69/330.html
28
+ */
29
+ ,END char(10000) label='Ending value for format'
23
30
  ,LABEL char(32767) label='Format value label'
24
31
  ,MIN num length=3 label='Minimum length'
25
32
  ,MAX num length=3 label='Maximum length'
@@ -30,7 +37,6 @@ create table &libds(
30
37
  ,MULT num label='Multiplier'
31
38
  ,FILL char(1) label='Fill character'
32
39
  ,NOEDIT num length=3 label='Is picture string noedit?'
33
- ,TYPE char(1) label='Type of format'
34
40
  ,SEXCL char(1) label='Start exclusion'
35
41
  ,EEXCL char(1) label='End exclusion'
36
42
  ,HLO char(13) label='Additional information'
package/package.json CHANGED
@@ -35,5 +35,5 @@
35
35
  "devDependencies": {
36
36
  "@sasjs/cli": "3.24.0"
37
37
  },
38
- "version": "4.45.12"
38
+ "version": "4.46.1"
39
39
  }