<?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: creating a macro variable within a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343424#M78842</link>
    <description>&lt;P&gt;If you only have one record per COMPANY where DATE_DIFF=0 then you can just merge the file back with itself.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input company $ date_diff price @@ ;
cards;
A -2 2 A -1 5 A 0 3 A 1 1 A 2 7
;;;;

data want ;
  merge have have(rename=(date_diff=x price=base_price) where=(x=0) );
  by company ;
  price_diff = price - base_price ;
  drop x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Mar 2017 17:30:54 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-03-22T17:30:54Z</dc:date>
    <item>
      <title>creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343383#M78823</link>
      <description>&lt;P&gt;suppsoe that I have the following data&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="3" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;company&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;date diff&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;price&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;-2&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;-1&lt;/TD&gt;
&lt;TD align="right"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is to find the "date diff" = 0, and take the price of that "date diff" and subtract it from all the other prices. So here I need to subtract 3 from all the other prices and get the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 192pt;" border="0" width="256" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="4" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;company&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;date diff&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;price&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;price diff&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;-2&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;-1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;-1&lt;/TD&gt;
&lt;TD align="right"&gt;5&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;-2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;7&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course I can by inspection find the price of the date diff = 0 and then subtract it from the other prices, but I would like to have a dynamic model that can be used form many companies (A, B, C ....). That is why I imagine that somehow the price = 3 can be put into a macro variable and then subtracted from the other prices to create price diff, but I might be completely wrong, so please let me know if there is another way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:34:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343383#M78823</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-03-22T16:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343390#M78826</link>
      <description>&lt;P&gt;Do you have multiple companies or 0's that you need to find or a single one?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:34:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343390#M78826</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-22T16:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343391#M78827</link>
      <description>&lt;P&gt;many companies, each one having their own date diff = 0&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343391#M78827</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-03-22T16:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343393#M78828</link>
      <description>I would use SQL, first query to find the date different price. Then join on the original table and perform the subtract.</description>
      <pubDate>Wed, 22 Mar 2017 16:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343393#M78828</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-03-22T16:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343395#M78830</link>
      <description>&lt;P&gt;Then&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;solution is correct, a single data step to start may be easier IMO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;Calculate Diff - if 0 then output record to dataset (data step)&lt;/P&gt;
&lt;P&gt;2. Merge back with main data&lt;/P&gt;
&lt;P&gt;3. Next calculation required&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343395#M78830</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-22T16:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343398#M78831</link>
      <description>&lt;P&gt;That is actually what I did originally, but since my data is big I thought that maybe there is a simpler way, but if this is the best solution for the case I will just stick to it&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343398#M78831</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-03-22T16:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343399#M78832</link>
      <description>&lt;P&gt;The GROUP BY is not necessary for your sample data, but will account for new groups. This does depend on the full set of data being similar to what you provided.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input company $ date_diff price;
datalines;
A -2 2
A -1 5
A  0 3
A  1 1
A  2 7
;

proc sql;
    CREATE TABLE want AS
    SELECT *, 
           price - max((date_diff = 0) * price) AS price_diff
    FROM have
    GROUP BY company
    ORDER BY company, date_diff;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Mar 2017 16:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343399#M78832</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-03-22T16:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343415#M78837</link>
      <description>&lt;P&gt;If your data set is already sorted by COMPANY, a single DATA step will do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do until (last.company);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by company;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if date_diff=0 then max_price=price;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do until (last.company);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by company;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; price_diff = price - max_price;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop max_price;&amp;nbsp;&amp;nbsp; /* optional, of course */&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The top loop finds the maximum PRICE value.&amp;nbsp; The bottom loop reads the same observations, calculates, and outputs.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 17:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343415#M78837</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-22T17:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343417#M78838</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;That is actually what I did originally, but since my data is big I thought that maybe there is a simpler way, but if this is the best solution for the case I will just stick to it&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt;&amp;nbsp;In the future, please note what you've tried in your initial question and include the&amp;nbsp;code otherwise it's a waste of our time to suggest things that you already know.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 17:11:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343417#M78838</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-22T17:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343421#M78840</link>
      <description>&lt;P&gt;thanks for the suggestion&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;price &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;max&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;date_diff &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt; price&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; AS price_diff&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;becasue I thought of calculating the price_diff in several steps, but it can be done in one!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2017 17:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343421#M78840</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-03-22T17:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: creating a macro variable within a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343424#M78842</link>
      <description>&lt;P&gt;If you only have one record per COMPANY where DATE_DIFF=0 then you can just merge the file back with itself.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input company $ date_diff price @@ ;
cards;
A -2 2 A -1 5 A 0 3 A 1 1 A 2 7
;;;;

data want ;
  merge have have(rename=(date_diff=x price=base_price) where=(x=0) );
  by company ;
  price_diff = price - base_price ;
  drop x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Mar 2017 17:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-macro-variable-within-a-data-step/m-p/343424#M78842</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-03-22T17:30:54Z</dc:date>
    </item>
  </channel>
</rss>

