I want to turn invalid values to a single value and them print only the ID's of the obs that has these values=> I have multiple error is the second part (the loop), is it possible to fix somehow this code, or is it simply impossible to do that this way?
data comb8;set comb;
if q2 not in (1,2,3,4,5)then q2=666;
if q3 not in (1,2,3,4,5)then q3=666;
if q10 not in (1,2,3,4,5)then q10=666;
if gender not in(0,1)then gender=666;
if married not in(0,1)then married=666;
run;
data comb10; set comb8;
do i=1 to n;
if i NE 666 then drop(?????????);
end;
keep id;
run;
Hoping I've understood this right - the end result you're after is to just keep the obervation number of any observation that contains an invalid value for any of the criteria you're checking for? If so, try this:
data comb8(keep=id where=(not missing(obid)));
set comb;
if q2 not in (1,2,3,4,5) then obid=_n_;
if q3 not in (1,2,3,4,5) then obid=_n_;
if q10 not in (1,2,3,4,5) then obid=_n_;
if gender not in(0,1) then obid=_n_;
if married not in(0,1) then obid=_n_;
run;
_n_
is an automatic variable that identifies the observation number that has been loaded in from the set statement. You can set obid to this value when an issue is found and then use (where=(not missing(obid)))
to only keep the observations that had invalid values
Just in case it's of help to anyone else later on...
data comb8;set comb;
if q2 not in (1,2,3,4,5)then q2=666;
if q3 not in (1,2,3,4,5)then q3=666;
if q10 not in (1,2,3,4,5)then q10=666;
if gender not in(0,1)then gender=666;
if married not in(0,1)then married=666;
run;
data comb10(keep=obid where=(not missing(obid)));
set comb8;
Array Q q1-q10 gender married . ;
do i=1 to dim(Q) ;
if Q[i] EQ 666 then obid=_n_;
end;
run;