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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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