With these new specifications the DO-END block after "else if _n0<3 ..." changes:
data want(drop=_:);
set have;
array e[*] event1 event2 case32 event46;
length _s $4 _v event_type $40;
_s=cat(of e[*]);
_n0=countc(_s,'0');
if _n0=3 then do; /* Three zeros indicate that only one type of form has been filled. */
_vn=verify(_s,'0'); /* This determines which form it was. */
_v=vname(e[_vn]);
event_type=catx(' ',_v,grp,'only');
if _vn<3 then event_type=' '||event_type; /* Indentation improves sort order of report columns. */
Duration=e[_vn];
end;
else if _n0<3 then do; /* Fewer than 3 zeros indicate that multiple forms have been filled. */
_n1=countc(_s,'1');
if _n1=1 then do; /* If value 1 occurs only once, ... */
_vn=whichn(1, of e[*]); /* ... this determines the corresponding form. */
_v=vname(e[_vn]);
if _vn=4 then event_type=catx(' ',_v,grp,'only');
else event_type=catx(' ',_v, 'only');
if _vn<3 then event_type=' '||event_type; /* Indentation improves sort order of report columns. */
Duration=1; /* Value 1 overrides value 2. */
end;
else do;
event_type='Multiple';
Duration=1+(_n1=0); /* Duration=2 only if value 1 does not occur, otherwise Duration=1. */
end;
end;
format Duration days.;
run;
First, we count the 1s in the four-digit string _s. If that number _n1=1, we determine where the single 1 occurred (WHICHN function) and conclude that event_type is the corresponding "event" only (variable _v). In the special case where that is "Event46" (i.e., _vn=4), the value of grp is inserted. As previously, we indent "Event1" and "Event2". Definitely, Duration=1, so that value 1 prevails.
If _n1 does not equal 1 (while _n0<3), we must have either two (or more) 1s or zero 1s and two (or more) 2s. Both cases qualify for event_type='Multiple' and the Duration is 1 or 2, respectively.
To obtain an overview of the event_type and Duration values resulting from the various four-digit combinations in _s (and the value of grp), you can omit the DROP= dataset option from the DATA statement (let's call the new output dataset CHK) and run the following PROC FREQ step:
proc freq data=chk;
tables event_type*duration*_s / missing list;
run;
... View more