<?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 match variables then compute in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266114#M52409</link>
    <description>&lt;P&gt;It may also help to ensure that your date is a SAS date valued variable. That will help sorting and lots of other things if they are not currently date valued.&lt;/P&gt;</description>
    <pubDate>Mon, 25 Apr 2016 17:07:49 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-04-25T17:07:49Z</dc:date>
    <item>
      <title>How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/265992#M52373</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been trying to find answers to the subject title but still unable to find any. Hope that you guys can help me with this:&lt;/P&gt;&lt;P&gt;My datasets have date, ID and amount, for eg.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date &amp;nbsp; &amp;nbsp; ID &amp;nbsp; &amp;nbsp; Amount&lt;/P&gt;&lt;P&gt;1 Jan &amp;nbsp; AA &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 Feb &amp;nbsp; AA &amp;nbsp; &amp;nbsp; &amp;nbsp;500&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 Mar &amp;nbsp; AA &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1 Jan &amp;nbsp; BB &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;&lt;P&gt;1 Jan &amp;nbsp; CC &amp;nbsp; &amp;nbsp; 1000&lt;/P&gt;&lt;P&gt;1 Feb &amp;nbsp; CC &amp;nbsp; &amp;nbsp; &amp;nbsp;500&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to achieve is to find the difference for each month, based on the ID. However, I do not know how to match the same ID then do the computation, meaning if first ID = second ID then difference = second amount - first amount.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output maybe something like:&lt;/P&gt;&lt;P&gt;Date &amp;nbsp; &amp;nbsp; ID &amp;nbsp; &amp;nbsp; Amount &amp;nbsp; &amp;nbsp;Difference from previous month&lt;/P&gt;&lt;P&gt;1 Jan &amp;nbsp; AA &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;&lt;P&gt;1 Feb &amp;nbsp; AA &amp;nbsp; &amp;nbsp; &amp;nbsp;500 &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;500&lt;/P&gt;&lt;P&gt;1 Mar &amp;nbsp; AA &amp;nbsp; &amp;nbsp; &amp;nbsp;100 &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;400&lt;/P&gt;&lt;P&gt;1 Jan &amp;nbsp; BB &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;&lt;P&gt;1 Jan &amp;nbsp; CC &amp;nbsp; &amp;nbsp; 1000&lt;/P&gt;&lt;P&gt;1 Feb &amp;nbsp; CC &amp;nbsp; &amp;nbsp; &amp;nbsp;500 &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;500&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Really sorry for the poor formatting. And, I do not know how to use Macro and SQL. Hence, if it is possible to do without them, will really appreciate it. However, if either Macro or SQL is required, I will read up on the codes required.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope to get some advice/solution from you guys. Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 10:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/265992#M52373</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-04-25T10:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/265998#M52374</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure that your dataset is sorted by ID (with a prior proc sort for instance).&lt;/P&gt;
&lt;P&gt;Then you can use the "by id;" instruction in your data step.&lt;/P&gt;
&lt;P&gt;This creates an automatic variable first.ID wich is 1 for the first observation of&lt;/P&gt;
&lt;P&gt;each ID, zero otherwise.&lt;/P&gt;
&lt;P&gt;The instruction "retain X Y;" allows You to keep the values of variables X and Y&lt;/P&gt;
&lt;P&gt;from one observation to the other. So you can use retain to keep track of&lt;/P&gt;
&lt;P&gt;the initial amount and the intermediary values of the calculated difference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 10:21:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/265998#M52374</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-04-25T10:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/265999#M52375</link>
      <description>&lt;P&gt;You need to use BY group processing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming your data is sorted by ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read up on BY groups, they're very powerful.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
Set have;

BY ID;

Diff = dif(amount);

If first.ID then diff = .;

Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Apr 2016 10:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/265999#M52375</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-25T10:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266020#M52378</link>
      <description>&lt;P&gt;Reeza has the right idea.&amp;nbsp; However, based on your example, you would need a slight change to the formula:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;diff = - dif(amount);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 12:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266020#M52378</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-04-25T12:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266114#M52409</link>
      <description>&lt;P&gt;It may also help to ensure that your date is a SAS date valued variable. That will help sorting and lots of other things if they are not currently date valued.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 17:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266114#M52409</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-25T17:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266261#M52455</link>
      <description>hey, thank you!</description>
      <pubDate>Tue, 26 Apr 2016 02:41:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266261#M52455</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-04-26T02:41:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266262#M52456</link>
      <description>got it! thank you!</description>
      <pubDate>Tue, 26 Apr 2016 02:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266262#M52456</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-04-26T02:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266263#M52457</link>
      <description>okay! will try it out and see the results and adjust accordingly. thank you!</description>
      <pubDate>Tue, 26 Apr 2016 02:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266263#M52457</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-04-26T02:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to match variables then compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266264#M52458</link>
      <description>noted, will do! thank you!</description>
      <pubDate>Tue, 26 Apr 2016 02:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-variables-then-compute/m-p/266264#M52458</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-04-26T02:43:03Z</dc:date>
    </item>
  </channel>
</rss>

