if .... then .... and....or....

Reply
Contributor
Posts: 68

if .... then .... and....or....

Hi guys, a question is seeking for your help. Thank you in advance!

Method1

define Hypertensive/ computed 'Hypertensive?' width=14;

compute Hypertensive / character length=3 ;

if Gender=F then do;

  if SBP gt 138 or DBP gt 88 then Hypertensive='Yes';

  else Hypertensive='No';

    end;

else do;

  if SBP gt 140 or DBP gt 90 then Hypertensive='Yes';

   else  Hypertensive='No';

    end;

endcomp;

run;

The result is:

Gender    SBP    DBP  Hypertensive?

  F         110     62  No

            120     70  No

            138     88  No

            132     76  No

  M         144     90  Yes

            130     80  No

            142     82  Yes

            150     96  Yes

Method2

compute Hypertensive / character length=3;

if Gender = 'F' and (SBP gt 138 or DBP gt 88)

then Hypertensive = 'Yes';

else Hypertensive='No';

if Gender = 'M' and

(SBP gt 140 or DBP gt 90)

then Hypertensive = 'Yes';

else Hypertensive = 'No';

endcomp;

run;

The result is:

Hypertensice Patients                                  

  Gender    SBP    DBP  Hypertensive?
  F         110     62  No
            120     70  No
            138     88  No
            132     76  No
  M         144     90  Yes
            130     80  No
            142     82  No
            150     96 No                                  My question is: I think the two methods will get the same result, but why it's different?

The original requirement is:

Hypertensive is defined as Yes for females if the SBP is

greater than 138 or the DBP is greater than 88 and No otherwise. For males,

Hypertensive is defined as Yes if the SBP is over 140 or the DBP is over

90 and No otherwise.

PROC Star
Posts: 7,437

Re: if .... then .... and....or....

I'm leaving my response, below, even though it is clearly incorrect.  identified the discrepancy between the methods in his post.

---------------------------

The only difference I see between the two, other than the obvious change in method, is the fact that in method1 you forgot to quote the letter F.

However, you would have gotten identical results regardless.

I think there is something else different between the two sets of code that you actually ran and that which you posted.

Respected Advisor
Posts: 4,826

Re: if .... then .... and....or....

The two logical tests are NOT equivalent. In method 2 the condition which should be applied to males only is also applied to females. The first part of the test if Gender = 'M' and (SBP gt 140 or DBP gt 90) fails for females and the else clause is executed, yielding Hypersensitive = 'No'.

PG

PG
Contributor
Posts: 68

Re: if .... then .... and....or....

Hi PG, Still very confused:smileyconfused::smileyconfused::smileyconfused:, can you please give some more explanations? Thank you!

Super User
Posts: 11,141

Re: if .... then .... and....or....

You may want to look up "truth tables" for evaluating combinations of AND and OR conditions. Building these for programming is a very useful skill.

Contributor
Posts: 68

Re: if .... then .... and....or....

Hi ballardw, what does "truth table" here mean? and how to build this skills? Thank you.

Super User
Posts: 11,141

Re: if .... then ....  and....or....

Truth table is one approach to learning how to evaluate logical comparison statements, especially involving AND, OR, NOT and compounds of those logical operators. Search online for examples as this was an entire section in an Intro to Logic course in college.

Respected Advisor
Posts: 4,826

Re: if .... then .... and....or....

I cannot reproduce your example. For the data illustrating your example, both codes yield the same result. The discrepancy between the two tests occurs for hypertensive females (which are absent from your example). Run this example:

data _null_;

input Gender $   SBP    DBP;

if Gender = 'F' and (SBP gt 138 or DBP gt 88)

    then Hypertensive1 = 'Yes';

    else Hypertensive1 = 'No';

put gender SBP DBP Hypertensive1=;

if Gender = 'M' and (SBP gt 140 or DBP gt 90)

    then Hypertensive1 = 'Yes';

    else Hypertensive1 = 'No';

put gender SBP DBP Hypertensive1=;

if Gender="F" then do;

    if SBP gt 138 or DBP gt 88

        then Hypertensive2 = 'Yes';

        else Hypertensive2 = 'No';

    end;

else do;

    if SBP gt 140 or DBP gt 90

        then Hypertensive2 = 'Yes';

        else Hypertensive2 = 'No';

    end;

put gender SBP DBP Hypertensive1= Hypertensive2=;

datalines;

  F         110     62

  F         140     70

  M         130     80

  M         144     90

  ;

You should get

F 110 62 Hypertensive1=No

F 110 62 Hypertensive1=No

F 110 62 Hypertensive1=No Hypertensive2=No

F 140 70 Hypertensive1=Yes                      <-- correct result

F 140 70 Hypertensive1=No                       <-- overturned

F 140 70 Hypertensive1=No Hypertensive2=Yes

M 130 80 Hypertensive1=No

M 130 80 Hypertensive1=No

M 130 80 Hypertensive1=No Hypertensive2=No

M 144 90 Hypertensive1=No

M 144 90 Hypertensive1=Yes

M 144 90 Hypertensive1=Yes Hypertensive2=Yes

For females, the result of the first test is overturned by the second test which always yields Hypertensive="No" (for females).

PG

PG
Contributor
Posts: 68

Re: if .... then .... and....or....

Hi PG,

It looks like that the obs "M 144 90 Yes"  is a rewrote result. I'm wondering if this obs can be rewrote based on the condition of

if Gender = 'F' and (SBP gt 138 or DBP gt 88)

then Hypertensive = 'Yes';

else Hypertensive='No';


Why not other Male obs can be rewrote?

Respected Advisor
Posts: 4,826

Re: if .... then .... and....or....

You are right. Male Hypertensive value will sometimes be wrong from the Female test but will get overwritten to the correct value by the Male test.

The flaw in your testing is that both conditions (Male and Female hypertensive) are applied to both genders.

PG

PG
Contributor
Posts: 68

Re: if .... then .... and....or....

I still don't know why the last two observations of male in method 2 are "no" since the first observations of male has had a value of 'yes', which in my opinion it means that the condition "if Gender = 'M' and (SBP gt 140 or DBP gt 90) then Hypertensive = 'Yes'; else Hypertensive = 'No';" has already taken effect.

If it's already taken effect, why the condition only applies on the first or two observations of male, not applies on the last two observations of male?

Trusted Advisor
Posts: 3,208

Re: if .... then .... and....or....

The if then else with nesting construct is a notorious one for errors when combined as others.
Method2 is a wrong coded one a pgstats already. The values for F would never able to get an YES.
In this example I do not get why you got those results.  It is part of proc report.

I more logical way to get reliable results is not combining the negative/positive tests.

Method 3;

compute Hypertensive / character length=3;

Hypertensive='No';

if ( Gender = 'F' and (SBP gt 138 or DBP gt 88) ) then Hypertensive = 'Yes';

if ( Gender = 'M' and (SBP gt 140 or DBP gt 90) ) then Hypertensive = 'Yes';

---->-- ja karman --<-----
PROC Star
Posts: 7,437

Re: if .... then .... and....or....

While I incorrectly responded to your original post, I don't think you need an example. Rather, just think through what your code is doing.

/*Method 1*/

if Gender='F' then do;

  if SBP gt 138 or DBP gt 88 then Hypertensive='Yes';

  else Hypertensive='No';

end;

else do;

  if SBP gt 140 or DBP gt 90 then Hypertensive='Yes';

  else  Hypertensive='No';

end;

   

/*Method 2*/

if Gender = 'F' and (SBP gt 138 or DBP gt 88) then Hypertensive = 'Yes';

else Hypertensive='No';

/*in the above statement all records that don't meet the above condition*/

/*will be assigned Hypertensive='No' .. including those where Gender NE 'F'*/

if Gender = 'M' and (SBP gt 140 or DBP gt 90) then Hypertensive = 'Yes';

else Hypertensive = 'No';

/*in the above statement all records that don't meet the above condition*/

/*will be assigned Hypertensive='No' .. including those records where*/

/*Gender EQ 'F'*/

Contributor
Posts: 68

Re: if .... then .... and....or....

Hi Arthur,

It looks like that the obs "M 144 90 Yes"  is a rewrote result. I'm wondering if this obs can be rewrote based on the condition of

if Gender = 'F' and (SBP gt 138 or DBP gt 88)

then Hypertensive = 'Yes';

else Hypertensive='No';


Why not other Male obs can be rewrote?

PROC Star
Posts: 7,437

Re: if .... then .... and....or....

I'm not sure what you mean by "rewrote". As pointed out, none of us get the same results as you indicated you had (i.e., where males 3 and 4 get a value of NO using method2.

In your code for method2, the first if-then statements (the one you intended for Females but captures both males and females) becomes irrelevant as the second set of if-then statements determine the values for both Females and Males.

When I run that code I get:

F No

F No

F No

F No

M Yes

M No

M Yes

M Yes

i.e., the correct results, but for the wrong reason. Specifically, Females will get a value of NO even if they met the desired conditions.

Post the actual data and code you used to get the results you described.

Ask a Question
Discussion stats
  • 14 replies
  • 499 views
  • 0 likes
  • 5 in conversation