Loading...
Searching...
No Matches
mpe_derived.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Generates the SAS statements that create Data Controller derived variables
4 @details Derived (computed) variables are configured in MPE_DERIVED. Each
5 row defines one variable, which is created in a DATA step view ahead of the
6 filter, so that it can be filtered on in the EDIT screen.
7
8 The engine lives in @sasjs/core as mp_derivedvars - this macro only supplies
9 what is specific to Data Controller: the control table name, the bitemporal
10 filter that picks the current version of a row, and the list of names that
11 belong to the metadata and may not be used as a variable.
12
13 The macro writes the generated statements to a fileref, so that callers can
14 compose them into a view:
15
16 %mpe_derived(libds=&libds,dclib=&mpelib,outref=dercode)
17 data work.vw_derived / view=work.vw_derived;
18 set &libds;
19 %include dercode;
20 run;
21
22 Alternatively, pass outview= and the macro will build the view itself.
23
24 The filter must be applied by the caller, in the step that reads the view:
25
26 data out;
27 set work.vw_derived;
28 where %inc filtref;;
29 run;
30
31 A WHERE statement applies to the data set named on the SET statement, so it
32 cannot reference a variable that is created in the same data step. Applying
33 it to the view puts the derived variables on the input, which is what makes
34 them filterable.
35
36 Each statement block is of the form:
37
38 attrib REGION length=$21 label='REGION=<expression>' format=$21.;
39 REGION = <expression>;
40
41 Type and length are declared by the administrator, so the result is
42 deterministic. VAR_LABEL is optional: when it is missing or blank the label
43 is the variable name, an equals sign and the expression, otherwise the
44 configured label is used as given.
45
46 VAR_DEPS lists every input the expression uses - physical columns and
47 derived variables alike. The statements are generated in dependency order,
48 so a derived variable may build on another one without a sequence number
49 being maintained. A cycle is rejected.
50
51 Semicolons, macro triggers and comments are rejected, so a row can only ever
52 be an expression, and an expression that would let the row submit code of its
53 own - DOSUBL, or a function reaching the file system, the environment or the
54 macro symbol table - is refused by name.
55
56 The field level rules themselves are core's: mp_derivedvarcheck scans the
57 definitions and mp_derivedvars calls it, which is the same macro the MPE_DERIVED
58 post edit hook runs at submit time (services/hooks/mpe_derived_postedit). A
59 row is therefore measured against one rule list, whether it is being staged
60 or read.
61
62 A derived variable may not reuse the name of a physical column, and may not
63 reference itself: an intra-row calculation would imply a retained value,
64 which depends on the order rows happen to arrive in.
65
66 <h4> SAS Macros </h4>
67 @li mp_derivedvars.sas
68 @li mp_abort.sas
69
70 @param [in] libds= The target table (libref.dataset, lib must be assigned)
71 @param [in] dclib= The Data Controller control library
72 @param [out] outref= (0) Fileref to receive the generated statements
73 @param [out] outvars= (work.derived_vars) Dataset of derived variable names
74 @param [in] outview= (0) Optional two-level name for the derived view
75
76 @version 9.2
77 @author 4GL Apps Ltd
78 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
79 and may not be re-distributed or re-sold without the express permission of
80 4GL Apps Ltd.
81
82 @sasjslint {"maxLineLength": 89}
83
84**/
85
86%macro mpe_derived(libds=,
87 dclib=,
88 outref=0,
89 outvars=work.derived_vars,
90 outview=0
91);
92
93%local reserved;
94
95/* the only name that has to be refused outright - the metadata columns of the
96 target table are physical columns, so the engine's collision check covers
97 them whatever MPE_TABLES calls them */
98%let reserved=%str(_____DELETE__THIS__RECORD_____);
99
100/* dc_dttmtfmt is the bitemporal cut-off used in the where= below - callers get
101 it from %mpeinit(). Unset, the filter degenerates to "lt tx_to" and the
102 control-table step fails with a syntax fault a long way from the cause. */
103%mp_abort(iftrue= (%length(&libds)=0 or %length(&dclib)=0 or %length(&dc_dttmtfmt)=0)
104 ,mac=&sysmacroname
105 ,msg=%str(Missing: libds dclib dc_dttmtfmt)
106)
107
108/* A format catalog is addressed as LIBREF.CATALOGNAME-FC (the form
109 mpe_validatecol accepts, and the one viewtables returns). A catalog has no
110 observations, so it can have no derived variables - and it is not a dataset,
111 so the engine's existence check would abort the caller. Return an empty
112 variable list and an empty statement file instead, and leave outview alone:
113 a caller only reads the view when the list is not empty. */
114%if %length(&libds)>3 and "%upcase(%substr(&libds,%length(&libds)-2,3))"="-FC" %then %do;
115 /* the file has to exist even though it stays empty - callers %include it
116 unconditionally, and a fileref with no file behind it fails there */
117 %if "%upcase(&outref)" ne "0" %then %do;
118 filename &outref temp lrecl=32767;
119 data _null_;
120 file &outref;
121 run;
122 %end;
123 /* The shape is the engine's own: mp_derivedvars builds
124 `data &outvars(keep=name); length name $32; name=''; if 0; run;` before it
125 returns (empty case, and again from the sorted list in the populated case),
126 so NAME is the only column either path can produce. Every caller reads
127 NAME alone - getdata's `where a.NAME in (select NAME from
128 work.derived_vars)` is the only projection of this dataset in the SAS tree. */
129 data &outvars(keep=name);
130 length name $32;
131 name='';
132 if 0;
133 run;
134 %return;
135%end;
136
137%mp_derivedvars(
138 libds=&libds
139 ,control=&dclib..mpe_derived
140 ,where=%str(&dc_dttmtfmt lt tx_to and active=1)
141 ,outref=&outref
142 ,outvars=&outvars
143 ,outview=&outview
144 ,reserved=&reserved
145)
146
147%mend mpe_derived;