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;
53 %let keylen=%length(%superq(key));
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
71 call symputx(
'inds',inds,
'L');
72 call symputx(
'baseds',baseds,
'L');
73 call symputx(
'outds',outds,
'L');
77 call symputx(
'validrefs',validrefs,
'L');
78 if validrefs ne 1 then
do;
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');
86 if a ne 0 then b=open(baseds,
'i');
89 if a=0 then comments=
'Input dataset cannot be opened';
90 else if b=0 then comments=
'Model dataset cannot be opened';
93 comments=
'Column names types and lengths match';
94 if attrn(a,
'NVARS') ne attrn(b,'NVARS') then do;
96 comments='Column count differs from model';
98 else do i=1 to attrn(b,'NVARS');
103 comments=catx(
' ','Missing column',bn);
105 else if vartype(a,pos) ne vartype(b,i) or
106 varlen(a,pos) ne varlen(b,i) then do;
108 comments=catx(
' ','Type or length differs for',bn);
111 if result='PASS' and &keylen>32767 then do;
113 comments='Key list too
long';
115 else if result='PASS' then do;
117 do i=1 to countw(keys,
' ');
118 keyname=scan(keys,i,
' ');
119 if not nvalid(keyname,'V7') then do;
121 comments='Invalid key column name';
123 else if varnum(a,keyname)=0 then do;
125 comments=catx(
' ','Key column not found',keyname);
128 if lengthn(missvars)+lengthn(keyname)+11+
129 4*(not missing(missvars))>32767 then do;
131 comments='Key expression too
long';
135 keylist=catx(
',',keylist,cats('t.',keyname));
136 missvars=catx(' or ',missvars,cats('missing(t.',keyname,
')'));
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');
149 %if &validrefs ne 1 %then %return;
152 %let tmp=work.%mf_getuniquename(prefix=mp_assertmodel);
153 %if &result=PASS and %length(%superq(keylist))>0 %then %do;
155 %let duplicaterows=-1;
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
163 group by &keylist having count(*)>1
165 %let sqlstatus2=&sqlrc;
167 %if &sqlstatus ne 0 or &sqlstatus2 ne 0 %then %do;
169 %let comments=Unable to check key values;
171 %else %if &missingrows ne 0 or &duplicaterows ne 0 %then %do;
173 %let comments=Key contains missing or duplicate values;
175 %else %let comments=Column model and key match;
180 test_description=symget('desc');
181 test_result=symget('result');
182 test_comments=symget('comments');
186 proc append base=&outds data=&tmp;
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,.);