BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jiangmi
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

4 REPLIES 4
umeshMahajan
Fluorite | Level 6

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;
jiangmi
Calcite | Level 5
shouldn't SAS ignore the wrongful comparison anyway?
Tom
Super User Tom
Super User

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.

jiangmi
Calcite | Level 5
Thanks for the detailed explanation.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 909 views
  • 0 likes
  • 3 in conversation