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:
Patient | Year | Dementia |
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 |
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:
Patient | Year | Dementia |
A | 2010 | 0 |
A | 2012 | 0 |
A | 2014 | 0 |
B | 2010 | 0 |
Can anyone help me with this coding? Thank you.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: