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
- /
- Base SAS Programming
- /
- IF THEN Confusion.

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-25-2016 11:42 AM

data have;

input type$ @@;

datalines;

A B

;

data next;

set have;

if type=. or type=' ' then type_ind=0;

else if type='A' then type_ind=1;

else type_ind=2;

run;

can somebody tell me whey type_ind return all 0?

I know remove 'type=.' will fix the problem,

but how that piece of code affects the results?

Shouldn't it be ignored in the process?

Thanks.

Joe

Accepted Solutions

Solution

05-25-2016
12:13 PM

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

Posted in reply to jiangmi

05-25-2016 12:10 PM

You asked SAS to convert your character variable TYPE to a number so that it could compare it the numeric missing value. Since 'A' and 'B' cannot convert to numbers the result is a missing value. Hence your IF condition is true. Either use the MISSING() function as it will handle both numeric and character values.

`if missing(TYPE) then ...`

You clearly know that TYPE is character since your other IF statements reference values like 'A', so why did you try to compare TYPE to numeric missing value? Perhaps you do not understand how SAS stores character variables? SAS stores character variables as fixed length strings that automatically padded on the right with spaces. It will trim trailing spaces in most comparisons automatically and treats a values that only contains spaces the same as a missing value.

All Replies

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

Posted in reply to jiangmi

05-25-2016 11:53 AM

This i sbecause you care comparing character variable with numerical value. This will work

```
data have;
input type $ @@;
datalines;
A B
;
data next;
set have;
if type=' ' or missing(type) then type_ind=0;
else if type='A' then type_ind=1;
else type_ind=2;
run;
```

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

Posted in reply to umeshMahajan

05-25-2016 11:59 AM

shouldn't SAS ignore the wrongful comparison anyway?

Solution

05-25-2016
12:13 PM

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

Posted in reply to jiangmi

05-25-2016 12:10 PM

You asked SAS to convert your character variable TYPE to a number so that it could compare it the numeric missing value. Since 'A' and 'B' cannot convert to numbers the result is a missing value. Hence your IF condition is true. Either use the MISSING() function as it will handle both numeric and character values.

`if missing(TYPE) then ...`

You clearly know that TYPE is character since your other IF statements reference values like 'A', so why did you try to compare TYPE to numeric missing value? Perhaps you do not understand how SAS stores character variables? SAS stores character variables as fixed length strings that automatically padded on the right with spaces. It will trim trailing spaces in most comparisons automatically and treats a values that only contains spaces the same as a missing value.

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

05-25-2016 12:13 PM

Thanks for the detailed explanation.