I have a dataset that lists my center ID and the organs transplanted at that center ID.
I need to convert the dataset to tell me at each unique center ID how many and which organs are transplanted.
What the data looks like:
Center ID Organ
1 Heart
1 Lung
1 Kidney
2 Heart
3 Heart
3 Liver
4 Heart
4 Lung
5 Liver
6 Lung
7 Heart
7 Lung
7 Liver
7 Kidney
What I need it to look like:
Center ID Organ
1 Heart+Lung+Kidney
2 Heart
3 Heart+Liver
4 Heart+Lung
5 Liver
6 Lung
7 Heart+Lung+Liver+Kidney
The last column can be coded for the different combinations of organs. Any help will be truly appreciated!!
data have;
input (CenterID Organ ) ($);
cards;
1 Heart
1 Lung
1 Kidney
2 Heart
3 Heart
3 Liver
4 Heart
4 Lung
5 Liver
6 Lung
7 Heart
7 Lung
7 Liver
7 Kidney
;
data want;
do until(last.centerid);
set have;
by centerid;
length want $100;
want=catx('+',want,organ);
end;
drop organ;
run;
Here is one option:
data have;
infile datalines;
input centerId 1. organ:$20.;
datalines;
1 Heart
1 Lung
1 Kidney
2 Heart
3 Heart
3 Liver
4 Heart
4 Lung
5 Liver
6 Lung
7 Heart
7 Lung
7 Liver
7 Kidney
;
proc sort data=have;
by centerid organ;
DATA want;
SET have;
BY centerId organ;
FORMAT organ_list $100. cnt 9.;
RETAIN organ_list cnt;
IF first.centerid THEN
DO;
organ_list = '';
cnt = 0;
END;
organ_list = CATX('+',organ_list,organ);
cnt+1;
IF last.centerid; *output only last one for centerID;
DROP organ;
RUN;
Hi @Kashvig
According to your post, you want for each unique center ID how many and which organs are transplanted. Your output example doesn't include counts, but here is an expanded version of the code provided by @novinosrin ,that includes counts for each organ and total.
data have;
input (CenterID Organ ) ($);
cards;
1 Heart
1 Lung
1 Kidney
1 Heart
2 Heart
3 Heart
3 Liver
4 Heart
4 Lung
5 Liver
6 Lung
7 Heart
7 Lung
7 Lung
7 Liver
7 Kidney
;
proc sql;
create table w as
select distinct CenterID, Organ, count(*) as Cnt
from have
group by CenterID, Organ
order by CenterID, Organ;
quit;
data want;
drop Cnt Tot organ;
do until(last.centerid);
set w;
length want $100;
by centerid;
if first.centerid then Tot = 0;
want=catx(' + ',want,trim(organ)||':'||strip(put(Cnt,8.)));
Tot = Tot + Cnt;
end;
want=catx(' - ',want,'Total:'||strip(put(Tot,8.)));
run;
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.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.