<?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 Dif is going back two rows on the second observation in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403698#M66901</link>
    <description>&lt;P&gt;I cannot figure out why the dif function is going back 2 rows&amp;nbsp;in the&amp;nbsp;second observation.&amp;nbsp; It&amp;nbsp;works for the first observation and all subsequent observations.&amp;nbsp; In the 2nd and fourth row the dif function is not calculating the value I would like.&amp;nbsp; I am sure that my lack of understanding of first is the culprit, but I can't figure it out.&amp;nbsp; (Row 2 difference should be 0, Row 4 difference should be 1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is greatly appreciated (I use SAS EG 7.1, SAS 9.4).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output data&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ClaimNum&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Participant_Name&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;TX_DOS&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;difference&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;CLAIM1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;NAME1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;9/13/2016&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;CLAIM1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;NAME1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;9/13/2016&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;CLAIM2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;NAME2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8/25/2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;CLAIM2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;NAME2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8/26/2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;-384&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;CLAIM2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;NAME2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8/27/2015&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS Code&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA EM_LIST_W_DAYS;
  set WORK.EM_LIST;

       by ClaimNum ParticipantID;
             if first.ClaimNum and first.ParticipantID then 
                      difference = 0;
             else if first.ClaimNum=0 and first.ParticipantID=1 then 
                      difference = 0;
             else if first.ClaimNum=0 and first.ParticipantID=0 then
                      difference = DIF(TX_DOS);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Oct 2017 20:54:34 GMT</pubDate>
    <dc:creator>Mar_Lds</dc:creator>
    <dc:date>2017-10-12T20:54:34Z</dc:date>
    <item>
      <title>Dif is going back two rows on the second observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403698#M66901</link>
      <description>&lt;P&gt;I cannot figure out why the dif function is going back 2 rows&amp;nbsp;in the&amp;nbsp;second observation.&amp;nbsp; It&amp;nbsp;works for the first observation and all subsequent observations.&amp;nbsp; In the 2nd and fourth row the dif function is not calculating the value I would like.&amp;nbsp; I am sure that my lack of understanding of first is the culprit, but I can't figure it out.&amp;nbsp; (Row 2 difference should be 0, Row 4 difference should be 1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is greatly appreciated (I use SAS EG 7.1, SAS 9.4).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output data&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ClaimNum&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Participant_Name&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;TX_DOS&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;difference&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;CLAIM1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;NAME1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;9/13/2016&lt;/P&gt;&lt;/TD&gt;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ipantID;
             if first.ClaimNum and first.ParticipantID then 
                      difference = 0;
             else if first.ClaimNum=0 and first.ParticipantID=1 then 
                      difference = 0;
             else if first.ClaimNum=0 and first.ParticipantID=0 then
                      difference = DIF(TX_DOS);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 20:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403698#M66901</guid>
      <dc:creator>Mar_Lds</dc:creator>
      <dc:date>2017-10-12T20:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dif is going back two rows on the second observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403701#M66902</link>
      <description>&lt;P&gt;For DIF to operate on consecutive observations, it must execute on every observation.&amp;nbsp; So calculate something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;temp = dif(TX_DOS);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then&amp;nbsp;use TEMP in your IF/THEN statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;else if first.ClaimNum=0 and first.ParticipantID=0 then difference=temp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 21:06:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403701#M66902</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-12T21:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: Dif is going back two rows on the second observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403706#M66903</link>
      <description>&lt;P&gt;DIF updates a queue of differences.&amp;nbsp; If you only occasionally execute DIF then the sequence of differences are applied only to the corresponding observations.&amp;nbsp; This program accommodates that property:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA EM_LIST_W_DAYS;
  set WORK.EM_LIST;
  by ClaimNum ParticipantID;
  dif=ifn(first.paarticipantid,0,dix(tx_dos));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Two points:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Using "by cliaminum participantid;".&amp;nbsp;&amp;nbsp;The&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;first.&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.&lt;/STRONG&gt;&lt;/EM&gt; indicators are hierarchical.&amp;nbsp; &amp;nbsp;All instances of first.claimnum=1 are subsets of the instances in which first.participantid=1.&amp;nbsp; And (less petinent in your case) every time first.claimnum=1 all the by-variables to its right will have first. indicators set to 1.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The IFN function evaluates both outcomes (the 2nd and 3rd arguments), regardless of the results of the first argument.&amp;nbsp; As a result the DIF function is updated with every observation, even though it is not always assigned to the result variable.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 12 Oct 2017 21:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Dif-is-going-back-two-rows-on-the-second-observation/m-p/403706#M66903</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-12T21:21:31Z</dc:date>
    </item>
  </channel>
</rss>

