Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_filtervalidate.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Checks a generated filter query for validity
4 @details Runs a generated filter in proc sql with the validate option.
5 Used in mp_filtercheck.sas in an fcmp container.
6
7 A truncated comparison cannot be written in SQL, so a `=:` / `^=:` clause is
8 validated as the equivalent truncated comparison operator (EQT / NET). The
9 clause itself is not altered - only the generated validate query is.
10
11 Built to support dynamic filtering in
12 [Data Controller for SAS®](https://datacontroller.io).
13
14 Usage:
15
16 data work.filtertable;
17 infile datalines4 dsd;
18 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
19 OPERATOR_NM:$10. RAW_VALUE:$4000.;
20 datalines4;
21 AND,AND,1,AGE,=,12
22 AND,AND,1,SEX,<=,"'M'"
23 AND,OR,2,Name,NOT IN,"('Jane','Alfred')"
24 AND,OR,2,Weight,>=,7
25 ;;;;
26 run;
27
28 %mp_filtergenerate(work.filtertable,outref=myfilter)
29
30 %mp_filtervalidate(myfilter,sashelp.class)
31
32 @returns The SYSCC value will be 1008 if there are validation issues.
33 On an abort the message carries the reason and the clause that failed, so
34 the inputs are visible without reading the log.
35
36 @param [in] inref The input fileref to validate (generated by
37 mp_filtergenerate.sas)
38 @param [in] targetds The target dataset against which to verify the query
39 @param [out] abort= (YES) If YES will call mp_abort.sas on any exceptions
40 @param [out] outds= (work.mp_filtervalidate) Output dataset containing the
41 diagnostic message, if one exists. If this table contains any rows,
42 there are problems!
43
44 <h4> SAS Macros </h4>
45 @li mf_getuniquefileref.sas
46 @li mf_nobs.sas
47 @li mp_abort.sas
48
49 <h4> Related Macros </h4>
50 @li mp_filtercheck.sas
51 @li mp_filtergenerate.sas
52
53 @version 9.3
54 @author Allan Bowe
55
56**/
57
58%macro mp_filtervalidate(inref,targetds,abort=YES,outds=work.mp_filtervalidate);
59
60%mp_abort(iftrue= (&syscc ne 0 or &syserr ne 0)
61 ,mac=&sysmacroname
62 ,msg=%str(syscc=&syscc / syserr=&syserr - on macro entry)
63)
64
65%local fref1 flt_clause flt_msg;
66%let fref1=%mf_getuniquefileref();
67
68data _null_;
69 file &fref1;
70 infile &inref end=eof;
71 length flt_clause flt_sql $4000;
72 retain flt_clause;
73 if _n_=1 then do;
74 put "proc sql;";
75 put "validate select * from &targetds";
76 put "where " ;
77 end;
78 input;
79 /**
80 * SQL has no colon modifier, so the validation copy of the clause uses the
81 * truncated comparison operators it is equivalent to - EQT for =: and NET
82 * for ^=: (see the truncated string comparison operators in the SQL
83 * procedure documentation). The clause applied to the data step keeps the
84 * colon form; only this generated validate query is translated.
85 *
86 * Only the operator token is rewritten. The line is written by
87 * mp_filtergenerate as VARIABLE_NM OPERATOR_NM RAW_VALUE, so the operator
88 * is always the second token, and anchoring the pattern at the start of the
89 * line leaves a raw value that contains the same characters as written.
90 */
91 flt_sql=prxchange('s/^( *\S+ +)\^=:( |$)/$1net$2/',1,_infile_);
92 flt_sql=prxchange('s/^( *\S+ +)=:( |$)/$1eqt$2/',1,flt_sql);
93 put flt_sql;
94 putlog flt_sql;
95 /* the clause as SQL receives it, kept so a failure can name the part that
96 failed - the SQL message reports only the clashing data types */
97 flt_clause=cats(flt_clause,ifc(_n_>1,' ',''),strip(flt_sql));
98 if eof then do;
99 put ";quit;";
100 call symputx('flt_clause',flt_clause,'l');
101 end;
102run;
103
104%inc &fref1;
105
106data &outds;
107 if &sqlrc or &syscc or &syserr then do;
108 REASON_CD='VALIDATION_ERR'!!'OR: '!!
109 coalescec(symget('SYSERRORTEXT'),symget('SYSWARNINGTEXT'));
110 output;
111 end;
112 else stop;
113run;
114
115filename &fref1 clear;
116
117%if %mf_nobs(&outds)>0 %then %do;
118 %if &abort=YES %then %do;
119 data _null_;
120 set &outds;
121 length flt_msg $4000;
122 call symputx('REASON_CD',reason_cd,'l');
123 /* the clause is reported with the reason: the SQL message says which
124 types clashed but not which inputs produced it */
125 flt_msg=catx(' ','Filter validation issues:',reason_cd
126 ,catx(' ','Clause: where',symget('flt_clause')));
127 call symputx('flt_msg',flt_msg,'l');
128 stop;
129 run;
130 %mp_abort(
131 mac=&sysmacroname,
132 msg=%superq(flt_msg)
133 )
134 %end;
135 %let syscc=1008;
136%end;
137
138%mend mp_filtervalidate;