Solved
New Contributor
Posts: 2

# If statement in data step

Hello,

I'm preparing for SAS base certification and I require some clarification regarding the below question.

Data Step

Data work.Test;

Infile ‘c:\class1.csv’ dsd;

Input Name \$ Sex \$ Age Height;

If Age NE 16 and Age NE 15 then Group = 1;

Else Group = 2;

Q Which of the following assignment statements for variable group are functionally equivalent to the original statement used in the above data step?

Below are 2 of the answers give. The correct answer is answer B. I understand why answer B is correct. However can someone please explain why answer A is wrong? My argument is a person can't have 2 age values, therefore answer A should work too.

A) if (Age NE 16) or (Age NE 15) then Group=1; else Group=2;

B) if Age not in(15,16) then Group=1; else Group=2

Any help regarding this is appreciated. Thank you in advance.

Accepted Solutions
Solution
‎05-18-2015 10:26 AM
Super User
Posts: 9,599

## Re: If statement in data step

Ah yes.  Its actually so subtle I didn't see it on a first read through.  They have switched the logic gate in A:

If Age=16, then the logic:

(age ne 16)=0, (age ne 15)=1 therefore 0 or 1 =1

If age=15 then:

(age ne 16)=1, (age ne 15)=0 therefore 1 or 0 =1

In the original code:

If Age=16, then the logic:

age ne 16=0, age ne 1=1 therefore 0 and 1 =0

If age=15 then:

age ne 16=1, age ne 15=0 therefore 1 and 0 =0

You see the switch from the and to the or reverses the result.

Plus the fact that in() reads easier and is expandable.  And the original is two tests, the in() is one.

All Replies
Solution
‎05-18-2015 10:26 AM
Super User
Posts: 9,599

## Re: If statement in data step

Ah yes.  Its actually so subtle I didn't see it on a first read through.  They have switched the logic gate in A:

If Age=16, then the logic:

(age ne 16)=0, (age ne 15)=1 therefore 0 or 1 =1

If age=15 then:

(age ne 16)=1, (age ne 15)=0 therefore 1 or 0 =1

In the original code:

If Age=16, then the logic:

age ne 16=0, age ne 1=1 therefore 0 and 1 =0

If age=15 then:

age ne 16=1, age ne 15=0 therefore 1 and 0 =0

You see the switch from the and to the or reverses the result.

Plus the fact that in() reads easier and is expandable.  And the original is two tests, the in() is one.

Super User
Posts: 10,761

## Re: If statement in data step

Simple Q:

it should be

if (Age NE 16) and (Age NE 15) then Group=1; else Group=2;

if age=15  or if age=16

the condition :"(Age NE 16) or (Age NE 15) " will be true .

Xia Keshan

Super User
Posts: 10,209

## Re: If statement in data step

A) if (Age NE 16) or (Age NE 15)

is always true. If a person's age is 15, the first half is true, if 16, the second half is true, and with all other ages both parts are true. group will always be set to 1.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 2

## Re: If statement in data step

Thank you very much for the prompt replies

🔒 This topic is solved and locked.