Search code examples
sascharacternumeric

how to covert a character to numeric variable in SAS


I am trying to convert the variable "MAGE" from character to numeric in SAS

I tried new_MAGE = input(MAGE, informat.);

and received and an error


Solution

  • You need to use an actual informat specification, not the string informat..

    For most values try the normal numeric informat. The INPUT() function does not care if you use a width on the informat specification that is larger than the length of the string being read. So just use:

    data want;
      set have;
      new_MAGE = input(MAGE, 32.);
    run;
    

    If the values in MAGE have thousands separators then you might want to use the COMMA informat instead.

    data want;
      set have;
      new_MAGE = input(MAGE, comma32.);
    run;