I am trying to combine observations by ID, using 12 different variables. I am checking for duplicate ID's and trying to combine them into one observation. Below is the code I am currently using, but every time I go to the dataset, one of the observations takes over, and the others seem to disappear.
proc sort data = test;
by ID;
run;
data have;
update dup (obs=0) dup;
by ID;
run;
proc sql;
create table want as
select PDIAG10, SDIAG10_1, SDIAG10_2,SDIAG10_3,SDIAG10_4,SDIAG10_5,SDIAG10_6,SDIAG10_7,SDIAG10_8,SDIAG10_9,SDIAG10_10, SDIAG10_11, VISIT_ID, AGE, DOS_MY, hTYPE, ER, gender, ID, RACE, Married
from have
group by ID;
quit;
Please show an example of what you are talking about.
Post (as TEXT not PHOTOGRAPHS) a few observations for the same ID with two or three other variables.
Show what you want as the output once the observations (which I assume is what you meant by rows) are "combined". Again show the result as TEXT.
What I have:
ColA Col1 Col2 Col3 Col4 Col4 Col5 Col6 ... Col11 ID
Z123 1
H456 E125 T158 1
Z123 T158 E125 1
What I want:
ColA Col1 Col2 Col3 Col4 Col4 Col5 Col6 ... Col11 ID
Z123 H456 E125 T158 1
Sorry about the previous reply! I haven't used this before.
If kind of looks like that instead picking just one of the values of a variable, say COL1, you want to create a different type of variable that can hold multiple values?
You have not stated your rules for constructing the new values of COLA, COL1, .....
If the answers to my questions are all "yes", then this program would produce the results you describe:
data want (drop=_:);
length cola $34 ;
retain cola;
set have (keep=rename=(cola=_old_cola));
by id;
if findw(cola,trim(_old_cola),' ')=0 then cola=catx(' ',cola,_old_cola);
array col col1-col11;
array _tmp $4 _tmp1-_tmp11;
do over _tmp; _tmp=coalescec(_tmp,col); end;
if last.id;
do over col; col=_tmp; end;
run;
Let's clarify a little more. Your requirements are still not entirely clear to me. I now think you want
In general, each new COL will have unique values found in that COL, but not found in the COLs to its left.
Is the above correct?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.