<?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: Calculating a new column using macro based on information contained in existing column in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471031#M70915</link>
    <description>&lt;P&gt;I bet you a nice pint of beer that you don't need any macro language (at least no macro code) to solve your issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post some example data in a data step (see my footnotes), and a sample of the expected output from that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now your code looks like an honorable mention in the Obfuscated SAS Code Contest.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jun 2018 11:01:26 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-06-18T11:01:26Z</dc:date>
    <item>
      <title>Calculating a new column using macro based on information contained in existing column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471027#M70914</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I have a macro %calcCost(Column1,column6) which uses the information contained in column1 and column6 to perform some calculation and it then assign a value to a refence called Calcost. The calculation it performs is dependent on the information contained in Column1 and column6.&lt;/P&gt;&lt;P&gt;I want to apply this macro to each row of my data to populate a new column, called column99.&lt;BR /&gt;The code need to do the following:&lt;BR /&gt;start with row 1&lt;BR /&gt;1. run macro %cost_byrow which will call macro %calcCost(info in Column1 of row1, info in column6 of row1). e.g. %calcCost(10%,&amp;nbsp; &amp;nbsp;190)&lt;BR /&gt;2. macro generates a value stored in reference &amp;amp;CalcCost&lt;BR /&gt;3. populate column99 with value from &amp;amp;CalcCost&lt;BR /&gt;repeat for next row&lt;BR /&gt;until end of data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I'm using.&amp;nbsp; the issue is the the code in red is producing a table with just one record. How can I obtain add a column, column99, to my data "Tiering.Data" which contains the calculated CalcCost for each observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;%macro AMC;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;%do ID=1 %to &amp;amp;nobs;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;%cost_byrow;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;data Tiering.AMC;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;set Tiering.Data;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %if _n_ = &amp;amp;ID. %then;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;column99 = &amp;amp;CalcCost;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;%mend AMC;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%macro cost_byrow;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%global chargerate Amount;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;data _null_;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;set Tiering.Data(where=(rownum=_n_));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;call symputx('chargerate',Column1);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;call symputx('Amount',Column6);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%calcCost(&amp;amp;chargerate.,Amount.);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%mend cost_byrow;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Krish&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jun 2018 10:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471027#M70914</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2018-06-18T10:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a new column using macro based on information contained in existing column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471031#M70915</link>
      <description>&lt;P&gt;I bet you a nice pint of beer that you don't need any macro language (at least no macro code) to solve your issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post some example data in a data step (see my footnotes), and a sample of the expected output from that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now your code looks like an honorable mention in the Obfuscated SAS Code Contest.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jun 2018 11:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471031#M70915</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-18T11:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a new column using macro based on information contained in existing column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471039#M70916</link>
      <description>&lt;P&gt;There is several things wrong here.&amp;nbsp; Firstly you don't need macro code for this, and using it is the reason you are having problems.&amp;nbsp; Use Base SAS which is the programming language.&amp;nbsp; Second off, you are mixing macro and datastep.&amp;nbsp; You need to understand that Macro is not the executable code, it just generates some text which then goes into the compiler - hence why you should use it sparingly and only for specific circumstances, not for every bit of code you do.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%if _n_ = &amp;amp;ID. %then;&lt;/PRE&gt;
&lt;P&gt;_n_ is a&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;datastep&lt;/STRONG&gt;&lt;/U&gt; automatic variable, it is not available to the macro processor.&amp;nbsp; This will not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;column99 = &amp;amp;CalcCost;&lt;/PRE&gt;
&lt;P&gt;Calcost is not defined in the code you provide nor is &amp;amp;nobs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This:&lt;/P&gt;
&lt;PRE&gt;%do ID=1 %to &amp;amp;nobs;&lt;/PRE&gt;
&lt;P&gt;There is no associated %end stament with this.&lt;/P&gt;
&lt;P&gt;This:&lt;/P&gt;
&lt;PRE&gt;%global chargerate Amount;&lt;/PRE&gt;
&lt;P&gt;Its really a bad idea to create global macro variables from within a macro, you could alter something at higher levels.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end of the day, this code could be a simple datastep - provide test data in the form of a datastep and we can provide working code.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jun 2018 12:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-a-new-column-using-macro-based-on-information/m-p/471039#M70916</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-18T12:10:38Z</dc:date>
    </item>
  </channel>
</rss>

