DATA Step, Macro, Functions and more

How to convert character(numbers/alphabets) to numeric?

Reply
Contributor
Posts: 70

How to convert character(numbers/alphabets) to numeric?

Trying to convert the Character variable to Numeric using INPUT function,but getting error  NOTE: Invalid argument to function INPUT

 

OLD_Variable :

10202008AB_RANK00325---(Proc contents : Type: Char . Len:100 Format $100. Informat $100.

 

data test;

set test1;

new_var=input(old_variable, $25.);

run;

 

Output : new_var is still in character format.

Respected Advisor
Posts: 4,936

Re: How to convert character(numbers/alphabets) to numeric?

Posted in reply to Kalai2008

If you want the decimal number at the start of your field, try:

 

new_var = input(old_variable, 8.);

 

If the number is hexadecimal:

 

new_var = input(old_variable, hex10.);

 

or, if you want the number at the end:

 

new_var = input (substr(old_variable, 16), 5.);

PG
Contributor
Posts: 70

Re: How to convert character(numbers/alphabets) to numeric?

 

 

Thank you for the reply.

 

Let me make it clear,

 

I want the OLD_Variable which is in character format with letters/numbers (10202008AB_RANK00325)

to be converted into new variable as Numeric without eliminating/truncating any letter or numbers.

I want exactly the same as Numeric Variable.

 

 

Super Contributor
Posts: 298

Re: How to convert character(numbers/alphabets) to numeric?

[ Edited ]
Posted in reply to Kalai2008

(10202008AB_RANK00325)

 

You want a numeric from the above. I only know that a NUMERIC contains only digits from 0 to 9.  Can you show how  you want that numeric should look like? 

Super User
Posts: 19,878

Re: How to convert character(numbers/alphabets) to numeric?

Posted in reply to Kalai2008

Kalai2008 wrote:

 

 

Thank you for the reply.

 

Let me make it clear,

 

I want the OLD_Variable which is in character format with letters/numbers (10202008AB_RANK00325)

to be converted into new variable as Numeric without eliminating/truncating any letter or numbers.

I want exactly the same as Numeric Variable.

 

 


This isn't possible, a numeric variable does not have letters in it

What are you trying to do that you feel you need a numeric variable? Perhaps there's another way to achieve this. 

Contributor
Posts: 35

Re: How to convert character(numbers/alphabets) to numeric?

Posted in reply to Kalai2008

Hi Kalai2008

 

If you want a numeric variable you should remove the $ from the input function  ie you just need new_var=input(old_variable, 25.);

 

However this will likely result in a numeric variable with missing values since it looks like OLD_VARIABLE contains characters/letters as well as numbers.

 

You won't be able to convert OLD_VARIABLE to numeric unless you remove the characters.

 

What numeric value do you hope to see from

"10202008AB_RANK00325" ?

 

Contributor
Posts: 70

Re: How to convert character(numbers/alphabets) to numeric?

Posted in reply to ABritinAus

Thank you for the reply. I don't want to remove any character from the variable. Need exact  old_variable to numeric.

Contributor
Posts: 35

Re: How to convert character(numbers/alphabets) to numeric?

Posted in reply to Kalai2008

Ah, I misunderstood what old_variable contains.

 

If old_variable doesn't contain any characters, then PG's answer should be what you need.

 

Respected Advisor
Posts: 4,936

Re: How to convert character(numbers/alphabets) to numeric?

Posted in reply to Kalai2008

If old_variable contains a valid number and you want to convert the whole field, use:

 

new_var = input(old_variable, best32.);

 

The first 32 non blank characters will be converted to a number.

PG
Ask a Question
Discussion stats
  • 8 replies
  • 148 views
  • 2 likes
  • 5 in conversation