Hello,
I am attempting to convert this character variable called PriorWeight to a numeric variable called PriorWeight2. I have a range of weight for 292 observations, and am not sure how to include those within the If/Then statements. The range of weights include numbers from 87 to 319, with the last 4 observations having a ? as their PriorWeight, which is why I included the ELSE PriorWeight2 = .;
Here is my code:
/* Convert PriorWeight to a numeric variable */
IF PriorWeight = 87 - 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;
RUN;
I have also already tried (87:319), '87' - '319'.
Please let me know what you think!
Try this:
IF 87 <= PriorWeight <= 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;
Try this:
IF 87 <= PriorWeight <= 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;
Naturally, I like @SASKiwi's solution, above, but as a practical matter, you could just code:
PriorWeight2 = INPUT(PriorWeight,3.)
If PriorWeight is not numeric, then PriorWeight2 will also be non-numeric. The problem with the above code is that it will generate messages in the log. A clean log is the best practice, so definitely use @SASKiwi's solution, but I think it's good to understand what SAS is going to do if it tries a numeric operation on non-numeric data.
Jim
@jimbarbour - This variation of your solution will avoid log messages and would be the approach I would normally take:
PriorWeight2 = INPUT(PriorWeight,?? 3.);
Ah! Excellent. Thank you for that @SASKiwi. I've used ?? in INPUT statements, but I've not tried it in an INPUT function. That's really nice -- so compact.
Jim
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.