- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Look at the changes I've made. This will hopefully explain to you why things didn't work as desired.
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Look at the changes I've made. This will hopefully explain to you why things didn't work as desired.
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.