BookmarkSubscribeRSS Feed
FP12
Obsidian | Level 7

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?

4 REPLIES 4
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Oligolas
Barite | Level 11

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 -

andreas_lds
Jade | Level 19

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;
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1366 views
  • 0 likes
  • 5 in conversation