The method to solve this efficiently is to transpose it to a long format, remove duplicates and then transpose back to a wide format.
Does the order of the ICD10 code matter? This solution assumes it doesn't, and given your data it shouldn't otherwise your second example would have issues.
Here's a worked example. Ignore the first step-it just generates sample data to test on, you can replace the dataset name with yours as well as the appropriate variable names.
*Generate sample data;
array dx(6) $ dx1-dx6;
array diag(6) $ _temporary_ ("A", "B", "C", "D", "E", "F");
do id=1 to 6;
do j=1 to 6;
if rand('bernoulli', 0.2)=1 then do;
*Flip to long form;
proc transpose data=have out=flipped prefix=diag;
by id id2;
proc sort data=flipped nodupkey out=flipped_unique;
where not missing(diag1);
by id id2 diag1;
*Flip to wide form;
proc transpose data=flipped_unique out=want prefix=dx;
by id id2;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.