<?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: Using a number stored in a variable to convert another varibale into the required decimal place in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574423#M162309</link>
    <description>&lt;P&gt;Here is another somewhat clunky way that does not involve converting to character:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  data_value = 2.222;
  decimals = 2;
  data_rounded = round(data_value, 1/(10**decimals));
  put _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 18 Jul 2019 02:18:46 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-07-18T02:18:46Z</dc:date>
    <item>
      <title>Using a number stored in a variable to convert another varibale into the required decimal place</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574410#M162301</link>
      <description>&lt;P&gt;Hi I have a column called ' Decimal' which has the required decimal places stored (It is in numeric) eg&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Decimal&amp;nbsp;&amp;nbsp; Data&lt;/P&gt;&lt;P&gt;0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.222&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3.322&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I want the data to look like;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3.3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know the round function can achieve this easily but how do you use the variable 'Decimal' to&amp;nbsp;convert the Data into its actual decimal places.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tries this so far on Proc SQL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;FONT face="Courier New" size="3"&gt;round(Data,Decimal) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NewData&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;also tried&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;FONT face="Courier New" size="3"&gt;strip(put(Data,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;8.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;Decimal)) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NewData&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;It seems like I cannot use a variable to manipulate the decimal places of another variable. Any help is appreciated!&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 01:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574410#M162301</guid>
      <dc:creator>kenjichan1212</dc:creator>
      <dc:date>2019-07-18T01:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using a number stored in a variable to convert another varibale into the required decimal place</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574420#M162306</link>
      <description>&lt;P&gt;You can definitely do this, but you need to use PUTC() or PUTN() which allow the second parameter to be a defined string. So you need the length and then the decimal place and combine them into the format you want and then pass that to the PUT function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

myformat = catx(".", "8", decimalPlaces);
myValue = left(putn(value, myformat));

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;TABLE class="table" aria-label="Data Set WORK.WANT"&gt;&lt;CAPTION aria-label="Data Set WORK.WANT"&gt;&amp;nbsp;&lt;/CAPTION&gt;&lt;COLGROUP&gt;&lt;COL /&gt;&lt;/COLGROUP&gt;&lt;COLGROUP&gt;&lt;COL /&gt;&lt;COL /&gt;&lt;COL /&gt;&lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;decimalPlaces&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;value&lt;/TH&gt;
&lt;TH class="header" scope="col"&gt;myformat&lt;/TH&gt;
&lt;TH class="header" scope="col"&gt;myValue&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;2.222&lt;/TD&gt;
&lt;TD class="data"&gt;8.0&lt;/TD&gt;
&lt;TD class="data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;3.322&lt;/TD&gt;
&lt;TD class="data"&gt;8.1&lt;/TD&gt;
&lt;TD class="data"&gt;3.3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280733"&gt;@kenjichan1212&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi I have a column called ' Decimal' which has the required decimal places stored (It is in numeric) eg&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Decimal&amp;nbsp;&amp;nbsp; Data&lt;/P&gt;
&lt;P&gt;0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.222&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3.322&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I want the data to look like;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;P&gt;3.3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know the round function can achieve this easily but how do you use the variable 'Decimal' to&amp;nbsp;convert the Data into its actual decimal places.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tries this so far on Proc SQL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;FONT face="Courier New" size="3"&gt;round(Data,Decimal) &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NewData&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;also tried&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;FONT face="Courier New" size="3"&gt;strip(put(Data,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;8.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;Decimal)) &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NewData&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;It seems like I cannot use a variable to manipulate the decimal places of another variable. Any help is appreciated!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 02:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574420#M162306</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-18T02:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using a number stored in a variable to convert another varibale into the required decimal place</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574423#M162309</link>
      <description>&lt;P&gt;Here is another somewhat clunky way that does not involve converting to character:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  data_value = 2.222;
  decimals = 2;
  data_rounded = round(data_value, 1/(10**decimals));
  put _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jul 2019 02:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574423#M162309</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-07-18T02:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using a number stored in a variable to convert another varibale into the required decimal place</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574424#M162310</link>
      <description>&lt;P&gt;Format your posts as a self-contained data step using datalines statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Revisit the documentation on the round function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this give you what you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length Decimal Data 8;
   input Decimal Data;
   datalines;
0 1.123
1 2.345
2 3.456
3 4.567
;
run;

data want;
   set have;
   newdata=round(Data,10**(-Decimal));&lt;BR /&gt;   format newdata best.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Decimal	Data	newdata
0	1.123	1
1	2.345	2.3
2	3.456	3.46
3	4.567	4.567&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jul 2019 02:26:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-number-stored-in-a-variable-to-convert-another-varibale/m-p/574424#M162310</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-07-18T02:26:56Z</dc:date>
    </item>
  </channel>
</rss>

