BookmarkSubscribeRSS Feed
GuiVtzl
Fluorite | Level 6

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

7 REPLIES 7
mohamed_zaki
Barite | Level 11

could you share

the complete log part or your code for the coversion

GuiVtzl
Fluorite | Level 6

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

mohamed_zaki
Barite | Level 11

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?

user24feb
Barite | Level 11

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;

RamKumar
Fluorite | Level 6

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;

RaviKommuri
Fluorite | Level 6

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

nravi
Calcite | Level 5

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 2411 views
  • 0 likes
  • 6 in conversation