I have a dataset with ID, the date of their procedure, and the procedure name. Here's what it looks like:
ID Date procedure
1 1/1/2010 A
2 1/2/2010 A
2 1/2/2010 B
3 1/3/2010 A
3 1/3/2010 B
3 1/3/2010 C
I would like to have just one row per encounter. So if they have the same ID and date, they should be in the same row. So here is what I want the data to look like:
ID Date Procedure1 Procedure2 Procedure 3
1 1/1/2010 A . .
2 1/2/2010 A B .
3 1/3/2010 A B C
How can I make that happen?
(Using SAS 9.4)
Hi @Paige1 See if this helps
data have;
input ID Date :mmddyy10. procedure $;
format date mmddyy10.;
cards;
1 1/1/2010 A
2 1/2/2010 A
2 1/2/2010 B
3 1/3/2010 A
3 1/3/2010 B
3 1/3/2010 C
;
/*Identify and initialize the max count of ID for each by group*/
proc sql noprint;
select max(c) into :c trimmed
from (select count(*) as c from have group by id,date);
quit;
proc summary nway data=have missing;
class id date;
output out = want(drop=_type_ _freq_)
idgroup(out[&c](Procedure)=) ;
run;
data have;
input ID Date :mmddyy10. procedure $;
format date mmddyy10.;
cards;
1 1/1/2010 A
2 1/2/2010 A
2 1/2/2010 B
3 1/3/2010 A
3 1/3/2010 B
3 1/3/2010 C
;
/*Identify and initialize the max count of ID for each by group*/
proc sql noprint;
select max(c) into :c trimmed
from (select count(*) as c from have group by id,date);
quit;
data want;
do _n_=1 by 1 until(last.date);
set have;
by id date;
array P(*)$ Procedure1-Procedure&c;
p(_n_)=Procedure;
end;
run;
data have;
input ID Date :mmddyy10. procedure $;
format date mmddyy10.;
cards;
1 1/1/2010 A
2 1/2/2010 A
2 1/2/2010 B
3 1/3/2010 A
3 1/3/2010 B
3 1/3/2010 C
;
proc transpose data=have out=want(drop=_name_) prefix=procedure;
by id date;
var procedure;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.