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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.