BookmarkSubscribeRSS Feed
helios4122
Calcite | Level 5

I have a numeric variable called AMOUNT and it has a value of 48.14 after I'm coverting it into a Character Variable called AMOUNT_CHAR but when I do that, the value appears as only 48. How do I convert the full value into character including the decimal as well.

 

Here's what I'm using.

 

AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,$10.));

 

Thanks

3 REPLIES 3
MayurJadhav
Quartz | Level 8

You need to mention proper format in PUT function. 

@helios4122 

 

try this:

data test;
AMOUNT = 48.14;
AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,8.2));
run;
Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj
PaigeMiller
Diamond | Level 26

@helios4122 wrote:

I have a numeric variable called AMOUNT and it has a value of 48.14 after I'm coverting it into a Character Variable called AMOUNT_CHAR but when I do that, the value appears as only 48. How do I convert the full value into character including the decimal as well.


Don't do this. A value of 48.14 is a number, the best programming practice is to leave it as a number.

--
Paige Miller
ballardw
Super User

@helios4122 wrote:

I have a numeric variable called AMOUNT and it has a value of 48.14 after I'm coverting it into a Character Variable called AMOUNT_CHAR but when I do that, the value appears as only 48. How do I convert the full value into character including the decimal as well.

 

Here's what I'm using.

 

AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,$10.));

 

Thanks


You are running into several issues that I believe all stem from misunderstanding of Format involved syntax.

First you likely received a WARNING in the log such as is generated by the code shown below:

1    data example;
2       amount = 48.14;
3       AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,$10.));
WARNING: Variable amount has already been defined as numeric.
4    run;

That Warning is because 1) the variable Amount is numeric and 2) you use a $ format with it. So part of what happens, since you are attempting to Put with $10 is SAS does an internal conversion to allow use of the $10 format.

$W format is for character values only. As such they never support decimal places as they do not belong in character values.

So you want to use a 10. , or W.D (W is total number of characters and D is the number of decimal points to include when displaying or Putting a numeric value. If you want to include exactly 2 decimal places for every single value of your Amount variable then the format to use would end in .2, could be 10.2, 8.2 or whatever makes sense.

However if some of your values have more decimal places they will get rounded with W.2 format to 2 places.

 

There is another artifact as well. Put with numeric formats by default will right justify the result. So with a value like 48.14 and 10.2 format the result will be 10 characters long and have 5 leading spaces by default.

If you want the result to be left justified you have to use the justification option in put:

put(amount, 10.2 -L)

Syntax and details over.

 

Now, what perceived advantage do you get by creating a character value from the Amount? It won't sort properly if left justified, it needs to be converted back to numeric for any calculation.

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!

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
  • 3 replies
  • 988 views
  • 0 likes
  • 4 in conversation