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.
@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.
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.);
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.
(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?
@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.
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?
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" ?
Thank you for the reply. I don't want to remove any character from the variable. Need exact old_variable to numeric.
Ah, I misunderstood what old_variable contains.
If old_variable doesn't contain any characters, then PG's answer should be what you need.
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.
Hi PG
how I could make sas to put a missing value for any numeric number that has a letter like this 1836398E777?
regards
@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.);
No comma after the question marks
new_var = input(old_variable, ?? best32.);
@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.
> 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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.