1
votes

I have the following dataset where each subject ID has a corresponding value (AVAL) that is currently in a character format.

data test;
    input SUBJID$ AVAL$6.;
    cards;
    001 97.9 
    001 119 
    001 061 
    001 62 
    001 151.0 
    ;
    run;

Basically, I want to convert the AVAL variable from character to numeric. I used the following code.

data test;
set test;
AVAL=input(AVAL,1.0);
run;

When I run this code, I don't get the desired output. Also it seems like the AVAL variable is still in character format

How do I covert the AVAL variable to be in numeric format?

1

1 Answers

0
votes

Several things here.

First - I assume you can't simply change the initial input (i.e., your first data step there is to illustrate the data, not to illustrate your code). It's slightly wrong anyway, but the easiest solution would simply be to input AVAL as numeric to begin with.

Second - you cannot change a variable to numeric from character without changing the name, though you can change the name and then drop/rename to "fix" that.

Third - your input statement has the wrong informat. Left side of the . should be total width of the field in characters - so 11.5 is 4 wide, so input(charvar,4.) is correct for numbers like that. Never put anything after the ., it's so rare that would be needed nowadays that it's better to simply say never.

Your final statement would be like this:

data test;
  set test;
  AVAL_N=input(AVAL,12.); *I use 12 to make *sure* it is plenty wide, but probably you can be lower than that - whatever the widest field possible is;
  drop AVAL;
  rename AVAL_N = AVAL;  *so on the OUTPUT it is correct;
run;