turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- If statement in data step

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-18-2015 10:08 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to YasodaJ

05-18-2015 10:26 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to YasodaJ

05-18-2015 10:26 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to YasodaJ

05-18-2015 10:27 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to YasodaJ

05-18-2015 11:56 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to YasodaJ

05-21-2015 05:11 AM

Thank you very much for the prompt replies