BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ssills24
Calcite | Level 5

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! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Try this:

IF 87 <= PriorWeight <= 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star

Try this:

IF 87 <= PriorWeight <= 319 THEN PriorWeight2 = INPUT(PriorWeight,3.);
ELSE PriorWeight2 = . ;
jimbarbour
Meteorite | Level 14

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

SASKiwi
PROC Star

@jimbarbour  - This variation of your solution will avoid log messages and would be the approach I would normally take:

PriorWeight2 = INPUT(PriorWeight,?? 3.);
jimbarbour
Meteorite | Level 14

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