Convert Numeric to Character 20

17,266

Solution 1

While this thread is already dead, I thought I'd way in and answer why the 14 digits conversion became in E notation.

Typically, or rather, unless otherwise specified, numeric formats in SAS use BEST12 format. As such, when a numeric value is longer than 12 characters (including any commas and periods), BEST12 chooses E notation as the best way to format the value.

The input function, in that case receives the formatted value put(acctnum, BEST12.). There would've been 2 ways around it.

Either use

input(put(acctnum, 14.), $20.);

Or, change the format of the variable using the format statement (directly in a data step or with proc datasets like) - this has the added benefit that if you open the table in SAS, you will see the 14 digits and not the scientific formatted value.

proc datasets library=work nolist;
    modify dsname;
        format acctnum 14.;
run;

Vincent

Solution 2

Try this:

data WORK.T82APR ;       
set WORK.T82APR;
acctnum = put(F1, $20.);
rename f2 = tariff;
run;

Ok, I didn't pay attention to your own rename statement, so I adjusted my answer to reflect that now.

Share:
17,266
Robert Garner
Author by

Robert Garner

Updated on June 04, 2022

Comments

  • Robert Garner
    Robert Garner almost 2 years

    Hi I am building a dataset, but the data I am merging is in different formats.

    From the Excel sheet i import its in numeric 8, and the other 2 datasets im merging to are character 20, so I want to change the numeric 8 to char 20.

    How can I change the variable acctnum, to char 20? (I also want to keep this as its name, as I presume a new variable will be created)

    data WORK.T82APR;       
    set WORK.T82APR;
    rename F1 = acctnum f2 = tariff;
    run;
    
    proc contents data=T82APR;
    run;
    
  • Robert Garner
    Robert Garner almost 13 years
    Sorry for not replying sooner, I have been away. I've managed to convert from numeric to char, like this: data WORK.T82APR; set WORK.T82APR; acctnum1=input(acctnum, $20.); tariff1=input(tariff, $2.); drop acctnum tariff; rename acctnum1 = acctnum tariff1 = tariff; run; However, the format of the number is incorrect, it appears like this 3.0184403E13 rather than the 14 digits its meant to be. Any Clues?
  • Robert Garner
    Robert Garner almost 13 years
    I've worked this out now. I saved the xls as csv, and imported identifying the variables as char $20. Why didnt I do this to start with!?
  • Yoh
    Yoh almost 13 years
    Good to read it works out for you now. Maybe post your own answer including the way you worked it out for future reference for people looking for the same solution...