BookmarkSubscribeRSS Feed
BochengJing
Fluorite | Level 6

Hi SAS users, 

This may seem to be an easy and obvious programming but I just couldn't figure it out. I need your help. 

In my dementia data, I have patients with repeated records throughout the years. In some years, patients were diagnosed with dementia while in other years, patients did not. My data looks like this:  

PatientYearDementia
A20100
A20120
A20140
A20161
A20181
B20100
B20121
B20140
B20161
B20180
C20101
C20120
C20140
C20161
C20181

What I want is for each patient, all their the dementia=0 observations prior to the dementia=1 diagnosis. In another words, if a patient has been diagnosed with dementia (dementia=1), I don't care what happened after (once you are diagnosed, you are identified as dementia forever) -- I want to extract all the dementia=0 cases prior to their dementia=1 diagnosis. So the table I want looks like this: 

PatientYearDementia
A20100
A20120
A20140
B20100

 

Can anyone help me with this coding? Thank you.

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20
data have;
input Patient $ Year Dementia;
datalines;
A 2010 0
A 2012 0
A 2014 0
A 2016 1
A 2018 1
B 2010 0
B 2012 1
B 2014 0
B 2016 1
B 2018 0
C 2010 1
C 2012 0
C 2014 0
C 2016 1
C 2018 1
;

data want;
   set have;
   by patient;
   if first.patient then _iorc_ = 0;
   if Dementia then _iorc_ = 1;
   if _iorc_ = 0;
run;

 

Result:

 

Patient Year  Dementia
A       2010  0
A       2012  0
A       2014  0
B       2010  0

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 232 views
  • 0 likes
  • 2 in conversation