<?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: function dif(x) and last.observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383806#M91573</link>
    <description>&lt;P&gt;Given that (a) &amp;nbsp;you need to perform additional calculations, and (b) you need all the results on a single observation, I think you need to switch gears:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by subject;&lt;/P&gt;
&lt;P&gt;retain x1 x2 y1 y2 z1 z2;&lt;/P&gt;
&lt;P&gt;if first.subject then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;x1 = x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;y1 = y;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;z1 = 2;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else if last.subject=0 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;x2 = x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;y2 = y;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;z2 = z;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if last.subject;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*** Now you have all 9 values on a single observation. &amp;nbsp;Perform the final calculations in whatever way you would prefer;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jul 2017 16:45:36 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-07-28T16:45:36Z</dc:date>
    <item>
      <title>function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383681#M91539</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help with the following issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have dataset with variables x,y,z for subjects 1, 2, 3, etc.&lt;/P&gt;&lt;P&gt;We need to add difference - dif(x) between last.observation and 'observation before last'. When simple dif(x) is working it is impossible to apply it for 'last.observation' and 'observation before last'. Does a way exist to sort it out?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the code below and output.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data multiple;
	infile datalines;
	input subject 1-2 X 4 Y 6 Z 8;
datalines;
01 1 2 3
01 4 5 6
01 7 8 9
01 8 9 5
02 8 7 6
02 5 4 3
02 2 1 0
03 8 7 9
03 7 5 4
;
run;

proc sort data=multiple;
		by subject;
run;

data one;
	set multiple;
	by subject;
		MX=dif(x);
			if last.subject then do; LX=dif(x); LY=dif(y); LZ=dif(z); end;
run;

proc print data=one; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.jpg" style="width: 243px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14163i7DCCD284B8DAF811/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.jpg" alt="Untitled.jpg" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled2.jpg" style="width: 359px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14164iD2AB5F24EACF61BA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled2.jpg" alt="Untitled2.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 13:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383681#M91539</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-07-28T13:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383702#M91544</link>
      <description>&lt;P&gt;In general, the way you approach this is to calculate on every observation, then reset values to missing. &amp;nbsp;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LX = Mx;&lt;/P&gt;
&lt;P&gt;LY = dif(y);&lt;/P&gt;
&lt;P&gt;LZ = dif(z);&lt;/P&gt;
&lt;P&gt;if last.subject=0 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;lx = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;ly = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;lz = .;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note that you might want to re-set&amp;nbsp;MX:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MX = dif(x);&lt;/P&gt;
&lt;P&gt;if first.subject then mx=.;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 14:00:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383702#M91544</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-28T14:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383714#M91550</link>
      <description>&lt;P&gt;Thank you. It seems below is what I need.&lt;/P&gt;&lt;PRE&gt;data one (drop= MX MY MZ);
	set multiple;
	by subject;
		MX=dif(x);MY=dif(y);MZ=dif(z);
		if last.subject=0 then do; LX=.; LY=.; LY=.; end;
				else do; LX=MX; LY=MY; LZ=MZ; output; end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jul 2017 14:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383714#M91550</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-07-28T14:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383767#M91563</link>
      <description>&lt;P&gt;the Dif and Lag functions maintain separate queues of values. So when used inside an IF the queue contains the last time the condition was true, not the previous record.&lt;/P&gt;
&lt;P&gt;Note that your result for row 9 is the different with the previous LAST subject 2. And Subject 1 had no output because there was no previous "last subject".&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 15:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383767#M91563</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-07-28T15:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383800#M91571</link>
      <description>&lt;P&gt;If you are planning on outputting just the last observation for each SUBJECT (as in your latest program), you can use much less:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data one;&lt;/P&gt;
&lt;P&gt;set multiple;&lt;/P&gt;
&lt;P&gt;by subject;&lt;/P&gt;
&lt;P&gt;LX = dif(x);&lt;/P&gt;
&lt;P&gt;LY = dif(y);&lt;/P&gt;
&lt;P&gt;LZ = dif(x);&lt;/P&gt;
&lt;P&gt;if last.subject;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 16:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383800#M91571</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-28T16:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383804#M91572</link>
      <description>&lt;P&gt;Thank you. Here it is the task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14176i65BBD3C33CBFBEDA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.jpg" alt="Untitled.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here it is my solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one (drop= SumX X SumY Y SumZ Z);
	set multiple;
	by subject;
		difX3_2=dif(x);difY3_2=dif(y);difZ3_2=dif(z);
		difX3_1=dif2(x);difY3_1=dif2(y);difZ3_1=dif2(z);
		SumX+X;SumY+Y;SumZ+Z;
	if last.subject then do; MeanX=SumX/3;MeanY=SumY/3;MeanZ=SumZ/3; SumX=0; SumY=0; SumZ=0; output; end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 16:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383804#M91572</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-07-28T16:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383806#M91573</link>
      <description>&lt;P&gt;Given that (a) &amp;nbsp;you need to perform additional calculations, and (b) you need all the results on a single observation, I think you need to switch gears:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by subject;&lt;/P&gt;
&lt;P&gt;retain x1 x2 y1 y2 z1 z2;&lt;/P&gt;
&lt;P&gt;if first.subject then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;x1 = x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;y1 = y;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;z1 = 2;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else if last.subject=0 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;x2 = x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;y2 = y;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;z2 = z;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if last.subject;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*** Now you have all 9 values on a single observation. &amp;nbsp;Perform the final calculations in whatever way you would prefer;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 16:45:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383806#M91573</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-28T16:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: function dif(x) and last.observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383900#M91603</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;... I think you need to switch gears:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*** Now you have all 9 values on a single observation. &amp;nbsp;Perform the final calculations in whatever way you would prefer;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I think I got your idea...&amp;nbsp;&lt;/P&gt;&lt;P&gt;Actually it was a task to use LAG, DIF, MEAN. Sorry for not mentioning it.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14194iEBD514693DA032B3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.jpg" alt="Untitled.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jul 2017 04:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/function-dif-x-and-last-observation/m-p/383900#M91603</guid>
      <dc:creator>DmytroYermak</dc:creator>
      <dc:date>2017-07-29T04:33:23Z</dc:date>
    </item>
  </channel>
</rss>

