- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
in operator works with both numeric and character data. It does not convert anything. Can you give us the cdoe you are using?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 <=).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@_maldini_ I would love to see an example of a numeric variable where IN converts it to character.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@_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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.