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

Hello guys,

I wanted to build this logic but for some reasons it's been giving me a missing. The variable N_NUM is a continuous variable (some values have decimals eg 0.5. 0.6 etc.) that I'm trying to recategorize to 0-149, 150-500, and > 500. I ran this code but it's been giving me missing values (.) instead of '0', while the other cases worked. What could be the cause of this?

Data B; set  A;
If M_NUM < 150 then count= 0;
if 150 >= M_NUM  <=500 then count= 1;
if M_NUM  > 500 then count= 2;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Look at the changes I've made. This will hopefully explain to you why things didn't work as desired.

Patrick_1-1650836933355.png

The added ELSE statements are cleaner coding practice, what really hit you was the wrong condition.

 

data a;
  do m_num=.,149,150,151,499,500,501;
    output;
  end;
run;

data b;
  set  a;
  if m_num < 150 then count= 0;
  else if 150 <= m_num  <=500 then count= 1;
  else if m_num  > 500 then count= 2;
run;

proc print data=b;
run;

Patrick_0-1650836882812.png

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Here's a useful debugging technique. You look at the data set B with your own eyes, and see if there is a pattern as to what values of M_NUM produce the missing values instead of 0 (also your code will never produce '0' as the value of COUNT).

 

If you are still stumped, SHOW US several of the M_NUM values that result in a value of COUNT=.

--
Paige Miller
Patrick
Opal | Level 21

Look at the changes I've made. This will hopefully explain to you why things didn't work as desired.

Patrick_1-1650836933355.png

The added ELSE statements are cleaner coding practice, what really hit you was the wrong condition.

 

data a;
  do m_num=.,149,150,151,499,500,501;
    output;
  end;
run;

data b;
  set  a;
  if m_num < 150 then count= 0;
  else if 150 <= m_num  <=500 then count= 1;
  else if m_num  > 500 then count= 2;
run;

proc print data=b;
run;

Patrick_0-1650836882812.png

 

tarheel13
Rhodochrosite | Level 12

because in SAS, . is less than 0. you could add a where statement for where ^missing(m_num). otherwise, you could put your statements in a do block: if ^missing(m_num) then do; end;

 

I also highly recommend checking your work with proc freq or proc means like this: 

proc means data=b n nmiss min max;
class count / missing;
var m_num;
run;

proc freq data=B;
tables m_num*count / list missing;
run;

both of these would allow you to see if the count variable was derived correctly. 

Tom
Super User Tom
Super User

If you are unsure how a complex condition will be evaluated on trick you can do is to format it as a WHERE statement and run it.   SAS will then print the its interpretation of the condition as a NOTE in the log.

10    Data B; set  A;
11    where 150 >= M_NUM  <=500 ;
12    run;

NOTE: There were 1 observations read from the data set WORK.A.
      WHERE M_NUM<=150;
NOTE: The data set WORK.B has 1 observations and 1 variables.

You can see that SAS has reduced the test for whether M_NUM is both less than or equal to 150 and less than or equal to 500 to the simpler single test that M_NUM is less than or equal to 150.

 

I suspect that you meant 150 <= M_NUM <= 500 instead.

14    Data B; set  A;
15    where 150 <= M_NUM  <=500 ;
16    run;

NOTE: There were 0 observations read from the data set WORK.A.
      WHERE (M_NUM>=150 and M_NUM<=500);
NOTE: The data set WORK.B has 0 observations and 1 variables.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 801 views
  • 2 likes
  • 5 in conversation