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

This should be a pretty easy one, but I can't find the answer anywhere...

 

I'm trying to recode into a new variable using IF-THEN-ELSE statments. How do I incorporate a range of values for a numeric variable?

 

IF walk4rec10 = . THEN wcat = .;
ELSE IF walk4rec10 = (-1, 0) THEN wcat = 0;
ELSE IF walk4rec10 = (1:3) THEN wcat = 1;
ELSE IF walk4rec10 > 3 THEN wcat = 2;

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
nehalsanghvi
Pyrite | Level 9

If walk4rec10 does not contain float values, then you could also do this:

IF walk4rec10 = . THEN wcat = .;
 ELSE IF walk4rec10 in (-1,0)  THEN wcat = 0;
 ELSE IF walk4rec10 in (1,2,3) THEN wcat = 1;
 ELSE IF walk4rec10 > 3 THEN wcat = 2;

View solution in original post

15 REPLIES 15
nehalsanghvi
Pyrite | Level 9
This should work:

IF walk4rec10 = . THEN wcat = .;
ELSE IF walk4rec10 >= -1 and walk4rec10 <=0 THEN wcat = 0;
ELSE IF walk4rec10 >= 1 and walk4rec10 <= 3 THEN wcat = 1;
ELSE IF walk4rec10 > 3 THEN wcat = 2;

nehalsanghvi
Pyrite | Level 9

If walk4rec10 does not contain float values, then you could also do this:

IF walk4rec10 = . THEN wcat = .;
 ELSE IF walk4rec10 in (-1,0)  THEN wcat = 0;
 ELSE IF walk4rec10 in (1,2,3) THEN wcat = 1;
 ELSE IF walk4rec10 > 3 THEN wcat = 2;
_maldini_
Barite | Level 11
Using the IN operator converts the variable to character...
nehalsanghvi
Pyrite | Level 9

in operator works with both numeric and character data. It does not convert anything. Can you give us the cdoe you are using?

_maldini_
Barite | Level 11

This is the original syntax I was using:

 

IF walk4rec10 = . THEN wcat = .;
ELSE IF walk4rec10 IN(-1,0) THEN wcat = 0;
ELSE IF walk4rec10 IN(1:3) THEN wcat = 1;
ELSE IF walk4rec10 > 3 THEN wcat = 2;

 

I was getting a message saying that numeric values were converted to character. 

 

When I just rewrote the syntax in order to post it here, the "conversion message" was absent. Go figure. I have no explanation for that!

 

Thanks for your help. 

nehalsanghvi
Pyrite | Level 9

Oh, I see. You had the range in the second statement with IN, which is why you probably got an unexpected result. Frankly, I did not know about that issue. But it can be easily cirucmvented by either specifying IN (1,2,3) or if it is a larger range, using the comparison  operators (> and < OR >= and <=).

Reeza
Super User

@_maldini_ I would love to see an example of a numeric variable where IN converts it to character.

nehalsanghvi
Pyrite | Level 9
🙂
ballardw
Super User

@_maldini_ wrote:

This is the original syntax I was using:

 

IF walk4rec10 = . THEN wcat = .;
ELSE IF walk4rec10 IN(-1,0) THEN wcat = 0;
ELSE IF walk4rec10 IN(1:3) THEN wcat = 1;
ELSE IF walk4rec10 > 3 THEN wcat = 2;

 

I was getting a message saying that numeric values were converted to character. 

 


Post the entire log including any messages for the code generating that message. It could well be that the conversion is happening somewhere else in the code and you are only providing part of the problem.

Also post LOG and code using code boxes brough up by the {i} menu icon. The forum message boxes sometimes get reformatted and odd artifacts have been known to appear in code.

art297
Opal | Level 21
data have;
  input IF walk4rec10;
  cards;
-5
-2
-1
0
1
2
3
4
5
;
data want;
  set have;
  IF -1 <=walk4rec10<=0 THEN wcat = 0;
  ELSE IF 1<=walk4rec10<=3 THEN wcat = 1;
  ELSE IF walk4rec10 > 3 THEN wcat = 2;
run;

Art, CEO, AnalystFinder.com

 

PGStats
Opal | Level 21

It is not clear if walk4rec is integer. This will work whether it is or not:

 

select;
	when (missing(walk4rec10)) wcat = .;
	when (walk4rec10 in (-1, 0)) wcat = 0;
	when (1 <= walk4rec10 <= 3) wcat = 1;
	otherwise wcat = 2;
	end;
PG
_maldini_
Barite | Level 11
Using the IN operator was converting the variable to character, so I abandoned it. This was my original source: http://support.sas.com/kb/13/533.html
nehalsanghvi
Pyrite | Level 9

The note seems to be specific to this scenario: "If a range is used with the IN operator and the IN operator is used again in the same step, you may receive the following error:"

 

There is no range in the code I sent. A usage of range with 'in' would look like this per the same SAS note:

 

if x in(1:3,5) then y=2;

PGStats
Opal | Level 21

Your source describes an unrelated problem corrected in SAS 9.2. The IN operator doesn't convert its operators if they are of the same type.

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 15 replies
  • 27873 views
  • 8 likes
  • 6 in conversation