BookmarkSubscribeRSS Feed
Leonoids
Calcite | Level 5

Hello,

 

I am trying to convert an amount field from character to numeric using input function but unable to do.

Getting error Invalid argument to input function.

 

I have tried commaw.d , bestw.d informats as well but its not working.

amount=input(amount_old,20.2);

example: data in amount_old field

23.45

12,300.56

123,123.23

 

Thanks in advance.

7 REPLIES 7
s_lassen
Meteorite | Level 14

Not sure what you have been doing, but this seems to work fine:

data have;
  length amount_old $20;
  input amount_old;
  amount=input(amount_old,comma20.2);
cards;
23.45
12,300.56
123,123.23
;run;
ChrisNZ
Tourmaline | Level 20

The dollar. informat is the most flexible one. It handles commas, dollar signs and plain numbers.

Leonoids
Calcite | Level 5

Thank you.

I can able to convert from char to numeric while creating dataset using datalines. 

I have pipe delimited file in which trying to process the data but its not happening.

 

data one;

infile "filename" delimiter='|' missover dsd lrecl=2000;

informat amount comma26.2;

length amount 8;

input amount;

run;

 

error: Invalid data for amount

Leonoids
Calcite | Level 5

for the above, if I am keeping amount column as character I can able to create the dataset without any errors. once the dataset creates, trying to convert amount field from char to numeric, getting error invalid argument to input function.

Tom
Super User Tom
Super User

@ChrisNZ wrote:

The dollar. informat is the most flexible one. It handles commas, dollar signs and plain numbers.


The DOLLAR informat is just an alias for the COMMA informat.

Tom
Super User Tom
Super User

Did you really intend to divide the integer values by 100?  If not then do NOT include a decimal part in the INFORMAT.  The purpose of the decimal part of an informat is to tell SAS that the decimal point is explicitly NOT present in the data to save one character in representing the number as text.  The maximum width that the COMMA informat supports is 32 and the INPUT() function does not mind if the width you use on the informat is larger than the length of the string you are reading.

amount=input(amount_old,comma32.);

 

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1072 views
  • 0 likes
  • 4 in conversation