Help using Base SAS procedures

If statement in data step

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

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
Super User
Posts: 7,942

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.

View solution in original post


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

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,018

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: 7,758

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
New Contributor
Posts: 2

Re: If statement in data step

Thank you very much for the prompt replies Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 711 views
  • 3 likes
  • 4 in conversation