@sasjs/core 5.6.0 → 5.7.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/mp_derivedvars.sas +536 -0
- package/ddl/mddl_derivedvars.sas +51 -0
- package/package.json +1 -1
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Generates the SAS statements that create derived variables
|
|
4
|
+
@details Derived (computed) variables are configured in a control table, one
|
|
5
|
+
row per variable. The macro writes the statements that create them to a
|
|
6
|
+
fileref, so that callers can compose them into a view:
|
|
7
|
+
|
|
8
|
+
%mp_derivedvars(libds=&libds,control=perm.derived_vars,outref=dercode)
|
|
9
|
+
data work.vw_derived / view=work.vw_derived;
|
|
10
|
+
set &libds;
|
|
11
|
+
%include dercode;
|
|
12
|
+
run;
|
|
13
|
+
|
|
14
|
+
Alternatively pass outview= and the macro will build the view itself.
|
|
15
|
+
|
|
16
|
+
Building a view is what makes a derived variable filterable. A WHERE
|
|
17
|
+
statement applies to the data set named on the SET statement, so it cannot
|
|
18
|
+
reference a variable created in the same data step:
|
|
19
|
+
|
|
20
|
+
data out;
|
|
21
|
+
set work.vw_derived;
|
|
22
|
+
where <filter>;
|
|
23
|
+
run;
|
|
24
|
+
|
|
25
|
+
Each variable produces two statements:
|
|
26
|
+
|
|
27
|
+
attrib REGION length=$21 label='REGION=<expression>' format=$21.;
|
|
28
|
+
REGION = <expression>;
|
|
29
|
+
|
|
30
|
+
Type and length are declared in the control table, so the result is
|
|
31
|
+
deterministic.
|
|
32
|
+
|
|
33
|
+
VAR_DEPS lists every input the expression uses - physical columns
|
|
34
|
+
and derived variables alike. The order the statements are generated in is
|
|
35
|
+
the dependency order (variables with no unsatisfied dependency first, ties
|
|
36
|
+
broken by name), so a derived variable may build on another one without the
|
|
37
|
+
administrator maintaining a sequence number. A cycle is rejected.
|
|
38
|
+
|
|
39
|
+
The declaration is checked against the expression: a derived variable that
|
|
40
|
+
is referenced must be declared, a declared name that is not referenced is an
|
|
41
|
+
error, and a declared name must be a column of the target table or a derived
|
|
42
|
+
variable defined for it. Semicolons, macro triggers and comments are
|
|
43
|
+
rejected, so a row can only ever be an expression. A derived variable may
|
|
44
|
+
not reuse the name of a physical column, and may not reference itself: an
|
|
45
|
+
intra-row calculation would imply a retained value, which depends on the
|
|
46
|
+
order rows happen to arrive in.
|
|
47
|
+
|
|
48
|
+
VAR_LABEL is optional. When it is missing or blank, the label is the
|
|
49
|
+
variable name, an equals sign and the expression - so a reader can see both
|
|
50
|
+
what the column is and where it comes from. A configured label replaces
|
|
51
|
+
that, and is used as given.
|
|
52
|
+
|
|
53
|
+
The control table is created by mddl_derivedvars.sas, and must carry LIBREF,
|
|
54
|
+
DSN, VAR_NM, VAR_DEPS, VAR_TYPE, VAR_LEN, VAR_FMT and VAR_LOGIC, with
|
|
55
|
+
VAR_LABEL optional. Extra columns are ignored, but a control table that
|
|
56
|
+
holds several versions of the same VAR_NM - for example a bitemporal
|
|
57
|
+
TX_FROM / TX_TO pair - must be narrowed with where= (typically to the
|
|
58
|
+
current versions), because two rows with the same name are rejected as a
|
|
59
|
+
configuration error.
|
|
60
|
+
|
|
61
|
+
<h4> SAS Macros </h4>
|
|
62
|
+
@li mf_existds.sas
|
|
63
|
+
@li mf_existvar.sas
|
|
64
|
+
@li mf_getuniquename.sas
|
|
65
|
+
@li mf_getvarlist.sas
|
|
66
|
+
@li mf_nobs.sas
|
|
67
|
+
@li mp_abort.sas
|
|
68
|
+
|
|
69
|
+
<h4> Related Macros </h4>
|
|
70
|
+
@li mddl_derivedvars.sas
|
|
71
|
+
|
|
72
|
+
@param [in] libds= The target table (libref.dataset, lib must be assigned)
|
|
73
|
+
@param [in] control= The control table holding the variable definitions
|
|
74
|
+
@param [in] where= (0) Optional additional filter for the control table
|
|
75
|
+
@param [out] outref= (0) Fileref to receive the generated statements
|
|
76
|
+
@param [out] outvars= (work.derived_varlist) Dataset of derived variable names
|
|
77
|
+
@param [in] outview= (0) Optional two-level name for the derived view
|
|
78
|
+
@param [in] reserved= (0) Space separated list of names that may not be used
|
|
79
|
+
|
|
80
|
+
@version 5.6
|
|
81
|
+
@author 4GL Apps Ltd
|
|
82
|
+
|
|
83
|
+
**/
|
|
84
|
+
|
|
85
|
+
%macro mp_derivedvars(libds=,
|
|
86
|
+
control=,
|
|
87
|
+
where=0,
|
|
88
|
+
outref=0,
|
|
89
|
+
outvars=work.derived_varlist,
|
|
90
|
+
outview=0,
|
|
91
|
+
reserved=0
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
%local physlist derived_err resv_list i w left nready nplaced
|
|
95
|
+
cyc_start cyc_cur cyc_next cyc_path cyc_seen cyc_i
|
|
96
|
+
ds_cfg ds_byname ds_bad ds_baddep ds_dup ds_dep ds_ref ds_stale
|
|
97
|
+
ds_undecl ds_self ds_edge ds_todo ds_ready ds_placed ds_srt;
|
|
98
|
+
|
|
99
|
+
/* every working table gets its own name, so a caller with tables of the same
|
|
100
|
+
name in WORK cannot collide with this macro's intermediates */
|
|
101
|
+
%let ds_cfg=%mf_getuniquename(prefix=dvcfg);
|
|
102
|
+
%let ds_byname=%mf_getuniquename(prefix=dvbyn);
|
|
103
|
+
%let ds_bad=%mf_getuniquename(prefix=dvbad);
|
|
104
|
+
%let ds_baddep=%mf_getuniquename(prefix=dvbdp);
|
|
105
|
+
%let ds_dup=%mf_getuniquename(prefix=dvdup);
|
|
106
|
+
%let ds_dep=%mf_getuniquename(prefix=dvdep);
|
|
107
|
+
%let ds_ref=%mf_getuniquename(prefix=dvref);
|
|
108
|
+
%let ds_stale=%mf_getuniquename(prefix=dvstl);
|
|
109
|
+
%let ds_undecl=%mf_getuniquename(prefix=dvund);
|
|
110
|
+
%let ds_self=%mf_getuniquename(prefix=dvsfl);
|
|
111
|
+
%let ds_edge=%mf_getuniquename(prefix=dvedg);
|
|
112
|
+
%let ds_todo=%mf_getuniquename(prefix=dvtd);
|
|
113
|
+
%let ds_ready=%mf_getuniquename(prefix=dvrdy);
|
|
114
|
+
%let ds_placed=%mf_getuniquename(prefix=dvpld);
|
|
115
|
+
%let ds_srt=%mf_getuniquename(prefix=dvsrt);
|
|
116
|
+
|
|
117
|
+
%mp_abort(iftrue= (%length(&libds)=0 or %length(&control)=0)
|
|
118
|
+
,mac=&sysmacroname
|
|
119
|
+
,msg=%str(Missing: libds control)
|
|
120
|
+
)
|
|
121
|
+
%mp_abort(iftrue= (&outref = 0)
|
|
122
|
+
,mac=&sysmacroname
|
|
123
|
+
,msg=%str(Please provide an outref for the generated statements)
|
|
124
|
+
)
|
|
125
|
+
%mp_abort(iftrue= (%mf_existds(libds=&libds) ne 1)
|
|
126
|
+
,mac=&sysmacroname
|
|
127
|
+
,msg=%str(Target table not found: &libds)
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
/* a long label can push a statement past the default record length, which
|
|
131
|
+
would wrap it and break the generated code */
|
|
132
|
+
filename &outref temp lrecl=32767;
|
|
133
|
+
|
|
134
|
+
/* ensure the outputs exist, and are empty */
|
|
135
|
+
data _null_;
|
|
136
|
+
file &outref;
|
|
137
|
+
run;
|
|
138
|
+
data &outvars(keep=name);
|
|
139
|
+
length name $32;
|
|
140
|
+
name='';
|
|
141
|
+
if 0;
|
|
142
|
+
run;
|
|
143
|
+
|
|
144
|
+
%if %mf_existds(libds=&control) ne 1 %then %do;
|
|
145
|
+
%put &sysmacroname: &control not found - no derived variables;
|
|
146
|
+
%return;
|
|
147
|
+
%end;
|
|
148
|
+
|
|
149
|
+
data work.&ds_cfg.;
|
|
150
|
+
set &control;
|
|
151
|
+
%if "&where" ne "0" and %length(&where)>0 %then %do;
|
|
152
|
+
where &where;
|
|
153
|
+
%end;
|
|
154
|
+
where also upcase(libref)="%upcase(%scan(&libds,1,.))"
|
|
155
|
+
and upcase(dsn)="%upcase(%scan(&libds,2,.))";
|
|
156
|
+
length name $32 logic $4000 fmt $32 deps $4000 lbl $256;
|
|
157
|
+
name=upcase(strip(var_nm));
|
|
158
|
+
logic=strip(var_logic);
|
|
159
|
+
type=upcase(strip(var_type));
|
|
160
|
+
len=var_len;
|
|
161
|
+
fmt=strip(upcase(var_fmt));
|
|
162
|
+
deps=upcase(strip(var_deps));
|
|
163
|
+
/* VAR_LABEL is optional - older control tables may not have the column */
|
|
164
|
+
/* mf_existvar returns the variable position, so 0 means absent */
|
|
165
|
+
%if %mf_existvar(&control,VAR_LABEL) gt 0 %then %do;
|
|
166
|
+
lbl=translate(strip(var_label),' ','0D0A'x);
|
|
167
|
+
%end;
|
|
168
|
+
%else %do;
|
|
169
|
+
lbl='';
|
|
170
|
+
%end;
|
|
171
|
+
run;
|
|
172
|
+
|
|
173
|
+
%if %mf_nobs(work.&ds_cfg.)=0 %then %return;
|
|
174
|
+
|
|
175
|
+
proc sort data=work.&ds_cfg. out=work.&ds_byname.;
|
|
176
|
+
by name;
|
|
177
|
+
run;
|
|
178
|
+
|
|
179
|
+
%let physlist=%upcase(%mf_getvarlist(&libds,dlm=%str(,),quote=SINGLE));
|
|
180
|
+
|
|
181
|
+
/* build a quoted list of reserved names, or an empty list */
|
|
182
|
+
%let resv_list=;
|
|
183
|
+
%if "&reserved" ne "0" %then %do i=1 %to %sysfunc(countw(&reserved,%str( )));
|
|
184
|
+
%let w=%scan(&reserved,&i,%str( ));
|
|
185
|
+
%let resv_list=&resv_list.%str(,)%str(%")%upcase(&w)%str(%");
|
|
186
|
+
%end;
|
|
187
|
+
%if %length(&resv_list)>0 %then %let resv_list=%substr(&resv_list,2);
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Reject anything that could break the generated data step, or that would
|
|
191
|
+
* produce a variable we cannot describe to the caller
|
|
192
|
+
*/
|
|
193
|
+
data work.&ds_bad.;
|
|
194
|
+
set work.&ds_byname.;
|
|
195
|
+
length reason $200;
|
|
196
|
+
call missing(reason);
|
|
197
|
+
|
|
198
|
+
if name='' then reason='VAR_NM is required';
|
|
199
|
+
else if verify(strip(name),'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')>0
|
|
200
|
+
then reason='VAR_NM must be a valid SAS name';
|
|
201
|
+
else if verify(substr(name,1,1),'0123456789')=0
|
|
202
|
+
then reason='VAR_NM must not start with a digit';
|
|
203
|
+
%if %length(&physlist)>0 %then %do;
|
|
204
|
+
else if name in (%unquote(&physlist))
|
|
205
|
+
then reason='VAR_NM collides with a column in the target table';
|
|
206
|
+
%end;
|
|
207
|
+
%if %length(&resv_list)>0 %then %do;
|
|
208
|
+
else if name in (%unquote(&resv_list))
|
|
209
|
+
then reason='VAR_NM is reserved';
|
|
210
|
+
%end;
|
|
211
|
+
else if type not in ('C','N')
|
|
212
|
+
then reason='VAR_TYPE must be C or N';
|
|
213
|
+
else if missing(len) or len ne int(len) or len<1
|
|
214
|
+
then reason='VAR_LEN must be a positive integer';
|
|
215
|
+
else if type='C' and len>32767
|
|
216
|
+
then reason='VAR_LEN exceeds 32767 for a character variable';
|
|
217
|
+
else if type='N' and len>8
|
|
218
|
+
then reason='VAR_LEN exceeds 8 for a numeric variable';
|
|
219
|
+
else if index(fmt,';')>0 or indexc(fmt,'&%')>0
|
|
220
|
+
or index(fmt,'/*')>0 or index(fmt,'*/')>0
|
|
221
|
+
then reason='VAR_FMT must be a format name, without ; & % or //*';
|
|
222
|
+
else if verify(strip(fmt),'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$.')>0
|
|
223
|
+
then reason='VAR_FMT must be a format name';
|
|
224
|
+
else if missing(logic)
|
|
225
|
+
then reason='VAR_LOGIC is required';
|
|
226
|
+
else if index(logic,';')>0
|
|
227
|
+
then reason='VAR_LOGIC must be a single expression - remove the ;';
|
|
228
|
+
else if indexc(logic,'&%')>0
|
|
229
|
+
then reason='VAR_LOGIC must not contain macro triggers';
|
|
230
|
+
else if index(logic,'/*')>0 or index(logic,'*/')>0
|
|
231
|
+
then reason='VAR_LOGIC must not contain comments';
|
|
232
|
+
|
|
233
|
+
if missing(reason)=0 then output;
|
|
234
|
+
run;
|
|
235
|
+
|
|
236
|
+
data work.&ds_dup.;
|
|
237
|
+
set work.&ds_byname.;
|
|
238
|
+
by name;
|
|
239
|
+
length reason $200;
|
|
240
|
+
if first.name and not last.name;
|
|
241
|
+
reason='VAR_NM is defined more than once';
|
|
242
|
+
run;
|
|
243
|
+
|
|
244
|
+
proc append base=work.&ds_bad. data=work.&ds_dup.;
|
|
245
|
+
run;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The declared dependencies, one row per word
|
|
249
|
+
*/
|
|
250
|
+
data work.&ds_dep.;
|
|
251
|
+
set work.&ds_byname.;
|
|
252
|
+
length dep_nm $32;
|
|
253
|
+
do i=1 to countw(deps,' ');
|
|
254
|
+
dep_nm=scan(deps,i,' ');
|
|
255
|
+
if not missing(dep_nm) then output;
|
|
256
|
+
end;
|
|
257
|
+
keep name dep_nm;
|
|
258
|
+
run;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The identifiers the expression actually references - quoted strings are
|
|
262
|
+
* removed first, so a name inside a literal is not mistaken for a reference
|
|
263
|
+
*/
|
|
264
|
+
data work.&ds_ref.;
|
|
265
|
+
set work.&ds_byname.;
|
|
266
|
+
length l $4000 tok $32;
|
|
267
|
+
l=prxchange("s/'([^']|'')*'//",-1,logic);
|
|
268
|
+
l=prxchange('s/"([^"]|"")*"//',-1,l);
|
|
269
|
+
l=prxchange('s/[^A-Za-z0-9_]/ /',-1,upcase(l));
|
|
270
|
+
do i=1 to countw(l,' ');
|
|
271
|
+
tok=scan(l,i,' ');
|
|
272
|
+
if not missing(tok) then output;
|
|
273
|
+
end;
|
|
274
|
+
keep name tok;
|
|
275
|
+
run;
|
|
276
|
+
|
|
277
|
+
/* a declared name must resolve - to a column of the target, or to a variable
|
|
278
|
+
in this configuration */
|
|
279
|
+
proc sql noprint;
|
|
280
|
+
create table work.&ds_baddep. as
|
|
281
|
+
select d.name, d.dep_nm
|
|
282
|
+
from work.&ds_dep. d
|
|
283
|
+
where d.dep_nm not in (select name from work.&ds_byname.)
|
|
284
|
+
/* a table with no columns leaves physlist empty - fall back to the derived
|
|
285
|
+
names alone rather than compiling an empty in-list */
|
|
286
|
+
%if %length(&physlist)>0 %then %do;
|
|
287
|
+
and d.dep_nm not in (%unquote(&physlist))
|
|
288
|
+
%end;
|
|
289
|
+
;
|
|
290
|
+
|
|
291
|
+
/* every declared name must actually appear in the expression */
|
|
292
|
+
create table work.&ds_stale. as
|
|
293
|
+
select d.name, d.dep_nm
|
|
294
|
+
from work.&ds_dep. d
|
|
295
|
+
where not exists (
|
|
296
|
+
select 1 from work.&ds_ref. r
|
|
297
|
+
where r.name=d.name and r.tok=d.dep_nm
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
/* a derived variable referenced by the expression must be declared, otherwise
|
|
301
|
+
the statement order would be wrong and the value would be silently missing */
|
|
302
|
+
create table work.&ds_undecl. as
|
|
303
|
+
select distinct r.name, r.tok as dep_nm
|
|
304
|
+
from work.&ds_ref. r
|
|
305
|
+
where r.tok in (select name from work.&ds_byname.)
|
|
306
|
+
and r.tok ne r.name
|
|
307
|
+
and not exists (
|
|
308
|
+
select 1 from work.&ds_dep. d
|
|
309
|
+
where d.name=r.name and d.dep_nm=r.tok
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
/* a variable may not depend on itself */
|
|
313
|
+
create table work.&ds_self. as
|
|
314
|
+
select name, dep_nm from work.&ds_dep.
|
|
315
|
+
where name=dep_nm;
|
|
316
|
+
quit;
|
|
317
|
+
|
|
318
|
+
%if %mf_nobs(work.&ds_baddep.)>0 %then %do;
|
|
319
|
+
data _null_;
|
|
320
|
+
set work.&ds_baddep.(obs=1);
|
|
321
|
+
call symputx('derived_err'
|
|
322
|
+
,cats(name,': VAR_DEPS lists ',dep_nm
|
|
323
|
+
,', which is not a column of the table or a derived variable'));
|
|
324
|
+
run;
|
|
325
|
+
%mp_abort(iftrue=1,mac=&sysmacroname,msg=%superq(derived_err))
|
|
326
|
+
%end;
|
|
327
|
+
|
|
328
|
+
%if %mf_nobs(work.&ds_self.)>0 %then %do;
|
|
329
|
+
data _null_;
|
|
330
|
+
set work.&ds_self.(obs=1);
|
|
331
|
+
call symputx('derived_err',cats(name,': VAR_DEPS may not list itself'));
|
|
332
|
+
run;
|
|
333
|
+
%mp_abort(iftrue=1,mac=&sysmacroname,msg=%superq(derived_err))
|
|
334
|
+
%end;
|
|
335
|
+
|
|
336
|
+
%if %mf_nobs(work.&ds_undecl.)>0 %then %do;
|
|
337
|
+
data _null_;
|
|
338
|
+
set work.&ds_undecl.(obs=1);
|
|
339
|
+
call symputx('derived_err'
|
|
340
|
+
,cats(name,': VAR_LOGIC references ',dep_nm
|
|
341
|
+
,', which is not in VAR_DEPS'));
|
|
342
|
+
run;
|
|
343
|
+
%mp_abort(iftrue=1,mac=&sysmacroname,msg=%superq(derived_err))
|
|
344
|
+
%end;
|
|
345
|
+
|
|
346
|
+
%if %mf_nobs(work.&ds_stale.)>0 %then %do;
|
|
347
|
+
data _null_;
|
|
348
|
+
set work.&ds_stale.(obs=1);
|
|
349
|
+
call symputx('derived_err'
|
|
350
|
+
,cats(name,': VAR_DEPS lists ',dep_nm
|
|
351
|
+
,', which VAR_LOGIC does not reference'));
|
|
352
|
+
run;
|
|
353
|
+
%mp_abort(iftrue=1,mac=&sysmacroname,msg=%superq(derived_err))
|
|
354
|
+
%end;
|
|
355
|
+
|
|
356
|
+
%if %mf_nobs(work.&ds_bad.)>0 %then %do;
|
|
357
|
+
data _null_;
|
|
358
|
+
set work.&ds_bad.;
|
|
359
|
+
putlog 'ERR' 'OR: derived variable config: ' name= reason=;
|
|
360
|
+
run;
|
|
361
|
+
data _null_;
|
|
362
|
+
set work.&ds_bad.(obs=1);
|
|
363
|
+
call symputx('derived_err',cats(name,': ',reason));
|
|
364
|
+
run;
|
|
365
|
+
%mp_abort(iftrue=1
|
|
366
|
+
,mac=&sysmacroname
|
|
367
|
+
,msg=%str(Invalid derived variable configuration - &derived_err)
|
|
368
|
+
)
|
|
369
|
+
%end;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Dependency order. The edges are between derived variables only - a
|
|
373
|
+
* physical column is available from the start. Repeatedly take the variables
|
|
374
|
+
* whose derived inputs have all been placed, alphabetically within a round;
|
|
375
|
+
* if a round places nothing, what is left is a cycle.
|
|
376
|
+
*/
|
|
377
|
+
proc sql;
|
|
378
|
+
create table work.&ds_edge. as
|
|
379
|
+
select d.name as child, d.dep_nm as parent
|
|
380
|
+
from work.&ds_dep. d
|
|
381
|
+
where d.dep_nm in (select name from work.&ds_byname.);
|
|
382
|
+
quit;
|
|
383
|
+
|
|
384
|
+
data work.&ds_todo.(keep=name);
|
|
385
|
+
set work.&ds_byname.;
|
|
386
|
+
run;
|
|
387
|
+
|
|
388
|
+
data work.&ds_placed.(keep=name seq);
|
|
389
|
+
length name $32;
|
|
390
|
+
name='';
|
|
391
|
+
seq=0;
|
|
392
|
+
if 0;
|
|
393
|
+
run;
|
|
394
|
+
|
|
395
|
+
%let left=%mf_nobs(work.&ds_todo.);
|
|
396
|
+
%let nplaced=0;
|
|
397
|
+
|
|
398
|
+
%do %while (&left>0);
|
|
399
|
+
proc sql;
|
|
400
|
+
create table work.&ds_ready. as
|
|
401
|
+
select t.name
|
|
402
|
+
from work.&ds_todo. t
|
|
403
|
+
where not exists (
|
|
404
|
+
select 1 from work.&ds_edge. e
|
|
405
|
+
inner join work.&ds_todo. t2 on e.parent=t2.name
|
|
406
|
+
where e.child=t.name
|
|
407
|
+
)
|
|
408
|
+
order by t.name;
|
|
409
|
+
quit;
|
|
410
|
+
|
|
411
|
+
%let nready=%mf_nobs(work.&ds_ready.);
|
|
412
|
+
%if &nready=0 %then %do;
|
|
413
|
+
/* walk the edges, so the message names the cycle rather than just the
|
|
414
|
+
names that could not be placed */
|
|
415
|
+
data _null_;
|
|
416
|
+
set work.&ds_todo.(obs=1);
|
|
417
|
+
call symputx('cyc_start',name);
|
|
418
|
+
run;
|
|
419
|
+
%let cyc_path=&cyc_start;
|
|
420
|
+
%let cyc_cur=&cyc_start;
|
|
421
|
+
%let cyc_seen=%str(.&cyc_start..);
|
|
422
|
+
%do cyc_i=1 %to &left;
|
|
423
|
+
%let cyc_next=;
|
|
424
|
+
proc sql noprint;
|
|
425
|
+
select e.parent into: cyc_next
|
|
426
|
+
from work.&ds_edge. e
|
|
427
|
+
where e.child="&cyc_cur"
|
|
428
|
+
and e.parent in (select name from work.&ds_todo.);
|
|
429
|
+
quit;
|
|
430
|
+
%if %length(&cyc_next)=0 %then %let cyc_i=&left;
|
|
431
|
+
%else %do;
|
|
432
|
+
%let cyc_path=&cyc_path. -> &cyc_next;
|
|
433
|
+
%if %index(&cyc_seen,%str(.&cyc_next..))>0 %then %let cyc_i=&left;
|
|
434
|
+
%else %do;
|
|
435
|
+
%let cyc_seen=&cyc_seen.&cyc_next..;
|
|
436
|
+
%let cyc_cur=&cyc_next;
|
|
437
|
+
%end;
|
|
438
|
+
%end;
|
|
439
|
+
%end;
|
|
440
|
+
%mp_abort(iftrue=1
|
|
441
|
+
,mac=&sysmacroname
|
|
442
|
+
,msg=%str(VAR_DEPS contains a cycle: &cyc_path)
|
|
443
|
+
)
|
|
444
|
+
%end;
|
|
445
|
+
|
|
446
|
+
/* seq carries the dependency order - the placement rounds are not name
|
|
447
|
+
ordered, so the final merge must not rely on by name alone */
|
|
448
|
+
data work.&ds_ready.;
|
|
449
|
+
set work.&ds_ready.;
|
|
450
|
+
seq=_n_+&nplaced;
|
|
451
|
+
run;
|
|
452
|
+
proc append base=work.&ds_placed. data=work.&ds_ready. force;
|
|
453
|
+
run;
|
|
454
|
+
%let nplaced=%eval(&nplaced+&nready);
|
|
455
|
+
|
|
456
|
+
proc sql;
|
|
457
|
+
delete from work.&ds_todo.
|
|
458
|
+
where name in (select name from work.&ds_ready.);
|
|
459
|
+
quit;
|
|
460
|
+
|
|
461
|
+
%let left=%mf_nobs(work.&ds_todo.);
|
|
462
|
+
%end;
|
|
463
|
+
|
|
464
|
+
proc sort data=work.&ds_placed.;
|
|
465
|
+
by name;
|
|
466
|
+
run;
|
|
467
|
+
|
|
468
|
+
data work.&ds_srt.;
|
|
469
|
+
merge work.&ds_placed.(in=in1) work.&ds_byname.(in=in2);
|
|
470
|
+
by name;
|
|
471
|
+
if in1 and in2;
|
|
472
|
+
run;
|
|
473
|
+
|
|
474
|
+
proc sort data=work.&ds_srt.;
|
|
475
|
+
by seq;
|
|
476
|
+
run;
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Generate the attrib + assignment statements
|
|
480
|
+
*/
|
|
481
|
+
data _null_;
|
|
482
|
+
file &outref;
|
|
483
|
+
set work.&ds_srt.;
|
|
484
|
+
/* 512 so the doubled quotes fit before the 256 boundary is applied */
|
|
485
|
+
length stmt $4600 label $512 fmt2 $49;
|
|
486
|
+
/* the configured label wins when there is one, otherwise the label is the
|
|
487
|
+
name, an equals sign and the expression. Labels are limited to 256 chars:
|
|
488
|
+
the quotes are doubled first, then the cut is made on an even quote
|
|
489
|
+
boundary - truncating between the two halves of an escape would leave the
|
|
490
|
+
generated literal unterminated */
|
|
491
|
+
if not missing(lbl) then label=lbl;
|
|
492
|
+
else label=cats(name,'=',translate(logic,' ','0D0A'x));
|
|
493
|
+
label=tranwrd(label,"'","''");
|
|
494
|
+
if length(label)>256 then do;
|
|
495
|
+
label=substr(label,1,256);
|
|
496
|
+
if mod(count(label,"'"),2) ne 0 then label=substr(label,1,255);
|
|
497
|
+
end;
|
|
498
|
+
if not missing(fmt) and index(fmt,'.')=0 then fmt=cats(fmt,'.');
|
|
499
|
+
if type='C' then do;
|
|
500
|
+
if missing(fmt) then fmt2=cats('$',len,'.');
|
|
501
|
+
else fmt2=fmt;
|
|
502
|
+
stmt=catx(' ','attrib',name,cats('length=$',len)
|
|
503
|
+
,cats("label='",label,"'"),cats('format=',fmt2,';'));
|
|
504
|
+
end;
|
|
505
|
+
else do;
|
|
506
|
+
if missing(fmt) then fmt2=cats(len,'.');
|
|
507
|
+
else fmt2=fmt;
|
|
508
|
+
stmt=catx(' ','attrib',name,cats('length=',len)
|
|
509
|
+
,cats("label='",label,"'"),cats('format=',fmt2,';'));
|
|
510
|
+
end;
|
|
511
|
+
put stmt;
|
|
512
|
+
stmt=cats(name,' = ',logic,';');
|
|
513
|
+
put stmt;
|
|
514
|
+
run;
|
|
515
|
+
|
|
516
|
+
%mp_abort(iftrue= (&syscc ne 0)
|
|
517
|
+
,mac=&sysmacroname
|
|
518
|
+
,msg=%str(Unable to generate derived variables for &libds)
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
data &outvars(keep=name);
|
|
522
|
+
set work.&ds_srt.;
|
|
523
|
+
run;
|
|
524
|
+
|
|
525
|
+
%if %length(&outview)>0 and &outview ne 0 %then %do;
|
|
526
|
+
data &outview / view=&outview;
|
|
527
|
+
set &libds;
|
|
528
|
+
%include &outref;
|
|
529
|
+
run;
|
|
530
|
+
%mp_abort(iftrue= (&syscc ne 0)
|
|
531
|
+
,mac=&sysmacroname
|
|
532
|
+
,msg=%str(Unable to build derived view &outview for &libds)
|
|
533
|
+
)
|
|
534
|
+
%end;
|
|
535
|
+
|
|
536
|
+
%mend mp_derivedvars;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief DDL for the derived variables control table
|
|
4
|
+
@details Creates the table read by mp_derivedvars.sas. One row defines one
|
|
5
|
+
derived variable for one target table.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
|
|
9
|
+
%mddl_derivedvars(libds=perm.derived_vars)
|
|
10
|
+
|
|
11
|
+
<h4> Related Macros </h4>
|
|
12
|
+
@li mp_derivedvars.sas
|
|
13
|
+
|
|
14
|
+
@param [in] libds= (work.derived_vars) The table to create. The libref must
|
|
15
|
+
be assigned, and the table must not already exist.
|
|
16
|
+
|
|
17
|
+
@version 5.6
|
|
18
|
+
@author 4GL Apps Ltd
|
|
19
|
+
|
|
20
|
+
**/
|
|
21
|
+
|
|
22
|
+
%macro mddl_derivedvars(libds=work.derived_vars);
|
|
23
|
+
|
|
24
|
+
%local lib;
|
|
25
|
+
%let libds=%upcase(&libds);
|
|
26
|
+
%if %index(&libds,.)=0 %then %let lib=WORK;
|
|
27
|
+
%else %let lib=%scan(&libds,1,.);
|
|
28
|
+
|
|
29
|
+
proc sql;
|
|
30
|
+
create table &libds(
|
|
31
|
+
libref char(8) label='Library Reference (8 chars)',
|
|
32
|
+
dsn char(32) label='Dataset Name (32 chars)',
|
|
33
|
+
var_nm char(32) label='Name of the derived variable',
|
|
34
|
+
var_deps char(4000) label='Columns and derived variables this uses',
|
|
35
|
+
var_type char(1) label='Either (C)haracter or (N)umeric',
|
|
36
|
+
var_len num label='Length of the derived variable',
|
|
37
|
+
var_fmt char(32) label='SAS format, eg DATE9.',
|
|
38
|
+
var_label char(256) label='Optional label; defaults to NAME=expression',
|
|
39
|
+
var_logic char(4000) label='SAS expression that produces the value',
|
|
40
|
+
active num label='Is the variable active? (1/0)'
|
|
41
|
+
);
|
|
42
|
+
quit;
|
|
43
|
+
|
|
44
|
+
proc datasets lib=&lib noprint;
|
|
45
|
+
modify %scan(&libds,-1,.);
|
|
46
|
+
index create
|
|
47
|
+
pk_derivedvars=(libref dsn var_nm)
|
|
48
|
+
/nomiss unique;
|
|
49
|
+
quit;
|
|
50
|
+
|
|
51
|
+
%mend mddl_derivedvars;
|
package/package.json
CHANGED