BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
CathyVI
Lapis Lazuli | Level 10

Hello,

 

How can I create a new column for preafib and prepfo BUT ignore the missing row when assigning a value to the new variable.

without assigning a value to the new variable when both  First_Ischemic or  First_Hemorrhagic is blank. For example, ID = 032462 my code was able to calculate when afib is less then First_Ischemic or First_Hemorrhagic but it is also assigning a value for when both columns are missing. I do not want it. 

Also, ID=023434 is assigning a value of YES to preafib because  First_Ischemic = .  so it recognizes First_Ischemic to be less than First_AFIB=21JUN2003. How can I rewrite my code to ignore the missing and only calculate for the non-missing rows. Thanks

 

data a;
input ID $6. First_Ischemic First_Hemorrhagic first_AFIB first_PFO ;
format First_Ischemic First_Hemorrhagic first_AFIB first_PFO date9.;
informat First_Ischemic First_Hemorrhagic first_AFIB first_PFO date9.;
datalines;
011386   23SEP2004  10FEB2020  .   . 
034627   01DEC2009  30NOV2009  .   10FEB2020     
011427   10JUL2003   .   .   .
012666   .   18SEP2006   20JUN2002   .
023434   .   18OCT2002   21JUN2003   . 
020485   15JUL2009   .   .   .   
032462   .   .   .    20JUN2002  
011386   23SEP2004  10FEB2020  .   . 
;
run;
proc sort data=a; by id; run;
data Prepost;
set a;
by id;
if First_AFIB ne .  then do;
if First_AFIB<First_Ischemic or First_Hemorrhagic then Preafib='Yes';
else if First_AFIB> First_Ischemic or First_Hemorrhagic then Preafib='No';
end;
if first_PFO ne .  then do;
if first_PFO<First_Ischemic or First_Hemorrhagic then Prepfo='Yes';
if first_PFO> First_Ischemic or First_Hemorrhagic then Prepfo='No';
end;
run;

Output of interest

ObsIDFirst_IschemicFirst_Hemorrhagicfirst_AFIBfirst_PFOPreafibPrepfo
101138623SEP200410FEB2020..  
201138623SEP200410FEB2020..  
301142710JUL2003...  
4012666.18SEP200620JUN2002.Yes 
502048515JUL2009...  
6023434.18OCT200221JUN2003.No 
7032462...20JUN2002  
803462701DEC200930NOV2009.10FEB2020 No

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
if First_AFIB ne .  then do;
     if First_AFIB<First_Ischemic or First_AFIB<First_Hemorrhagic then Preafib='Yes';
     else if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
          (First_AFIB> First_Ischemic or First_AFIB>First_Hemorrhagic) then Preafib='No';
end;
if first_PFO ne .  then do;
    if first_PFO<First_Ischemic or first_PFO<First_Hemorrhagic then Prepfo='Yes';
    else if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and 
        (first_PFO> First_Ischemic or first_PFO<First_Hemorrhagic) then Prepfo='No';      
end;

 

 

You will find it extremely helpful if you work out the logic in your head and/or on a piece of paper, so all possible situations are accounted for, and then program from that. 

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You have to test to see if first_ischemic and first_hemmorrhagic are both missing. You haven't done that, so in your code this isn't accounted for. I think this is what you want

 

if first_PFO ne .  then do;
    if first_PFO<First_Ischemic or first_PFO<First_Hemorrhagic then Prepfo='Yes';
    if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and 
        (first_PFO> First_Ischemic or first_PFO>First_Hemorrhagic) then Prepfo='No';
end;

 

 

Also your constructed IF statement 

 

if first_PFO<First_Ischemic or First_Hemorrhagic

seems wrong to me and I have adjusted this in my code.

--
Paige Miller
CathyVI
Lapis Lazuli | Level 10

@PaigeMiller @ballardw @Tom 

What do you think is wrong here. I expected ID=012666 to be "Yes"  if First_AFIB<First_Ischemic or First_AFIB<First_Hemorrhagic but am getting a "NO". I have modified the code few times but not the right output.

data Prepost1;
set a;
by id;
if First_AFIB ne .  then do;
if First_AFIB<First_Ischemic or First_AFIB<First_Hemorrhagic then Preafib='Yes';
if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
     (First_AFIB> First_Ischemic or First_AFIB>First_Hemorrhagic) then Preafib='No';
end;
if first_PFO ne .  then do;
    if first_PFO<First_Ischemic or first_PFO<First_Hemorrhagic then Prepfo='Yes';
    if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and 
        (first_PFO> First_Ischemic or first_PFO<First_Hemorrhagic) then Prepfo='No';      
end;
run;

 

PaigeMiller
Diamond | Level 26
if First_AFIB ne .  then do;
     if First_AFIB<First_Ischemic or First_AFIB<First_Hemorrhagic then Preafib='Yes';
     else if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and
          (First_AFIB> First_Ischemic or First_AFIB>First_Hemorrhagic) then Preafib='No';
end;
if first_PFO ne .  then do;
    if first_PFO<First_Ischemic or first_PFO<First_Hemorrhagic then Prepfo='Yes';
    else if (not missing(first_ischemic) or not missing(first_Hemorrhagic)) and 
        (first_PFO> First_Ischemic or first_PFO<First_Hemorrhagic) then Prepfo='No';      
end;

 

 

You will find it extremely helpful if you work out the logic in your head and/or on a piece of paper, so all possible situations are accounted for, and then program from that. 

--
Paige Miller
ballardw
Super User

SAS very nicely provides the ability to have multiple comparisons such as   a < b < c as a simple construct.

So if you want to exclude the missing values for First_afib from returning true with code like First_AFIB<First_Ischemic then provide a lower bound for First_afib:

if 0< First_AFIB<First_Ischemic then ...

Since your values are mostly dates you could use a date literal that you expect never to appear in the data a valid such as

if '01JAN1800'd < First_AFIB<First_Ischemic then ...

 

 

Tom
Super User Tom
Super User

What test do you think this code is performing?

First_AFIB<First_Ischemic or First_Hemorrhagic

SAS will treat that as this test:

(First_AFIB<First_Ischemic) or (First_Hemorrhagic)

So if FIRST_HEMORRHAGIC is not missing and not zero (zero is the date '01JAN1960'd) then that test will be TRUE.

 

 

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 5 replies
  • 2910 views
  • 3 likes
  • 4 in conversation