DATA Step, Macro, Functions and more

Problem to convert character to numeric

Reply
Contributor
Posts: 40

Problem to convert character to numeric

Hello everyone,

I am facing some trouble when I want to convert a character variable to a numeric one.

At start, my variable "A" is character such like :

A

---------

63,4 M€

14 866 M€

I used the compress function to have my have my variable appearing as following :

A

---------

119

63.4

14866

and so on...

Then I tried to convert it, I tried both input function and the "not rigourous" technique of multiply it by 1.

Each time I got the same error message saying :

Invalid numeric data, A='119.' , at line 16 column 6.

and my new variable B (supposedly numeric) has only missing values...

Could you help me try to figure this out please ?

Thanks a lot

Super Contributor
Posts: 490

Re: Problem to convert character to numeric

could you share

the complete log part or your code for the coversion

Contributor
Posts: 40

Re: Problem to convert character to numeric

Thank you mohamed

Here is my code (with the *1 technique) :

data test ; set figures ;

CA = A*1;

run;

In fact, in the log, it seems sas sees the data as e.g. "119." because it reads it like that (in the log) : 119€

But the € doesn't appear in my table ! I even ran a compress function before to suppress the € !

I have a way to do what I want (I substr a string as (length(myvariable)-1) and then *1) but it is not very good.

So if you have a solution to handle that invisible € sign, it would be great !

Thanks

Super Contributor
Posts: 490

Re: Problem to convert character to numeric

It look like your data set examples have problem,

A

---------

63,4 M€

14 866 M€

does it contain spaces and the currency spaced from the value.

Does your data look like this? Or please give well typed sample of your dataset?

Super Contributor
Posts: 336

Re: Problem to convert character to numeric

Might work (you could check for better informats):

Data A;

  Input X $20.;

  X_Num=Input(Compress(Substr(X,1,Length(X)-2)),Numx20.);

  Datalines;

63,4 M€

14 866 M€

;

Run;

Regular Contributor
Posts: 168

Re: Problem to convert character to numeric

Try similar kind of stuff in your code:

data B;

input x2 $ ;

datalines;

  36.2

  4.2

  14899

;

run;

data c;

set B;

x3=input(x2,best8.);

run;

proc contents data=c;

run;

Contributor
Posts: 45

Re: Problem to convert character to numeric

data have;                       

Input val $10.;                  

datalines;                       

119 M                           

634 M                          

14866 M                         

;                                

DATA WANT;                       

SET HAVE;                       

REMOVE_CHAR = COMPRESS(VAL,'MA');

NUMONLY = INPUT(REMOVE_CHAR,5.);

I have used same COMPRESS and INPUT functions, but I didn't get any error

Occasional Contributor
Posts: 5

Re: Problem to convert character to numeric

Try this..

data a;

input x $20.;

datalines;

63,4 M€

14 866 M€

;

run;

data b;

set a;

y=input(compress(x,'123456789','k'),8.);

run;

Ask a Question
Discussion stats
  • 7 replies
  • 378 views
  • 0 likes
  • 6 in conversation