DATA Step, Macro, Functions and more

IF-THE-ELSE Numeric Range

Accepted Solution Solved
Reply
Regular Contributor
Posts: 199
Accepted Solution

IF-THE-ELSE Numeric Range

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!


Accepted Solutions
Solution
‎03-03-2017 12:57 PM
Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

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


All Replies
Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

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;

Solution
‎03-03-2017 12:57 PM
Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

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;
Regular Contributor
Posts: 199

Re: IF-THE-ELSE Numeric Range

Using the IN operator converts the variable to character...
Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

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

Regular Contributor
Posts: 199

Re: IF-THE-ELSE Numeric Range

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. 

Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

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 <=).

Super User
Posts: 17,780

Re: IF-THE-ELSE Numeric Range

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

Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

Smiley Happy
Super User
Posts: 10,483

Re: IF-THE-ELSE Numeric Range


jcorroon 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.

PROC Star
Posts: 7,358

Re: IF-THE-ELSE Numeric Range

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

 

Respected Advisor
Posts: 4,641

Re: IF-THE-ELSE Numeric Range

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
Regular Contributor
Posts: 199

Re: IF-THE-ELSE Numeric Range

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
Frequent Contributor
Posts: 75

Re: IF-THE-ELSE Numeric Range

[ Edited ]

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;

Respected Advisor
Posts: 4,641

Re: IF-THE-ELSE Numeric Range

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
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 456 views
  • 8 likes
  • 6 in conversation