Hello,
I had a more difficult task here compared with the initial one which I got help. Please help. Thanks
I want to assign values for id where seizure and dementia MUST occur before the stroke event. There are two different variable (dates) for seizure and dementia.
Code not given the right output. For ID= 012666 I want PostStroke_S = 'No' since First_seizure is less than First_Hemorrhagic.
For ID=020485, I want PostStroke_D='No' since First_Ischemic is greater than First_dementia and R_dementia_12
For ID=032462, I want PostStroke_S ='No' since first_seizure is less than first_ischemic
Here is my code
data a;
input ID $6. First_Ischemic First_Hemorrhagic first_Seizure R_Seizure_12 first_Dementia R_Dementia_12 ;
format First_Ischemic First_Hemorrhagic first_Seizure R_Seizure_12 first_Dementia R_Dementia_12 date9.;
informat First_Ischemic First_Hemorrhagic first_Seizure R_Seizure_12 first_Dementia R_Dementia_12 date9.;
datalines;
011396 23SEP2004 10FEB2020 . . . .
034627 01DEC2009 30NOV2009 . 10FEB2020 . .
011427 11SEP2010 09AUG2010 10SEP2010 03FEB2012 . .
012666 . 18SEP2006 20JUN2002 . . .
023434 . 18OCT2002 21JUN2003 . . .
020485 15JUL2019 . . . 15AUG2009 25JUL2010
032462 13AUG2014 . 12AUG2014 20JUN2002 . .
011386 23SEP2004 10FEB2020 . . . .
;
run;
proc sort data=a; by id; run;
data SDpost ;
set a;
by id;
if First_Seizure ne . then do;
if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
(First_Seizure>First_Ischemic or First_Seizure>First_Hemorrhagic or
R_Seizure_12>First_Ischemic or R_Seizure_12>First_Hemorrhagic) then PostStroke_S='Yes';
else if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
(First_Seizure<First_Ischemic or First_Seizure<First_Hemorrhagic or
R_Seizure_12<First_Ischemic or R_Seizure_12<First_Hemorrhagic) then PostStroke_S='No';
end;
if First_Dementia ne . then do;
if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
(First_Dementia>First_Ischemic or First_Dementia>First_Hemorrhagic or
R_DEMENTIA_2>First_Ischemic or R_DEMENTIA_2>First_Hemorrhagic) then PostStroke_D='Yes';
else if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
(First_Dementia<First_Ischemic or First_Dementia<First_Hemorrhagic or
R_DEMENTIA_2<First_Ischemic or R_DEMENTIA_2<First_Hemorrhagic) then PostStroke_D='No';
end;
run;
Output of interest
Obs | ID | First_Ischemic | First_Hemorrhagic | first_Seizure | R_Seizure_12 | first_Dementia | R_Dementia_12 | PostStroke_S | R_DEMENTIA_2 | PostStroke_D |
1 | 011386 | 23SEP2004 | 10FEB2020 | . | . | . | . | . | ||
2 | 011396 | 23SEP2004 | 10FEB2020 | . | . | . | . | . | ||
3 | 011427 | 11SEP2010 | 09AUG2010 | 10SEP2010 | 03FEB2012 | . | . | Yes | . | |
4 | 012666 | . | 18SEP2006 | 20JUN2002 | . | . | . | No | . | |
5 | 020485 | 15JUL2019 | . | . | . | 15AUG2009 | 25JUL2010 | . | No | |
6 | 023434 | . | 18OCT2002 | 21JUN2003 | . | . | . | Yes | . | |
7 | 032462 | 13AUG2014 | . | 12AUG2014 | 20JUN2002 | . | . | No | . | |
8 | 034627 | 01DEC2009 | 30NOV2009 | . | 10FEB2020 | . | . | . |
So in your last thread, I urged you to create a diagram or table of all possibilities, and the desired outcome for each. Show us that.
Check this one..
data SDpost;
set a;
by id;
length PostStroke_S PostStroke_D $20;
/* Initialize variables */
PostStroke_S = ' ';
PostStroke_D = ' ';
/* Logic for Seizure */
if First_Seizure ne . then do;
/* Check if seizure occurred before any stroke event */
if (not missing(First_Ischemic) and First_Seizure < First_Ischemic) or
(not missing(First_Hemorrhagic) and First_Seizure < First_Hemorrhagic) then
PostStroke_S = 'No';
else
PostStroke_S = 'Yes';
end;
/* Logic for Dementia */
if First_Dementia ne . then do;
/* Check if dementia occurred before any stroke event */
if (not missing(First_Ischemic) and First_Dementia < First_Ischemic) or
(not missing(First_Hemorrhagic) and First_Dementia < First_Hemorrhagic) or
(not missing(R_Dementia_12) and R_Dementia_12 < First_Ischemic) or
(not missing(R_Dementia_12) and R_Dementia_12 < First_Hemorrhagic) then
PostStroke_D = 'No';
else
PostStroke_D = 'Yes';
end;
run;
data SDpost; set a; by id; length PostStroke_S PostStroke_D $20; /* Initialize variables */ PostStroke_S = ' '; PostStroke_D = ' '; /* Logic for Seizure */ if First_Seizure ne . then do; /* Check if seizure occurred before any stroke event */ if (not missing(First_Ischemic) and First_Seizure < First_Ischemic) or (not missing(First_Hemorrhagic) and First_Seizure < First_Hemorrhagic) then PostStroke_S = 'No'; else PostStroke_S = 'Yes'; end; /* Logic for Dementia */ if First_Dementia ne . then do; /* Check if dementia occurred before any stroke event */ if (not missing(First_Ischemic) and First_Dementia < First_Ischemic) or (not missing(First_Hemorrhagic) and First_Dementia < First_Hemorrhagic) or (not missing(R_Dementia_12) and R_Dementia_12 < First_Ischemic) or (not missing(R_Dementia_12) and R_Dementia_12 < First_Hemorrhagic) then PostStroke_D = 'No'; else PostStroke_D = 'Yes'; end; run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.