BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10

hello,

 

in a dataset , there are 2 records and a column named "amount"  (character), and values are

rec         amount

1           82.04

2          108498.19

 

And I would like to convert these values into numeric.

I try input(amount,5.) that returns 82.04 for rec1 (correct) but 10849 for rec 2(not correct because truncated)

I try input(amount,9.) that returns 108498.19 for rec 2 (correct) but "." for rec 1 (not correct)

 

please, what is the correct syntax and the correct informat ?

thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you reading from a text file? Make sure to use the TRUNCOVER option on the INFILE statement.  Or add the : modifier in front of the format in the INPUT statement.

Are you reading from a character variable using the INPUT() function? Is so then your issue is probably caused by non-printing characters on the end of the shorter string.  Like at TAB ('09'x) or a CR ('0D'x) or perhaps a NUL ('00'x) or what Microsoft likes to call a non-breaking space ('A0'x).  Try printing the value using $HEX format to see what characters are there and then use the COMPRESS() function to remove them.

If you want the value to print with commas as the thousand separators then use the COMMA format. If you want to change decimal point to a comma and use period as thousands separator then use COMMAX or change your locale settings.

 

data want ;
 set have ;
  amount_num = input(amount,32.);
  format amount_num comma14.2;
run;

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@PeterClemmensen wrote:
data have;
length amount $10;
input rec amount$;
datalines;
1 82.04
2 108498.19
;

data want;
	set have;
	amount_num = input(amount,comma10.2);
run;

When I execute this code and then do a proc print, I don't see the commas in amount_num.

--
Paige Miller
PaigeMiller
Diamond | Level 26

@PeterClemmensen wrote:

Strange, I do? 🙂

 

Udklip.PNG


Maybe there's something unusual about my computer and commas are written in invisible ink, but I do not see commas, not in my web browser looking at your reply, and not in my SAS 9.4 when I do a PROC PRINT.

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

Ah, I mixed them up with periods, my bad 🙂

BrunoMueller
SAS Super FREQ

Your INPUT function with informat should just work fine, see example below:

 

data want;
  length amount $ 15;

  do amount = "82.04", "108498.19";
    amount_n = input(amount, 15.);
    output;
  end;
run;
Tom
Super User Tom
Super User

Are you reading from a text file? Make sure to use the TRUNCOVER option on the INFILE statement.  Or add the : modifier in front of the format in the INPUT statement.

Are you reading from a character variable using the INPUT() function? Is so then your issue is probably caused by non-printing characters on the end of the shorter string.  Like at TAB ('09'x) or a CR ('0D'x) or perhaps a NUL ('00'x) or what Microsoft likes to call a non-breaking space ('A0'x).  Try printing the value using $HEX format to see what characters are there and then use the COMPRESS() function to remove them.

If you want the value to print with commas as the thousand separators then use the COMMA format. If you want to change decimal point to a comma and use period as thousands separator then use COMMAX or change your locale settings.

 

data want ;
 set have ;
  amount_num = input(amount,32.);
  format amount_num comma14.2;
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
  • 8 replies
  • 11782 views
  • 1 like
  • 5 in conversation