BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

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

ObsIDFirst_IschemicFirst_Hemorrhagicfirst_SeizureR_Seizure_12first_DementiaR_Dementia_12PostStroke_SR_DEMENTIA_2PostStroke_D
101138623SEP200410FEB2020.... . 
201139623SEP200410FEB2020.... . 
301142711SEP201009AUG201010SEP201003FEB2012..Yes. 
4012666.18SEP200620JUN2002...No. 
502048515JUL2019...15AUG200925JUL2010 .No
6023434.18OCT200221JUN2003...Yes. 
703246213AUG2014.12AUG201420JUN2002..No. 
803462701DEC200930NOV2009.10FEB2020.. . 

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Sarath_A_SAS
Obsidian | Level 7

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;

Sarath_A_SAS
Obsidian | Level 7
 

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: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 289 views
  • 0 likes
  • 3 in conversation