BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kalai2008
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12

@Kalai2008 wrote:

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.


 

We're all guessing here...and you still don't have an answer.

 

So....

 

Your old character variable is:  OLD_Variable = '10202008AB_RANK00325---'

 

Post EXACTLY what you want the NEW_Variable to be.  IOW, NEW_Variable = ???

 

Once we have your OLD_Variable ("have") and NEW_Variable ("want"), we can tell you if that is possible.

 

But if you think a numeric variable can contain letters, you need to hit the docs.

 

P.S.:  Some general rules re: put/input/formats/informats:

 

The put function always returns character output, accepts either a numeric or character input, accepts either a numeric or character format, the type of format should match the type of input.

 

The input function always takes a character input, returns either a numeric or character output, accepts either a numeric or character informat, the type of informat should match the type of output.

 

Anything else will result in implicit type conversion or an error.

 

So:

 

new_var=input(old_variable, $25.);

has a character informat, so will return a character result.

 

 

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

15 REPLIES 15
PGStats
Opal | Level 21

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
Kalai2008
Pyrite | Level 9

 

 

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.

 

 

KachiM
Rhodochrosite | Level 12

(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? 

Reeza
Super User

@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. 

Patrick
Opal | Level 21

@Kalai2008 

You can read a string which only consists of digits into a numeric variable but you can't do the same for an alphanumeric string. That's a general "rule" which applies to all programming languages/databases I know.

So, as others asked, WHY do you want to convert your string into a numerical value? What do you intend to do with it that requires it to be numerical?

ABritinAus
Obsidian | Level 7

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" ?

 

Kalai2008
Pyrite | Level 9

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

ABritinAus
Obsidian | Level 7

Ah, I misunderstood what old_variable contains.

 

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

 

PGStats
Opal | Level 21

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
Barkamih
Pyrite | Level 9

Hi PG 

 

how I could make sas to put a missing value for any numeric number that has a letter like this 1836398E777?

 

regards 

andreas_lds
Jade | Level 19

@Barkamih wrote:

Hi PG 

 

how I could make sas to put a missing value for any numeric number that has a letter like this 1836398E777?

 

regards 


 

Creating a new thread would have been better, than adding something to an old topic.

 

In a data step use  

num_var = input(old_var, ??, best32.);
PGStats
Opal | Level 21

No comma after the question marks

 

new_var = input(old_variable, ?? best32.);

PG
Tom
Super User Tom
Super User

@Barkamih wrote:

Hi PG 

 

how I could make sas to put a missing value for any numeric number that has a letter like this 1836398E777?

 

regards 


You can use the ? and/or ?? modifier on the informat.

183  data _null_;
184    input ch $32. ;
185    num=input(ch,??32.);
186    put ch= $quote. num= best32. ;
187  cards;

ch="1836398E777" num=.
ch="1235678" num=1235678
ch="1235E56" num=1.235E59

Note that have a single letter E (or D) in the string will cause SAS to treat that as scientific notation.

Note also that there is no "best" informat. If you use BEST32. as the informat that is just the same as 32. (or F32.).  The BEST format makes a decision about how to format a particular number to fit in a limited number of characters. For reading a character string into a number there is no similar decision that needs to be made.

ChrisNZ
Tourmaline | Level 20

> How I could make sas to put a missing value for any numeric number that has a letter like this 1836398E777?

Like this?


data WANT;
  '10202008AB_RANK00325'n = '222';
  NUM=ifn(anyalpha('10202008AB_RANK00325'n), ., input('10202008AB_RANK00325'n, ?? 32.));
  drop '10202008AB_RANK00325'n;
  rename NUM='10202008AB_RANK00325'n;        
run;

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 15 replies
  • 11519 views
  • 5 likes
  • 11 in conversation