BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nipun22
Obsidian | Level 7
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?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

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:

 

Nipun22
Obsidian | Level 7

can you explain with a example once for more better understanding ?

 

PaigeMiller
Diamond | Level 26

@Quentin has provided an example and explanation. Does that help?

--
Paige Miller
Tom
Super User Tom
Super User

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
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
Tom
Super User Tom
Super User

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.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1639 views
  • 0 likes
  • 5 in conversation