data WORK. NEW; set WORK. OLD (keep=X); if X <10 then X=1; else if X >=10 and X LT 20 then X=2; else X=3; run:
In filtering the values of the variable X. What would be new value of X if it's original value was missing?
Do you have access to SAS? One way to explore this is to test it by running the code.
data WORK.OLD ;
x=. ;
run ;
data WORK.NEW;
set WORK.OLD (keep=X);
if X <10 then X=1;
else if X >=10 and X LT 20 then X=2;
else X=3;
put x= ;
run ;
The result is:
5 data WORK.NEW; 6 set WORK.OLD (keep=X); 7 if X <10 then X=1; 8 else if X >=10 and X LT 20 then X=2; 9 else X=3; 10 11 put x= ; 12 run ; x=1 NOTE: There were 1 observations read from the data set WORK.OLD. NOTE: The data set WORK.NEW has 1 observations and 1 variables.
In SAS, a missing value has a low sort order. So when X has a missing the value, the condition if X <10
will be true.
In SAS a missing value is treated as the lowest possible value so your logic will assign it the value 1. Your program needs correcting though otherwise it won't work as you expect. You need to create a new variable containing the values 1,2,3 otherwise your ELSE statements will never execute.
data WORK.NEW;
set WORK.OLD (keep=X);
if X <10 then X1=1;
else if X >=10 and X LT 20 then X1=2;
else X1=3;
run:
can you explain with a example once for more better understanding ?
@Quentin has provided an example and explanation. Does that help?
Here is an example:
data test;
do x=-1,0,1,.,.z,.a,._ ;
lessthan0 = (x < 0);
output;
end;
run;
proc sort;
by x;
run;
proc print;
run;
Result
Obs x lessthan0 1 _ 1 2 . 1 3 A 1 4 Z 1 5 -1 1 6 0 0 7 1 0
Do you have access to SAS? One way to explore this is to test it by running the code.
data WORK.OLD ;
x=. ;
run ;
data WORK.NEW;
set WORK.OLD (keep=X);
if X <10 then X=1;
else if X >=10 and X LT 20 then X=2;
else X=3;
put x= ;
run ;
The result is:
5 data WORK.NEW; 6 set WORK.OLD (keep=X); 7 if X <10 then X=1; 8 else if X >=10 and X LT 20 then X=2; 9 else X=3; 10 11 put x= ; 12 run ; x=1 NOTE: There were 1 observations read from the data set WORK.OLD. NOTE: The data set WORK.NEW has 1 observations and 1 variables.
In SAS, a missing value has a low sort order. So when X has a missing the value, the condition if X <10
will be true.
First fix the code by removing the space after WORK. in the DATA and SET statements. (Or just remove the WORK. since using a one level name generally implies a dataset in the WORK library.)
All 28 missing values are less than any actual number.
So X will be 1 since . and .A and .Z and ._ and the other 24 missing values are all less than 10.
Note that unlike some languages SAS only uses BOOLEAN logic. It does not use TRI-LEVEL logic. A boolean expression always evaluates as either TRUE or FALSE, it is never MISSING even when missing values are involved.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: