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

Hello, I am struggling with conversion of a character price to numeric. The data looks like this:

Screen Shot 2019-04-29 at 11.24.26 AM.png

As there is dollar sign there, I did the following. But there is error, and all prices turn out to be 0. Could anyone help me?

	if Price ~=0 then Price_num = input(Price, dollar10.3);
	else Price_num = 0;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input price $;
cards;
0
$9.99
;

data want;
set have;
price_num=ifn(price ne '0',input(price,comma10.2),0);
format price_num dollar10.2;
run;

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

data have;
input price $;
cards;
0
$9.99
;

data want;
set have;
price_num=ifn(price ne '0',input(price,comma10.2),0);
format price_num dollar10.2;
run;
RebeccaJW
Calcite | Level 5
Thank you so much for your help! I appreciate it!
Tom
Super User Tom
Super User

Don't compare character strings to numbers. SAS will have to convert one or the other to make the comparison.

Don't add a decimal place value to an INFORMAT.  SAS will add an implied decimal point to integer values. So if you read $123,456 using DOLLAR10.3 informat the value will be 123.456 instead of 123456.

Make sure the width of the informat matches the width of the character variable.

 

Note that there is no difference between the DOLLAR informat and the COMMA informat. Both will ignore dollar signs and commas in the string.

Price_num = input(Price,comma32.);

Also you might need to watch out for right aligned values in the character variable.  Might not be obvious as in most places SAS will not display the leading spaces.  If in doubt print the values using $QUOTE. format and you should be able to see the leading spaces.  Ite will be an issue if the width of the informat is too short to reach the end of the actual digits in the character variable.

RebeccaJW
Calcite | Level 5
Thank you! I solve that!
ballardw
Super User

Try something like Comma12.2 instead of Dollar19,3

 

Do you actually have values with 3 decimals? US currency generally only has 2.

Tom
Super User Tom
Super User

Remember that decimal places on INFORMATs are inappropriate unless you KNOW that the strings were specifically created to support the implied decimal point that means.  It won't matter if all of the values are 0, missing, or contain an actual period character.  But if some are just digits without any period then resulting value will be divided by 10 to the power of the number of decimal places specified in the informat.

RebeccaJW
Calcite | Level 5
Thank you for explanation!
RebeccaJW
Calcite | Level 5
Thank you for pointing out it!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 12497 views
  • 1 like
  • 4 in conversation