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;

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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