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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.