<?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 will you calculate row differences within pairs for a set of variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874652#M345593</link>
    <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;, &lt;BR /&gt;What an efficient way to solve the problem!&lt;BR /&gt;Does this always return a positive value? Or is there a way(optional arguments) to control the result, like difference of first.variable from the last or vice versa...&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 May 2023 13:12:36 GMT</pubDate>
    <dc:creator>A_Kh</dc:creator>
    <dc:date>2023-05-09T13:12:36Z</dc:date>
    <item>
      <title>How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874465#M345500</link>
      <description>&lt;P&gt;This is the dataset&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data dsin;
input ID$ pair$ type$ time_point_3	time_point_6	time_point_9	time_point_12	time_point_18;
datalines;
1	1	a	111	134	.	.	.
2	1	b	110	.	123	.	.
3	2	a	.	131	.	102	120
4	2	b	.	.	.	121	123
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How will you calculate row differences within the same pair for each time_point? We want to get the following results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data dsout;
input ID$ pair$ type$ time_point_3	time_point_6	time_point_9	time_point_12	time_point_18 Diff_3	Diff_6	Diff_9	Diff_12	Diff_18;
datalines;
1	1	a	111	134	.	.	.	1		.	.	.
2	1	b	110	.	123	.	.	1		.	.	.
3	2	a	.	131	.	102	120	.	.	.	19	3
4	2	b	.	.	.	121	123	.	.	.	19	3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 May 2023 15:12:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874465#M345500</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-05-08T15:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874473#M345507</link>
      <description>&lt;P&gt;While you can do this with arrays, this type of data is easier to work with if you transpose to a LONG format.&amp;nbsp; I would consider something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=dsin out=vert (rename=(_name_=timepoint col1=value));
  var time_point_3	time_point_6	time_point_9	time_point_12	time_point_18 ;
  by id pair type ;
run ;

proc sort data=vert ;
  by pair timepoint type ;
run ;

data want ;
  set vert ;
  by pair timepoint ;
  Diff=dif(value) ;
  if first.timepoint then call missing(diff) ;
run ;

proc print data=want ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that returns -1 for the first difference, not 1.&amp;nbsp; You might be intending absolute value of the difference?&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 15:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874473#M345507</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-05-08T15:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874476#M345508</link>
      <description>&lt;P&gt;If you are positive you always have exactly two observations per PAIR then you can use use DIF() function.&amp;nbsp; Use double DOW loop so only the second DIF() is kept and merged back onto the first observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.pair);
    set dsin;
    by pair;
    diff_3=dif(tp_3);
    diff_6=dif(tp_6);
    diff_9=dif(tp_9);
    diff_12=dif(tp_12);
    diff_18=dif(tp_18);
  end;
  do until(last.pair);
    set dsin;
    by pair;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1683560957261.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83697iA983C171D861FA21/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1683560957261.png" alt="Tom_0-1683560957261.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 15:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874476#M345508</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-08T15:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874479#M345510</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set dsin;
	array old time_point_3	time_point_6	time_point_9	time_point_12	time_point_18;
	array new tp3 tp6 tp9 tp12 tp18;
	array diff diff_3 diff_6 diff_9 diff_12 diff_18;
	do over old;
		new= lag(old);
		if old ne . and new ne . then diff=old-new;
	end;
	drop tp:;
run;

proc sort data=want;by pair descending type;run; 

data want1;
	set want;
	by pair descending type;
	array temp [5];
	array diff [5] diff_3--diff_18; 
	do i= 1 to 5;
	retain temp;
		if first.pair then temp{i}=diff{i};
		if diff{i} eq . then diff{i}=temp{i};
	end;
	drop temp: i; 
proc print;run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 May 2023 15:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874479#M345510</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-05-08T15:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874488#M345518</link>
      <description>&lt;P&gt;Thank you! Yes, only two observations per pair.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 16:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874488#M345518</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-05-08T16:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874489#M345519</link>
      <description>Thank you!</description>
      <pubDate>Mon, 08 May 2023 16:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874489#M345519</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-05-08T16:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874650#M345592</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsin;
input ID$ pair$ type$ time_point_3	time_point_6	time_point_9	time_point_12	time_point_18;
datalines;
1	1	a	111	134	.	.	.
2	1	b	110	.	123	.	.
3	2	a	.	131	.	102	120
4	2	b	.	.	.	121	123
;
run;
proc sql;
select *,
range(time_point_3) as diff_3,
range(time_point_6) as diff_6,
range(time_point_9) as diff_9,
range(time_point_12) as diff_12,
range(time_point_18) as diff_18
 from dsin 
  group by pair;
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-1683636234930.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83739iBBB060851373FF9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1683636234930.png" alt="Ksharp_0-1683636234930.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2023 12:44:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874650#M345592</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-09T12:44:01Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874652#M345593</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;, &lt;BR /&gt;What an efficient way to solve the problem!&lt;BR /&gt;Does this always return a positive value? Or is there a way(optional arguments) to control the result, like difference of first.variable from the last or vice versa...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2023 13:12:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874652#M345593</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-05-09T13:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: How will you calculate row differences within pairs for a set of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874903#M345696</link>
      <description>"Does this always return a positive value?"&lt;BR /&gt;Yes . RANGE() always return positive value ,since it = max-min .&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;"Or is there a way(optional arguments) to control the result, like difference of first.variable from the last or vice versa... "&lt;BR /&gt;No, there is no other option, but I think you could use the skill posed by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; via LAG() function.</description>
      <pubDate>Wed, 10 May 2023 12:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-will-you-calculate-row-differences-within-pairs-for-a-set-of/m-p/874903#M345696</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-10T12:05:43Z</dc:date>
    </item>
  </channel>
</rss>

