Loading...
Searching...
No Matches
getcolvals.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Retrieves column info to enable population of dropdowns
4 @details An optional filterquery may be provided, if so then it is validated
5 and then used to filter the subsequent results.
6
7 <h4> Service Inputs </h4>
8
9 <h5> IWANT </h5>
10
11 The STARTROW and ROWS variables are used to fetch additional values beyond
12 the initial default (4000).
13
14 |libds:$19.|col:$9.|STARTROW:8.|ROWS:8.|
15 |---|---|---|---|
16 |DC258467.MPE_X_TEST|SOME_TIME|4001|1000
17
18 <h5> FILTERQUERY </h5>
19 |GROUP_LOGIC:$3|SUBGROUP_LOGIC:$3|SUBGROUP_ID:8.|VARIABLE_NM:$32|OPERATOR_NM:$10|RAW_VALUE:$32767|
20 |---|---|---|---|---|---|
21 |AND|AND|1|SOME_BESTNUM|>|1|
22 |AND|AND|1|SOME_TIME|=|77333|
23
24 <h4> Service Outputs </h4>
25 <h5> VALS </h5>
26 The type of this column actually depends on the underlying column type, so it can change
27 |FORMATTED|UNFORMATTED|
28 |---|---|
29 |$44.00|44|
30
31 <h5> META </h5>
32 |COLUMN:$32.|SASFORMAT:$32.|STARTROW:8.|ROWS:8.|
33 |---|---|---|---|
34 |COL_NAME|DOLLAR8.2|4001|1000
35
36 <h4> SAS Macros </h4>
37 @li mf_existds.sas
38 @li mf_nobs.sas
39 @li mpeinit.sas
40 @li mpeterm.sas
41 @li dc_assignlib.sas
42 @li mf_getvarformat.sas
43 @li mp_abort.sas
44 @li mp_cntlout.sas
45 @li mp_filtercheck.sas
46 @li mp_filtergenerate.sas
47 @li mpe_derived.sas
48 @li mp_validatecol.sas
49 @li mpe_validatecol.sas
50
51 @version 9.2
52 @author 4GL Apps Ltd.
53 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
54 and may not be re-distributed or re-sold without the express permission of
55 4GL Apps Ltd.
56
57 @sasjslint {"maxLineLength": 100}
58
59**/
60%mpeinit()
61
62/* input table may or may not exist */
63data work.initvars;
64 length GROUP_LOGIC $3 SUBGROUP_LOGIC $3 SUBGROUP_ID 8 VARIABLE_NM $32
65 OPERATOR_NM $10 RAW_VALUE $32767;
66 call missing(of _all_);
67 stop;
68data work.filterquery;
69 set %sysfunc(ifc(
70 %mf_existds(work.filterquery)=1
71 ,work.filterquery
72 ,work.initvars
73 ));
74run;
75
76/* print data for debugging */
77data _null_;
78 set work.iwant;
79 put (_all_)(=);
80run;
81data _null_;
82 set work.filterquery;
83 put (_all_)(=);
84run;
85
86/**
87 * libds and col are request inputs that flow into executable positions
88 * (set &libds, proc sql select &col2). They are read from the IWANT
89 * table with symget in a data step (never re-resolved) and validated
90 * here before use - mf_getvalue would re-resolve any macro content in
91 * the value before this code ran.
92 */
93%let libds=;
94%let col2=;
95%let is_libds=0;
96%let is_col=0;
97data _null_;
98 length _libds $64 _col $32;
99 set work.iwant;
100 _libds=libds;
101 _col=col;
102 %mpe_validatecol(_libds,LIBDS,is_libds)
103 %mp_validatecol(_col,ISNAME,is_col)
104 if is_libds=0 then putlog 'ERR' 'OR: Invalid libds:' _libds;
105 if is_col=0 then putlog 'ERR' 'OR: Invalid col:' _col;
106 call symputx('is_libds',is_libds,'l');
107 call symputx('is_col',is_col,'l');
108 if is_libds=1 then call symputx('libds',upcase(_libds),'l');
109 if is_col=1 then call symputx('col2',upcase(_col),'l');
110 stop;
111run;
112
113%let is_fmt=0;
114%let startrow=1;
115%let rows=4000;
116
117/* a request with no rows never reached the validation below, so this is the
118 only case that is missing inputs - an invalid value is a different abort, and
119 saying "missing" for it would hide the reason */
120%mp_abort(iftrue= (%mf_nobs(work.iwant)<=0)
121 ,mac=&_program..sas
122 ,msg=%str(Missing inputs from iwant)
123)
124%mp_abort(iftrue= (&is_libds ne 1)
125 ,mac=&_program..sas
126 ,msg=%str(Invalid libds)
127)
128%mp_abort(iftrue= (&is_col ne 1)
129 ,mac=&_program..sas
130 ,msg=%str(Invalid col)
131)
132
133%dc_assignlib(WRITE,%scan(&libds,1,.))
134
135data _null_;
136 call missing(startrow,rows);
137 set work.iwant;
138 /* check if the request is for a format catalog */
139 call symputx('orig_libds',libds);
140 is_fmt=0;
141 if substr(cats(reverse(libds)),1,3)=:'CF-' then do;
142 libds=scan(libds,1,'-');
143 putlog "Format Catalog Captured";
144 call symputx('libds','work.fmtextract');
145 is_fmt=1;
146 end;
147 call symputx('is_fmt',is_fmt);
148 call symputx('startrow',coalesce(startrow,&startrow));
149 call symputx('rows',coalesce(rows,&rows));
150 putlog (_all_)(=);
151run;
152
153%mp_cntlout(
154 iftrue=(&is_fmt=1)
155 ,libcat=&orig_libds
156 ,fmtlist=0
157 ,cntlout=work.fmtextract
158)
159
160
161/**
162 * Derived (computed) variables - these exist only in a view, so the filter
163 * must be validated and applied against that view
164 */
165%mpe_derived(libds=&libds,
166 dclib=&mpelib,
167 outref=dercode,
168 outview=work.vw_derived,
169 outvars=work.derived_vars
170)
171%let derived_nobs=%mf_nobs(work.derived_vars);
172%let filterds=%sysfunc(ifc(&derived_nobs>0,work.vw_derived,&libds));
173
174/**
175 * Validate the filter query
176 */
177%mp_filtercheck(work.filterquery,targetds=&filterds,abort=YES)
178
179/**
180 * Prepare the query
181 */
182%mp_filtergenerate(work.filterquery,outref=myfilter)
183
184/* cannot %inc in a sql where clause, only data step, so - use a view */
185data work.vw_vals/view=work.vw_vals;
186 set &filterds;
187 where %inc myfilter;;
188run;
189
190proc sql;
191create view work.vw_vals_sorted as
192 select distinct
193 put(&col2,%mf_getVarFormat(work.vw_vals,&col2,force=1)) as formatted,
194 &col2 as unformatted
195 from work.vw_vals;
196
197/* restrict num of output values */
198data work.vals;
199 set work.vw_vals_sorted;
200 if _n_ ge &startrow;
201 x+1;
202 if x>&rows then stop;
203 drop x;
204run;
205
206data vals;
207 /* ensure empty value if table is empty, for dropdowns */
208 if nobs=0 then output;
209 set vals nobs=nobs;
210 format unformatted ;
211 output;
212run;
213
214proc sql noprint;
215select count(*) into: nobs from work.vw_vals_sorted;
216data meta;
217 column="&col2";
218 sasformat="%mf_getVarFormat(work.vw_vals,&col2)";
219 startrow=&startrow;
220 rows=&rows;
221 nobs=&nobs;
222run;
223
224%mp_abort(iftrue= (&syscc ne 0)
225 ,mac=&_program..sas
226 ,msg=%str(syscc=&syscc)
227)
228
229%webout(OPEN)
230%webout(OBJ,vals,missing=STRING,showmeta=YES)
231%webout(OBJ,meta)
232%webout(CLOSE)
233
234
235%mpeterm()