Hi,
I want to convert a character A to numberic B
I use the input() function. But you can only use it if you know the length of A.
A = input(B,8.2)
But I don't know if it is 8.2 or whatever.
Do you know how I can manage it?
Use the best. format:
data test;
input b :$32.;
a = input(b,best32.);
format a best32.;
cards;
123456.78
12345678912.22
1.12345678905
3.14159265358979323846
;
run;
proc print data=test noobs;
run;
Result:
b a 123456.78 123456.78 12345678912.22 12345678912.22 1.12345678905 1.12345678905 3.14159265358979323846 3.14159265358979
Note how the Pi example shows the limits of numeric precision in SAS.
I would question why you do not know your data, seeing as that is the fundamental basis for programming with? What if it contains non-numerics, or signs/percentages, what format should the output variable have etc.
Hi,
a pragmatic approach could be to perform a reconversion check and issue warnings to your log in case of conversion failure.
Something like that:
if strip(put(a,best32.)) ne b then put 'W' 'ARNING: conversion issue please check' a= b=;
- Cheers -
Another way to verify that the char var has been converted successfully, uses the double ? modifier in the input function. Thanks to @Kurt_Bremser for provding example data 😉
data test;
input b :$32.;
a = input(b, ?? best32.);
if missing(a) and not missing(b) then put 'WARNING: Failed to convert ' b 'Check obs' _n_= ;
format a best32.;
cards;
123456.78
12345678912.22
1.12345678905
bob
3.14159265358979323846
;
run;
proc print data=test noobs;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.