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!
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;
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;
in operator works with both numeric and character data. It does not convert anything. Can you give us the cdoe you are using?
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.
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 <=).
@_maldini_ I would love to see an example of a numeric variable where IN converts it to character.
@_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.
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
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;
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;
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.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: