Help using Base SAS procedures

Convert CHAR to NUMERIC

Reply
Regular Contributor
Posts: 173

Convert CHAR to NUMERIC

Hello everyone,

I am using INPUT( ) to convert a character column to numeric. But it's not working as I expected.

second_prin_bal = input(first_pirn_bal2, ??8.);

a few examples:

first_prin_bal2: NULL second_prin_bal: .
first_prin_bal2: 19,856 second_prin_bal: .
first_prin_bal2: NULL second_prin_bal: .
first_prin_bal2: NULL second_prin_bal: .
first_prin_bal2: 74,246 second_prin_bal: .
first_prin_bal2: NULL second_prin_bal: .

How can I convert this properly?
Thanks in advance for any input!
Respected Advisor
Posts: 3,777

Re: Convert CHAR to NUMERIC

Use COMMA INFORMAT.
Frequent Contributor
Posts: 127

Re: Convert CHAR to NUMERIC

Hi KevinC,

You can use the following statement:

if input(first_prin_bal2, ?? best.) > .z then second_prin_bal2=first_prin_bal2+0;

Kind regards,
Florent
Regular Contributor
Posts: 173

Re: Convert CHAR to NUMERIC

Thank you both for your response!!
I played with it and I got it to work using put(xxx,??comma8.);

Thanks agian to everyone !
PROC Star
Posts: 1,561

Re: Convert CHAR to NUMERIC

While on the topic, and unrelated to the comma informat, but related to the subject:

1- best. in not an official informat
2- best. is not even the best informat for reading numbers.
3- 32. is the best informat for reading numbers
4- 32. still generates erroneous readings for large numbers as only part of the number is read
5- Letting sas do the conversion by adding +0 to a string as above is the only fool-proof solution, but generates the infamous message [pre] NOTE: Character values have been converted to numeric values at the places given by:[/pre]6- I reckon sas should provide a to_number() function so all normal (no comma etc) numbers can safely be read with no noxious log message.

Code:
[pre]
data _null_;
format Y best32.;
input X $40. COMMENT $40.; put COMMENT;
Y=X+0; put '0 ' X= Y=;
Y=input(X,?? best32.); put '1 ' X= Y=;
Y=input(X,?? 32. ); put '2 ' X= Y=;
cards;
1234567890123456789012345678901234567890 columns (integer >32)
123456798012345678901234567980 integer ≤32
1234567980123456789.1234567890123456789 integer part ≤32
1234567980123456789012345679801234567.8 integer part >32
0.0000000000000000001234567890123456789 small decimal
0.0000000000000000000000000000000000012 very small decimal
1234567980123456789012345678e10 positive scientific
12345679801234567890123456789012e10 positive scientific length>32
1234567980123456789012345678e-10 negative scientific
1234567980123456789012345678901e-10 negative scientific length>32
run;
[/pre]

Result:
[pre]
columns (integer >32)
0 X=1234567890123456789012345678901234567890 Y=1.2345678901234E39 => correct
1 X=1234567890123456789012345678901234567890 Y=12345678901234566802609541218304 => incorrect

2 X=1234567890123456789012345678901234567890 Y=12345678901234566802609541218304 => incorrect

integer ≤32
0 X=123456798012345678901234567980 Y=123456798012345682323755237376 => correct
1 X=123456798012345678901234567980 Y=123456798012345682323755237376 => correct
2 X=123456798012345678901234567980 Y=123456798012345682323755237376 => correct

integer part ≤32
0 X=1234567980123456789.1234567890123456789 Y=1234567980123456768 => correct
1 X=1234567980123456789.1234567890123456789 Y=1234567980123456768 => correct
2 X=1234567980123456789.1234567890123456789 Y=1234567980123456768 => correct

integer part >32
0 X=1234567980123456789012345679801234567.8 Y=1.2345679801234E36 => correct
1 X=1234567980123456789012345679801234567.8 Y=12345679801234567106475616894976 => incorrect
2 X=1234567980123456789012345679801234567.8 Y=12345679801234567106475616894976 => incorrect

small decimal
0 X=0.0000000000000000001234567890123456789 Y=1.2345678901234E-19 => more precision
1 X=0.0000000000000000001234567890123456789 Y=1.23456789012E-19 => less precision
2 X=0.0000000000000000001234567890123456789 Y=1.23456789012E-19 => less precision

very small decimal
0 X=0.0000000000000000000000000000000000012 Y=1.2E-36 => correct
1 X=0.0000000000000000000000000000000000012 Y=0 => incorrect
2 X=0.0000000000000000000000000000000000012 Y=0 => incorrect

positive scientific
0 X=1234567980123456789012345678e10 Y=1.2345679801234E37 => correct
1 X=1234567980123456789012345678e10 Y=1.2345679801234E37 => correct
2 X=1234567980123456789012345678e10 Y=1.2345679801234E37 => correct

positive scientific length>32
0 X=12345679801234567890123456789012e10 Y=1.2345679801234E41 => correct
1 X=12345679801234567890123456789012e10 Y=12345679801234567106475616894976 => incorrect
2 X=12345679801234567890123456789012e10 Y=12345679801234567106475616894976 => incorrect

negative scientific
0 X=1234567980123456789012345678e-10 Y=123456798012345680 => correct
1 X=1234567980123456789012345678e-10 Y=123456798012345680 => correct
2 X=1234567980123456789012345678e-10 Y=123456798012345680 => correct

negative scientific length>32
0 X=1234567980123456789012345678901e-10 Y=123456798012345679872 => correct
1 X=1234567980123456789012345678901e-10 Y=. => incorrect
2 X=1234567980123456789012345678901e-10 Y=. => incorrect

[/pre]
Ask a Question
Discussion stats
  • 4 replies
  • 199 views
  • 0 likes
  • 4 in conversation