Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_assertmodel.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Assert model and optionally check key under OBS= and FIRSTOBS=
4 @details Compares column names, types and lengths exactly, independent of
5 column order, case, formats and labels. The model dataset can be empty.
6 When key= is supplied and columns match, key components must be nonmissing
7 and composite keys unique within the caller's OBS= and FIRSTOBS= limits.
8 Limits can hide missing or duplicate keys. A blank key= skips key validation.
9 An empty dataset has no duplicate or missing keys.
10
11 One PASS or FAIL row is appended to outds. Missing datasets and invalid key
12 columns produce FAIL. The macro does not sort or intentionally modify inputs.
13 SASAUTOS installations must include the ddl folder for mddl_assert.
14 Dataset names must use ordinary V7 member names, not name literals.
15 Unqualified dataset names refer to WORK. OUTDS must identify a distinct
16 dataset and must not alias either input through a libref, filesystem link,
17 view or other storage mapping. Dataset options are not
18 accepted. Direct overlaps, malformed dataset names and name literals are
19 rejected with a diagnostic in the SAS log and nonzero SYSCC, without
20 appending a result row. Physical aliases are not detected.
21
22 Example:
23
24 %mp_assertmodel(work.actual,base=work.expected,key=id region,
25 desc=Checking the output model)
26
27 @param [in] inds Input dataset to check
28 @param [in] base= Reference dataset defining the expected columns
29 @param [in] key= Space-separated V7 key columns checked within OBS= and
30 FIRSTOBS= limits; blank skips key validation
31 @param [in] desc= (Testing dataset model) Description for the result row
32 @param [out] outds= (work.test_results) Assertion results dataset
33
34 <h4> SAS Macros </h4>
35 @li mf_getuniquename.sas
36 @li mddl_assert.sas
37 @li mp_validatecol.sas
38
39 <h4> Related Macros </h4>
40 @li mp_assert.sas
41 @li mp_assertcols.sas
42
43 @version 9.4
44 @author SASjs
45**/
46%macro mp_assertmodel(inds,base=,key=,desc=Testing dataset model,
47 outds=work.test_results);
48 %local result comments tmp keylist missvars
49 missingrows duplicaterows sqlstatus sqlstatus2 appendstatus
50 baseds validrefs keylen;
51 /* Resolve unqualified names to WORK and reject direct aliases. */
52 %let validrefs=0;
53 %let keylen=%length(%superq(key));
54 data _null_;
55 length inds baseds outds $32767 bn $32 keyname $32767
56 comments $256 result $5 keys keylist missvars $32767;
57 inds=cats(symget('inds'));
58 baseds=cats(symget('base'));
59 outds=cats(symget('outds'));
60 if index(inds,'.')=0 then inds=cats('WORK.',inds);
61 if index(baseds,'.')=0 then baseds=cats('WORK.',baseds);
62 if index(outds,'.')=0 then outds=cats('WORK.',outds);
63 %mp_validatecol(inds,LIBDS,valid_input)
64 %mp_validatecol(baseds,LIBDS,valid_base)
65 %mp_validatecol(outds,LIBDS,valid_output)
66 if valid_input and valid_base and valid_output then do;
67 if upcase(outds)=upcase(inds) or upcase(outds)=upcase(baseds) then
68 validrefs=2;
69 else do;
70 validrefs=1;
71 call symputx('inds',inds,'L');
72 call symputx('baseds',baseds,'L');
73 call symputx('outds',outds,'L');
74 end;
75 end;
76 else validrefs=0;
77 call symputx('validrefs',validrefs,'L');
78 if validrefs ne 1 then do;
79 if validrefs=2 then
80 putlog 'ERR' 'OR: MP_ASSERTMODEL result destination overlaps an input.';
81 else putlog 'ERR' 'OR: MP_ASSERTMODEL invalid dataset reference.';
82 call symputx('syscc',max(input(symget('syscc'),best32.),1012),'G');
83 stop;
84 end;
85 a=open(inds,'i');
86 if a ne 0 then b=open(baseds,'i');
87 else b=0;
88 result='FAIL';
89 if a=0 then comments='Input dataset cannot be opened';
90 else if b=0 then comments='Model dataset cannot be opened';
91 else do;
92 result='PASS';
93 comments='Column names types and lengths match';
94 if attrn(a,'NVARS') ne attrn(b,'NVARS') then do;
95 result='FAIL';
96 comments='Column count differs from model';
97 end;
98 else do i=1 to attrn(b,'NVARS');
99 bn=varname(b,i);
100 pos=varnum(a,bn);
101 if pos=0 then do;
102 result='FAIL';
103 comments=catx(' ','Missing column',bn);
104 end;
105 else if vartype(a,pos) ne vartype(b,i) or
106 varlen(a,pos) ne varlen(b,i) then do;
107 result='FAIL';
108 comments=catx(' ','Type or length differs for',bn);
109 end;
110 end;
111 if result='PASS' and &keylen>32767 then do;
112 result='FAIL';
113 comments='Key list too long';
114 end;
115 else if result='PASS' then do;
116 keys=symget('key');
117 do i=1 to countw(keys,' ');
118 keyname=scan(keys,i,' ');
119 if not nvalid(keyname,'V7') then do;
120 result='FAIL';
121 comments='Invalid key column name';
122 end;
123 else if varnum(a,keyname)=0 then do;
124 result='FAIL';
125 comments=catx(' ','Key column not found',keyname);
126 end;
127 else do;
128 if lengthn(missvars)+lengthn(keyname)+11+
129 4*(not missing(missvars))>32767 then do;
130 result='FAIL';
131 comments='Key expression too long';
132 leave;
133 end;
134 else do;
135 keylist=catx(',',keylist,cats('t.',keyname));
136 missvars=catx(' or ',missvars,cats('missing(t.',keyname,')'));
137 end;
138 end;
139 end;
140 end;
141 end;
142 if a ne 0 then rc=close(a);
143 if b ne 0 then rc=close(b);
144 call symputx('result',result,'L');
145 call symputx('comments',comments,'L');
146 call symputx('keylist',keylist,'L');
147 call symputx('missvars',missvars,'L');
148 run;
149 %if &validrefs ne 1 %then %return;
150 %let sqlstatus=0;
151 %let sqlstatus2=0;
152 %let tmp=work.%mf_getuniquename(prefix=mp_assertmodel);
153 %if &result=PASS and %length(%superq(keylist))>0 %then %do;
154 %let missingrows=-1;
155 %let duplicaterows=-1;
156 proc sql noprint;
157 select count(*) into :missingrows trimmed
158 from &inds as t where &missvars;
159 %let sqlstatus=&sqlrc;
160 select count(*) into :duplicaterows trimmed from (
161 select &keylist, count(*) as n
162 from &inds as t
163 group by &keylist having count(*)>1
164 );
165 %let sqlstatus2=&sqlrc;
166 quit;
167 %if &sqlstatus ne 0 or &sqlstatus2 ne 0 %then %do;
168 %let result=FAIL;
169 %let comments=Unable to check key values;
170 %end;
171 %else %if &missingrows ne 0 or &duplicaterows ne 0 %then %do;
172 %let result=FAIL;
173 %let comments=Key contains missing or duplicate values;
174 %end;
175 %else %let comments=Column model and key match;
176 %end;
177 %mddl_assert(&tmp)
178 data &tmp;
179 if 0 then set &tmp;
180 test_description=symget('desc');
181 test_result=symget('result');
182 test_comments=symget('comments');
183 output;
184 stop;
185 run;
186 proc append base=&outds data=&tmp;
187 run;
188 %let appendstatus=&syserr;
189 %if &appendstatus ne 0 %then
190 %put %str(ERR)OR: MP_ASSERTMODEL could not append a result to &outds.;
191 proc datasets lib=work nolist;
192 delete %scan(&tmp,2,.);
193 quit;
194%mend mp_assertmodel;