<?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 DIFn?! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800771#M315072</link>
    <description>&lt;P&gt;create a column _d, based on condition [here mod(i,10)], keep DIFn on i[n changes on mod(i,10)].&lt;/P&gt;
&lt;P&gt;Say mod(i,10)=5 to get DIF4(i), mod(i,10)=8 to get DIF3(i)... otherwise _D=.;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Mar 2022 02:44:18 GMT</pubDate>
    <dc:creator>hellohere</dc:creator>
    <dc:date>2022-03-08T02:44:18Z</dc:date>
    <item>
      <title>How to get DIFn?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800766#M315067</link>
      <description>&lt;P&gt;Code below. The DIFn return the dif value by GRP. But what I want is simple dif value by _N_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to do it?!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data temp;&lt;BR /&gt;do i=1 to 200;&lt;BR /&gt;grp=floor((i-1)/10);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;quit;&lt;/P&gt;
&lt;P&gt;data temp2;&lt;BR /&gt;set temp;&lt;BR /&gt;if i-floor(i/10)*10=5 then do;&lt;BR /&gt;_d=dif4(i);&lt;BR /&gt;end;&lt;BR /&gt;run;quit;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 02:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800766#M315067</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-03-08T02:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to get DIFn?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800767#M315068</link>
      <description>&lt;P&gt;_d shows up only on _N_=45/55/65... with value of 40.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What expected is to show up at 5/15/25... with value of 4&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 02:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800767#M315068</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-03-08T02:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to get DIFn?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800770#M315071</link>
      <description>&lt;P&gt;Could you please provide more details?:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*** USES i TO CALCULATE GROUPS ***;
DATA TEMP; 
      DO i = 1 TO 200;
              GROUP =  FLOOR( (i-1)/10 ); * max(GROUP) is 19;
              OUTPUT;
       END:
RUN:

*** WHAT DOES THIS STEP DO? ***;
* not using the GROUP variable;
DATA TEMP2;
      SET TEMP;
      IF i - FLOOR (i/10) * 10= 5 THEN DO;  
      * i - FLOOR (i/10) = 0.5? ;
               _D = DIF4( i ); * what is DIF4( )? ; 
      END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Mar 2022 02:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800770#M315071</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-03-08T02:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to get DIFn?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800771#M315072</link>
      <description>&lt;P&gt;create a column _d, based on condition [here mod(i,10)], keep DIFn on i[n changes on mod(i,10)].&lt;/P&gt;
&lt;P&gt;Say mod(i,10)=5 to get DIF4(i), mod(i,10)=8 to get DIF3(i)... otherwise _D=.;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 02:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800771#M315072</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-03-08T02:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to get DIFn?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800785#M315081</link>
      <description>&lt;P&gt;The DIF function, like the LAG function is a queue update function.&amp;nbsp; &amp;nbsp;In your IF statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if    i-floor(i/10)*10=5 then _d=dif4(i);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The queue is only updated at obs 5,15,15,25, 35,45,55,65,75,85, 95,.....,195.&amp;nbsp; &amp;nbsp;The first 4 times (5,15,...,35) the queue is updated the DIF4 function will return a missing value, because the queue needs 5 instances to fully populate with non-missing values.&amp;nbsp; Note that the queue is NOT updated for all the other observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you really want to do is update the queue at every observation, but keep the dif4 result only for obs 5,15,25,...195.&amp;nbsp; This is what I think you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  do i=1 to 200;
    grp=floor((i-1)/10);
    output;
  end;
run;
quit;

data temp2;
  set temp;
  _d=ifn(i-floor(i/10)*10=55,dif4(i),.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will return a value of 4 for obs 5,15,,25, etc., and a missing value otherwise.&amp;nbsp; That is because, unlike the IF statement, the IFN function &lt;EM&gt;&lt;STRONG&gt;always executes both the 2nd and 3rd arguments&lt;/STRONG&gt;&lt;/EM&gt;, regardless of which one it returns when assessing whether the first argument is true.&amp;nbsp; (It returns the 2nd arg when the first arg is true, otherwise it returns the 3rd arg).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the IFN function will update the DIF4 queue for every obs (not just every 10th obs as in the IF statement) you will be subtracting the 4th previous obs (not the 40th preceding obs) from the current obs but only keeping the result for every 10th obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are times when it is a good idea to put a DIF or LAG function in an IF statement.&amp;nbsp; But this is not such a time.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 04:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-DIFn/m-p/800785#M315081</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-08T04:01:41Z</dc:date>
    </item>
  </channel>
</rss>

