<?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 get difference for all the numerical value in two rows? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965116#M375794</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61362"&gt;@Stu_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Great idea for making the ID a character to make the solution very elegant. Nicely done!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If the ID is numeric you can just add a second SET statement with KEEP= dataset option to reset the ID back to the original value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  array _num_ _numeric_;
  do over _num_;
    _num_=dif(_num_);
  end;
  set have(keep=id);
  if not first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Apr 2025 15:28:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-04-25T15:28:32Z</dc:date>
    <item>
      <title>How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965042#M375774</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have data like this and with almost 500 columns numerical value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need an output like this, which use the last. ID&amp;nbsp; _numeric_ MINUS first.ID _numeric_, Since there are almost 500 numerical features, I cannot subtract one by one,&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;-1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;-1&lt;/TD&gt;&lt;TD&gt;-1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 24 Apr 2025 15:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965042#M375774</guid>
      <dc:creator>Li2024</dc:creator>
      <dc:date>2025-04-24T15:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965048#M375777</link>
      <description>&lt;P&gt;Hey &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466590"&gt;@Li2024&lt;/a&gt;! PROC EXPAND can do this very quickly. It's a great swiss-army knife of multi-row operations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=a out=want(where=(time &amp;gt; 0));
   by id;
   convert _NUMERIC_ / method=none transform=(dif 1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID	TIME	A	B	C	D
1	1	    1	1	1	1
2	1	    1	3  -1	3
3	1	    2	1  -1  -1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can ignore the TIME variable and drop it later if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a DATA Step, it looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set a;
    by id;

    array numvar[*] _NUMERIC_;
    array difvar[5] _TEMPORARY_;

    do i = 2 to dim(difvar);
        difvar[i] = dif(numvar[i]);

        if(NOT first.id) then numvar[i] = difvar[i];
    end;

    if(NOT first.id) then output;

    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID	A	B	C	D
1	1	1	1	1
2	1	3  -1	3
3	2	1  -1  -1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Apr 2025 16:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965048#M375777</guid>
      <dc:creator>Stu_SAS</dc:creator>
      <dc:date>2025-04-24T16:56:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965065#M375779</link>
      <description>&lt;P&gt;The Data step code works perfect. My SAS doesn't support the proc expand.&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 24 Apr 2025 17:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965065#M375779</guid>
      <dc:creator>Li2024</dc:creator>
      <dc:date>2025-04-24T17:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965069#M375782</link>
      <description>&lt;P&gt;You don't need the extra array.&lt;/P&gt;
&lt;P&gt;And if you make ID character then it is easy to apply to all of the numeric variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ A B C D;
cards;
1 1 2 3 4
1 2 3 4 5
2 1 3 4 5
2 2 6 3 8
3 1 3 4 7
3 3 4 3 6
;

data want;
  set have;
  by id;
  array _num_ _numeric_;
  do over _num_;
    _num_=dif(_num_);
  end;
  if not first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Apr 2025 18:28:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965069#M375782</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-04-24T18:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965091#M375784</link>
      <description>&lt;P&gt;Does every ID have exactly 2 records?&amp;nbsp; Then the solution offered by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;is the way to go.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Apr 2025 00:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965091#M375784</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-04-25T00:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965096#M375786</link>
      <description>&lt;P&gt;If you have only TWO obs for each ID, I think the Tom's code is a best try. If there were not, you could try this IML code,but you also have license of SAS/IML to run IML code.&lt;/P&gt;
&lt;P&gt;P.S. My IML code also could be used to TWO obs for each ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ A B C D;
cards;
1 1 2 3 4
1 2 3 4 5
2 1 3 4 5
2 2 6 3 8
3 1 3 4 7
3 3 4 3 6
;
data first last;
 set have;
 by id;
 if first.id then output first;
 if last.id then output last;
run;

proc iml;
use first;
read all var {ID};
close;
use first(drop=id);
read all var _num_ into first[c=vname];
close;
use last(drop=id);
read all var _num_ into last;
want=last-first;
create want from ID want[c=('id'||vname)];
append from ID want;
close;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1745545871842.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106508iDA04882B9A716A96/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1745545871842.png" alt="Ksharp_0-1745545871842.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Apr 2025 01:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965096#M375786</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-04-25T01:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965108#M375791</link>
      <description>&lt;P&gt;I would say that if you can have more than two obs for a given ID, just implement a minor modification to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ A B C D;
cards;
1 1 2 3 4
1 2 3 4 5
2 1 3 4 5
2 2 6 3 8
3 1 3 4 7
3 9 9 9 9    /*extra obs for ID 3 */
3 3 4 3 6
;
data want;
 set have;
 by id;
 if first.id or last.id; /*No further processing of "middle" obs*/
 array _n _numeric_;
 do over _n;
   _n=dif(_n);
 end;
 if not (first.id);   /*Avoid instances of 1 obs per id*/
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Apr 2025 13:33:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965108#M375791</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-04-25T13:33:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965115#M375793</link>
      <description>Great idea for making the ID a character to make the solution very elegant. Nicely done!</description>
      <pubDate>Fri, 25 Apr 2025 15:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965115#M375793</guid>
      <dc:creator>Stu_SAS</dc:creator>
      <dc:date>2025-04-25T15:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965116#M375794</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/61362"&gt;@Stu_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Great idea for making the ID a character to make the solution very elegant. Nicely done!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If the ID is numeric you can just add a second SET statement with KEEP= dataset option to reset the ID back to the original value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  array _num_ _numeric_;
  do over _num_;
    _num_=dif(_num_);
  end;
  set have(keep=id);
  if not first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Apr 2025 15:28:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965116#M375794</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-04-25T15:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get difference for all the numerical value in two rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965145#M375799</link>
      <description>Sure. Conditionally execute DIF() function when first.id and last.id .</description>
      <pubDate>Sat, 26 Apr 2025 01:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-difference-for-all-the-numerical-value-in-two-rows/m-p/965145#M375799</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-04-26T01:49:57Z</dc:date>
    </item>
  </channel>
</rss>

