BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Joachim133
Fluorite | Level 6

Hello. 

I have a dataset of patients having multiple hospital visits. Patients are included in my study, when they have a specific diagnose. I need to keep all visits and diagnoses prior to the inclusion date, and deleting all visits after the inclusion date. 

 

Example of dataset (not real data). Inclusion when a patient have diagnose code '200':

 

Patient_IDvisit_IDDateDiagnose 
1101.01.20111Keep observation
1202.01.20154keep observation
1303.01.20200keep observation
1404.01.20085delete observation
2501.06.20119keep observation
2602.06.20543keep observation
2703.06.20200keep observation
3801.09.19746keep observation
3902.09.19549keep observation
31003.09.19200keep observation
31104.09.19432delete observation
31205.09.19543delete observation

How can this be done?

I use SAS 9.4.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
hhinohar
Quartz | Level 8
data have;
	infile datalines dlm="09"x;
	input Patient_ID visit_ID Date:mmddyy10. Diagnose;
	format date mmddyys10.;
	datalines;
1	1	01.01.20	111	Keep observation
1	2	02.01.20	154	keep observation
1	3	03.01.20	200	keep observation
1	4	04.01.20	085	delete observation
2	5	01.06.20	119	keep observation
2	6	02.06.20	543	keep observation
2	7	03.06.20	200	keep observation
3	8	01.09.19	746	keep observation
3	9	02.09.19	549	keep observation
3	10	03.09.19	200	keep observation
3	11	04.09.19	432	delete observation
3	12	05.09.19	543	delete observation
;
run;

data want;
	do until(last.patient_id or diagnose=200);
		set have;
		by patient_id;
		if first.patient_id then _N_=0;
		if _N_=0 then output;
	end;
run;

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

Retain a flag variable:

data have;
input Patient_ID  visit_ID  Date :ddmmyy8. Diagnose :$3.;
format date yymmdd10.;
datalines;
1 1 01.01.20  111
1 2 02.01.20  154
1 3 03.01.20  200
1 4 04.01.20  085
2 5 01.06.20  119
2 6 02.06.20  543
2 7 03.06.20  200
3 8 01.09.19  746
3 9 02.09.19  549
3 10  03.09.19  200
3 11  04.09.19  432
3 12  05.09.19  543
;

data want;
set have;
by patient_id;
retain flag;
if first.patient_id then flag = 1;
if flag;
if diagnose = "200" then flag = 0;
drop flag;
run;
Joachim133
Fluorite | Level 6

Thank you!
However, I forgot to mention, that the including diagnose '200' may come several times per patient_ID. The cutoff should be the first time, the patient is admitted with the diagnose '200'. Does the code also work like that?

Kurt_Bremser
Super User

@Joachim133 wrote:

Thank you!
However, I forgot to mention, that the including diagnose '200' may come several times per patient_ID. The cutoff should be the first time, the patient is admitted with the diagnose '200'. Does the code also work like that?


Yes. Once the flag has been set to zero, it will stay that way until the next patient_id comes up.

Joachim133
Fluorite | Level 6
Hey Kurt.

That sounds great, thank you very much.
I didnt see your response, before I flagged hhinhar's response as solution, but I know yours work just as well.
regards
ldamar
Calcite | Level 5
Hi,
This can be done easy using proc sql,
/* ------*/
proc sql;
create view FILTER as /* Creating a view you keep the patient_dataset intact */
select * from <patient_dataset>
where Diagnose = <diagnose Id> /* Filter for diagnose, use semicolons if the variable Diagnose is text */
and Date <= '<diagnose date literal>'d /* Filter for date */
;
quit;
/* ------*/


Regards


hhinohar
Quartz | Level 8
data have;
	infile datalines dlm="09"x;
	input Patient_ID visit_ID Date:mmddyy10. Diagnose;
	format date mmddyys10.;
	datalines;
1	1	01.01.20	111	Keep observation
1	2	02.01.20	154	keep observation
1	3	03.01.20	200	keep observation
1	4	04.01.20	085	delete observation
2	5	01.06.20	119	keep observation
2	6	02.06.20	543	keep observation
2	7	03.06.20	200	keep observation
3	8	01.09.19	746	keep observation
3	9	02.09.19	549	keep observation
3	10	03.09.19	200	keep observation
3	11	04.09.19	432	delete observation
3	12	05.09.19	543	delete observation
;
run;

data want;
	do until(last.patient_id or diagnose=200);
		set have;
		by patient_id;
		if first.patient_id then _N_=0;
		if _N_=0 then output;
	end;
run;

Joachim133
Fluorite | Level 6
This works, thanks!
novinosrin
Tourmaline | Level 20


data have;
input Patient_ID  visit_ID  Date :ddmmyy8. Diagnose :$3.;
format date yymmdd10.;
datalines;
1 1 01.01.20  111
1 2 02.01.20  154
1 3 03.01.20  200
1 4 04.01.20  085
2 5 01.06.20  119
2 6 02.06.20  543
2 7 03.06.20  200
3 8 01.09.19  746
3 9 02.09.19  549
3 10  03.09.19  200
3 11  04.09.19  432
3 12  05.09.19  543
;

data want;
 do _n_=0 by 0 until(last.patient_id);
  set have;
  by patient_id;
  if not _n_ then output;
  if Diagnose='200' then _n_=constant('e');
 end;
run;

Hi @Joachim133  @Kurt_Bremser 's solution flag cut is elegance personified, i wanted to have some fun

novinosrin
Tourmaline | Level 20
data have;
input Patient_ID  visit_ID  Date :ddmmyy8. Diagnose :$3.;
format date yymmdd10.;
datalines;
1 1 01.01.20  111
1 2 02.01.20  154
1 3 03.01.20  200
1 4 04.01.20  085
2 5 01.06.20  119
2 6 02.06.20  543
2 7 03.06.20  200
3 8 01.09.19  746
3 9 02.09.19  549
3 10  03.09.19  200
3 11  04.09.19  432
3 12  05.09.19  543
;
Proc sql;
 create table want as
 select *
 from have
 group by patient_id
 having  date<=min(ifn(diagnose='200',date,.)) 
 order by patient_id,visit_id;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2136 views
  • 3 likes
  • 5 in conversation