<?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: Find first observation for which all subsequent observations have a particular variable value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622069#M182956</link>
    <description>&lt;P&gt;How about:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by person_id claim_date;
   if first.person_id or not missing(payment) then first_0 = .;
   retain first_0;
   if first_0=. and payment = 0 then first_0 = claim_date;
   keep person_id first_0;
   if last.person_id and not missing(first_0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's untested, but looks about right.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Feb 2020 23:04:47 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2020-02-03T23:04:47Z</dc:date>
    <item>
      <title>Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622046#M182940</link>
      <description>&lt;P&gt;I have a claims dataset that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;person_id&amp;nbsp; &amp;nbsp;claim_date&amp;nbsp; &amp;nbsp; claim_id&amp;nbsp; &amp;nbsp; payment&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/01/2019&amp;nbsp; &amp;nbsp;100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/03/2019&amp;nbsp; &amp;nbsp;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/09/2019&amp;nbsp; &amp;nbsp;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12/20/2019&amp;nbsp; &amp;nbsp;103&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10/20/2019&amp;nbsp; &amp;nbsp;201&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10/21/2019&amp;nbsp; &amp;nbsp;202&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11/30/2019&amp;nbsp; &amp;nbsp;203&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/02/2019&amp;nbsp; &amp;nbsp;301&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/05/2019&amp;nbsp; &amp;nbsp;302&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05/20/2019&amp;nbsp; &amp;nbsp;304&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12/30/2019&amp;nbsp; &amp;nbsp;305&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12/31/2019&amp;nbsp; &amp;nbsp;306&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there is only one claim allowed per date in this dataset, the dataset is already sorted by claim_date, and the dataset only contains claims from 2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The goal is to identify the first claim_date, if any, for which both of the following are true: 1) payment equals 0; and 2) all subsequent observations for the person_id also had payment equal to 0. So for person_id = 1, the date would be 02/09/2019; for person_id = 2, no such date exists because all of their claims had non-zero payments; and for person_id = 3, no such date exists because of the positive payment on 12/31/2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's straightforward to identify the first claim-date with payment = 0 for each person_id (the first criteria), but I'm struggling with how to operationalize the second criteria. I'd appreciate any help the community can provide.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 21:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622046#M182940</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-02-03T21:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622059#M182951</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/312"&gt;@chuakp&lt;/a&gt;&amp;nbsp; Please post the expected output for the sample too, ty! I mean in other words what the output should look like?&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 22:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622059#M182951</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-03T22:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622069#M182956</link>
      <description>&lt;P&gt;How about:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by person_id claim_date;
   if first.person_id or not missing(payment) then first_0 = .;
   retain first_0;
   if first_0=. and payment = 0 then first_0 = claim_date;
   keep person_id first_0;
   if last.person_id and not missing(first_0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's untested, but looks about right.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 23:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622069#M182956</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-02-03T23:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622070#M182957</link>
      <description>&lt;P&gt;1. Assuming you want a filtered output to subset only the record that satisfies the stated condition .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input person_id   claim_date :mmddyy10.    claim_id    payment;
format  claim_date mmddyy10. ;
cards;
1                 01/01/2019   100            50
1                 01/03/2019   101            40
1                 02/09/2019   102            0
1                 12/20/2019   103            0
2                 10/20/2019   201            50
2                 10/21/2019   202            40
2                 11/30/2019   203            30
3                 04/02/2019   301            20
3                 04/05/2019   302            20
3                 05/20/2019   304            0
3                 12/30/2019   305            0
3                 12/31/2019   306            50
;
data want;
 do _n_=1 by 1until(last.person_id);
  set have;
  by person_id;
  if t1 then do; 
   t2=sumabs(payment,t2);
   continue;
  end;
  if payment=0 then t1=claim_date;
 end;
 do _n_=1 to _n_;
  set have;
  by person_id;
  if not t2 and claim_date=t1 then output;
 end;
 drop t:;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Assuming you want a FLAG that identified that particular record as 1 and other as 0's&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 do _n_=1 by 1until(last.person_id);
  set have;
  by person_id;
  if t1 then do; 
   t2=sumabs(payment,t2);
   continue;
  end;
  if payment=0 then t1=claim_date;
 end;
 do _n_=1 to _n_;
  set have;
  by person_id;
  Flag= not t2 and claim_date=t1;
  output;
 end;
 drop t:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If both the assumptions of your expected output isn't what it is, you posting the expected output will greatly help. Ty!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 23:06:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622070#M182957</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-03T23:06:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622091#M182965</link>
      <description>&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;Thank you. I believe I have to add this code&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;if payment NE 0 then first_0 = .;&lt;/SPAN&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;This way, the program finds the first instance of a payment = 0 for a person_id. The program looks at the next observation and determines if payment = 0. If so, then first_0 stays the same. If not, that means that first_0 cannot be the first date in 2019 in which all subsequent claims have zero payments, so we set first_0 back to missing. Without this piece of code, I believe the program would just output the last observation in which payment equaled zero; if there is no such observation, first_0 will be missing.&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; person_id claim_date&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;person_id or &lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;payment&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; first_0 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;retain&lt;/SPAN&gt; first_0&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; first_0&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; and payment &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; first_0 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; claim_date&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;   if payment NE 0 then first_0 = .;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt; person_id first_0&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; last&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;person_id and &lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;first_0&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 01:29:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622091#M182965</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-02-04T01:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622092#M182966</link>
      <description>&lt;P&gt;Thanks - I tried this, but it didn't quite work. To answer your question, I am looking for an output dataset with one row per person_id, a column for person_id, and a column for the first date on which all claims had zero payment and on which all subsequent claims had zero payment.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 01:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622092#M182966</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-02-04T01:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622097#M182968</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/312"&gt;@chuakp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks - I tried this, but it didn't quite work. To answer your question, I am looking for an output dataset with one row per person_id, a column for person_id, and a column for the first date on which all claims had zero payment and on which all subsequent claims had zero payment.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input person_id claim_date :mmddyy10. claim_id payment;
  format claim_date mmddyy10.;
  cards;
1 01/01/2019 100 50
1 01/03/2019 101 40
1 02/09/2019 102 0
1 12/20/2019 103 0
2 10/20/2019 201 50
2 10/21/2019 202 40
2 11/30/2019 203 30
3 04/02/2019 301 20
3 04/05/2019 302 20
3 05/20/2019 304 0
3 12/30/2019 305 0
3 12/31/2019 306 50
;

proc sql;
/*  create table want as*/
    select l.person_id, min(l.claim_date) as claim_date format=mmddyy10.
    from have l
    where 
      payment=0
      and not exists
        ( select * 
          from have i
          where 
            i.person_id=l.person_id 
            and i.payment&amp;gt;0
            and i.claim_date &amp;gt; l.claim_date
        )
    group by 1
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Feb 2020 02:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622097#M182968</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-04T02:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622098#M182969</link>
      <description>&lt;P&gt;You're right.&amp;nbsp; I was thinking that, but wrote it incorrectly here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   if first.person_id or not missing(payment) then first_0 = .;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Should have been:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   if first.person_id or payment ne 0 then first_0 = .;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Feb 2020 02:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622098#M182969</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-02-04T02:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Find first observation for which all subsequent observations have a particular variable value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622107#M182974</link>
      <description>&lt;P&gt;You have to go through each person_id twice - once to review only the non-zero payments, keeping the last date of this subgroup (last_non_zero).&amp;nbsp; The second time reads ALL the records, setting DUMMY=1 only for the case in which current payment=0 and the single record lag of date matches last_non_zero:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (where=(payment^=0) in=non_zero_payment)
      have (in=second_pass);
  by person_id;
  if non_zero_payment=1 then _last_non_zero=claim_date;
  else if first.person_id then _last_non_zero=.;
  retain _last_non_zero;
  if second_pass;
  dummy=ifn(payment=0 and lag(claim_date)=_last_non_zero,1,0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The SET statement reads each person_id twice.&amp;nbsp; The first pass reads only non-zero payment and the second reads all records.&lt;/LI&gt;
&lt;LI&gt;The _last_non_zero date variable is updated with each non-zero payment.&amp;nbsp; Since _last_non_zero is a retained variable, then it is reset to missing whenever a person_id has ONLY zero payments.&lt;/LI&gt;
&lt;LI&gt;The "if second_pass" is a subsetting if.&lt;/LI&gt;
&lt;LI&gt;The DUMMY= expression tests for current payment=0 and lag of date matching the _last_non_zero date.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited addition: This program assume data are sorted by person_id/claim_date, but it purposely only used "BY PERSON_ID" in the by-statement.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 04:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-first-observation-for-which-all-subsequent-observations/m-p/622107#M182974</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-04T04:34:05Z</dc:date>
    </item>
  </channel>
</rss>

