Here's a more complete version. It assumes your variables are character. If they are numeric, you would need to switch from SORTC to SORTN.
data want (keep=id);
set have;
array hlth {20};
call sortc (of hlth{*});
do _n_=1 to 19 until (flag=1);
if not missing (hlth{_n_}) and hlth{_n_} = hlth{_n_+1} then flag=1;
end;
if flag=1;
run;
This version keeps just the ID. You would have to go back to the original data to check why these IDs are flagged.
For each observation, you could successively compare the largest HLTH code to the 2nd largest, then the 2nd largest to the 3rd largest until you either exhaust all the non-missing values (so flag would be 0), or you find a duplicate (flag=1).
To do that you can use the LARGEST function and the LAG function, as in:
data want (drop=_:);
set have;
flag=0;
do _L=1 to n(of hlth:) while (flag=0);
_x=largest(_L,of hlth:);
if _L>1 and _x=lag(_x) then flag=1;
end;
run;
I'm not going to test this on your data -
As long as you copy the data to another data set (because we're going to change data values), it should be easy enough.
Put the variables into an array, then use call sortc to change the order of the variables.
Then move through the array and compare whether two consecutive values are identical (being careful not to take two consecutive missing values).
Let me know if you need help with this.
Here's a more complete version. It assumes your variables are character. If they are numeric, you would need to switch from SORTC to SORTN.
data want (keep=id);
set have;
array hlth {20};
call sortc (of hlth{*});
do _n_=1 to 19 until (flag=1);
if not missing (hlth{_n_}) and hlth{_n_} = hlth{_n_+1} then flag=1;
end;
if flag=1;
run;
This version keeps just the ID. You would have to go back to the original data to check why these IDs are flagged.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.