<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to convert the decimal into character value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876677#M346331</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/442927"&gt;@helios4122&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's what I'm using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,$10.));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are running into several issues that I believe all stem from misunderstanding of Format involved syntax.&lt;/P&gt;
&lt;P&gt;First you likely received a WARNING in the log such as is generated by the code shown below:&lt;/P&gt;
&lt;PRE&gt;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;
&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;$W format is for character values only. As such they never support decimal places as they do not belong in character values.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;However if some of your values have more decimal places they will get rounded with W.2 format to 2 places.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;If you want the result to be left justified you have to use the justification option in put:&lt;/P&gt;
&lt;PRE&gt;put(amount, 10.2 -L)&lt;/PRE&gt;
&lt;P&gt;Syntax and details over.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Fri, 19 May 2023 14:52:12 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-05-19T14:52:12Z</dc:date>
    <item>
      <title>How to convert the decimal into character value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876623#M346317</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what I'm using.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,$10.));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 08:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876623#M346317</guid>
      <dc:creator>helios4122</dc:creator>
      <dc:date>2023-05-19T08:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert the decimal into character value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876629#M346322</link>
      <description>&lt;P&gt;You need to mention proper format in PUT function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/442927"&gt;@helios4122&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
AMOUNT = 48.14;
AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,8.2));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 May 2023 09:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876629#M346322</guid>
      <dc:creator>MayurJadhav</dc:creator>
      <dc:date>2023-05-19T09:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert the decimal into character value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876630#M346323</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/442927"&gt;@helios4122&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't do this. A value of 48.14 is a number, the best programming practice is to leave it as a number.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 09:51:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876630#M346323</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-19T09:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert the decimal into character value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876677#M346331</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/442927"&gt;@helios4122&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's what I'm using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AMOUNT_CHAR=COMPRESS(PUT(AMOUNT,$10.));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are running into several issues that I believe all stem from misunderstanding of Format involved syntax.&lt;/P&gt;
&lt;P&gt;First you likely received a WARNING in the log such as is generated by the code shown below:&lt;/P&gt;
&lt;PRE&gt;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;
&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;$W format is for character values only. As such they never support decimal places as they do not belong in character values.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;However if some of your values have more decimal places they will get rounded with W.2 format to 2 places.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;If you want the result to be left justified you have to use the justification option in put:&lt;/P&gt;
&lt;PRE&gt;put(amount, 10.2 -L)&lt;/PRE&gt;
&lt;P&gt;Syntax and details over.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 14:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-the-decimal-into-character-value/m-p/876677#M346331</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-19T14:52:12Z</dc:date>
    </item>
  </channel>
</rss>

