<?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: Hi All, Subtracting Values (1stObs-2nd obs ) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36965#M7285</link>
    <description>You can also do it as follows:&lt;BR /&gt;
&lt;BR /&gt;
Step 1:&lt;BR /&gt;
&lt;BR /&gt;
Index the obsevations with a new variable say IDX. The o/p will look like &lt;BR /&gt;
account_no trans_amt idx&lt;BR /&gt;
100 1000 1&lt;BR /&gt;
100 500 2&lt;BR /&gt;
100 2000 3&lt;BR /&gt;
200 3000 1&lt;BR /&gt;
200 2000 2&lt;BR /&gt;
&lt;BR /&gt;
Use first.account_no to initialize the idx.&lt;BR /&gt;
&lt;BR /&gt;
Step 2:&lt;BR /&gt;
&lt;BR /&gt;
Sort the data on idx desc. The o/p will be something like:&lt;BR /&gt;
100 2000 3&lt;BR /&gt;
100 500 2&lt;BR /&gt;
100 1000 1&lt;BR /&gt;
200 2000 2&lt;BR /&gt;
200 3000 1&lt;BR /&gt;
&lt;BR /&gt;
Step 3:&lt;BR /&gt;
Now using RETAIN do the subtraction. o/p would be like:&lt;BR /&gt;
account_no transaction_amt amount idx&lt;BR /&gt;
100 2000 0 3&lt;BR /&gt;
100 500 -1500 2&lt;BR /&gt;
100 1000 500 1&lt;BR /&gt;
200 2000 0 2&lt;BR /&gt;
200 3000 1000 1&lt;BR /&gt;
&lt;BR /&gt;
Step 4:&lt;BR /&gt;
Sort on idx ascending and get rid of it. o/p should look the way you want it to.</description>
    <pubDate>Thu, 07 Aug 2008 09:03:56 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-08-07T09:03:56Z</dc:date>
    <item>
      <title>Hi All, Subtracting Values (1stObs-2nd obs )</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36963#M7283</link>
      <description>I have a dataset similar to the below in which i have a column which contains account number and transaction amount. And i have situation in which i have to minus trans_amt(i.e 1stobs-2ndobs) by account number.&lt;BR /&gt;
&lt;BR /&gt;
Ex:&lt;BR /&gt;
data a;&lt;BR /&gt;
input account_no trans_amt;&lt;BR /&gt;
datalines;&lt;BR /&gt;
100 1000&lt;BR /&gt;
100 500&lt;BR /&gt;
100 2000&lt;BR /&gt;
200 3000&lt;BR /&gt;
200 2000&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
Output should like this :&lt;BR /&gt;
&lt;BR /&gt;
account_no trans_amt amount&lt;BR /&gt;
100            1000         500&lt;BR /&gt;
100             500         -1500&lt;BR /&gt;
100            2000         0&lt;BR /&gt;
200            3000         1000&lt;BR /&gt;
200            2000         0</description>
      <pubDate>Thu, 07 Aug 2008 04:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36963#M7283</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-07T04:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: Hi All, Subtracting Values (1stObs-2nd obs )</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36964#M7284</link>
      <description>If you make a copy of the dataset, deleting the first observation for each account, and then merging the copy back with the original dataset, you will have the current value and the next one on the same row of data, allowing you to substract.&lt;BR /&gt;
For example :&lt;BR /&gt;
[pre]&lt;BR /&gt;
data a;&lt;BR /&gt;
input account_no trans_amt;&lt;BR /&gt;
datalines;&lt;BR /&gt;
100 1000&lt;BR /&gt;
100 500&lt;BR /&gt;
100 2000&lt;BR /&gt;
200 3000&lt;BR /&gt;
200 2000&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data a2 ;&lt;BR /&gt;
	set a (rename=(trans_amt = next_amt)) ;&lt;BR /&gt;
	by account_no ;&lt;BR /&gt;
	if first.account_no then delete ;&lt;BR /&gt;
run ;&lt;BR /&gt;
data result (drop=next_amt) ;&lt;BR /&gt;
	merge a a2 ;&lt;BR /&gt;
	by account_no ;&lt;BR /&gt;
	amount = trans_amt-next_amt ;&lt;BR /&gt;
run ;&lt;BR /&gt;
proc print ;&lt;BR /&gt;
run ;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Regards,&lt;BR /&gt;
Olivier</description>
      <pubDate>Thu, 07 Aug 2008 07:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36964#M7284</guid>
      <dc:creator>Olivier</dc:creator>
      <dc:date>2008-08-07T07:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: Hi All, Subtracting Values (1stObs-2nd obs )</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36965#M7285</link>
      <description>You can also do it as follows:&lt;BR /&gt;
&lt;BR /&gt;
Step 1:&lt;BR /&gt;
&lt;BR /&gt;
Index the obsevations with a new variable say IDX. The o/p will look like &lt;BR /&gt;
account_no trans_amt idx&lt;BR /&gt;
100 1000 1&lt;BR /&gt;
100 500 2&lt;BR /&gt;
100 2000 3&lt;BR /&gt;
200 3000 1&lt;BR /&gt;
200 2000 2&lt;BR /&gt;
&lt;BR /&gt;
Use first.account_no to initialize the idx.&lt;BR /&gt;
&lt;BR /&gt;
Step 2:&lt;BR /&gt;
&lt;BR /&gt;
Sort the data on idx desc. The o/p will be something like:&lt;BR /&gt;
100 2000 3&lt;BR /&gt;
100 500 2&lt;BR /&gt;
100 1000 1&lt;BR /&gt;
200 2000 2&lt;BR /&gt;
200 3000 1&lt;BR /&gt;
&lt;BR /&gt;
Step 3:&lt;BR /&gt;
Now using RETAIN do the subtraction. o/p would be like:&lt;BR /&gt;
account_no transaction_amt amount idx&lt;BR /&gt;
100 2000 0 3&lt;BR /&gt;
100 500 -1500 2&lt;BR /&gt;
100 1000 500 1&lt;BR /&gt;
200 2000 0 2&lt;BR /&gt;
200 3000 1000 1&lt;BR /&gt;
&lt;BR /&gt;
Step 4:&lt;BR /&gt;
Sort on idx ascending and get rid of it. o/p should look the way you want it to.</description>
      <pubDate>Thu, 07 Aug 2008 09:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hi-All-Subtracting-Values-1stObs-2nd-obs/m-p/36965#M7285</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-07T09:03:56Z</dc:date>
    </item>
  </channel>
</rss>

