- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this:
IF 87 <= PriorWeight <= 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this:
IF 87 <= PriorWeight <= 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@jimbarbour - This variation of your solution will avoid log messages and would be the approach I would normally take:
PriorWeight2 = INPUT(PriorWeight,?? 3.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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