<?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 find the first record the value is 1 and  lasted 24 hours in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928629#M83452</link>
    <description>&lt;P&gt;hello, I want to find the first record: the value is 1 and &amp;nbsp;lasted 24 hours, like my picture：&lt;/P&gt;&lt;P&gt;in obs 1,the value is 1 and the record after 24h is obs&amp;nbsp; 4，but between these two records is a record with AVALCN=0,so&amp;nbsp; obs 1&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;in obs 2&amp;nbsp;,the value is 1 and the record after 24h is obs&amp;nbsp; 5，but between these two records is a record with AVALCN=0,so&amp;nbsp; obs 2&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;in obs 3,AVALCN=0 ,,so&amp;nbsp; obs 2&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;in obs 4,,the value is 1 and the record after 24h is obs&amp;nbsp; 6，the value between these two records is 1 ,so&amp;nbsp; obs 4&amp;nbsp; is Y;&lt;/P&gt;&lt;P&gt;in obs 5,the value is 1 ，no records were obtained after 24h,so&amp;nbsp; obs 5&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image_2024_05_16T07_30_22_310Z.png" style="width: 291px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96568i19D4775DB88D3A04/image-size/large?v=v2&amp;amp;px=999" role="button" title="image_2024_05_16T07_30_22_310Z.png" alt="image_2024_05_16T07_30_22_310Z.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 16 May 2024 08:33:58 GMT</pubDate>
    <dc:creator>ShanshanCheng</dc:creator>
    <dc:date>2024-05-16T08:33:58Z</dc:date>
    <item>
      <title>find the first record the value is 1 and  lasted 24 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928629#M83452</link>
      <description>&lt;P&gt;hello, I want to find the first record: the value is 1 and &amp;nbsp;lasted 24 hours, like my picture：&lt;/P&gt;&lt;P&gt;in obs 1,the value is 1 and the record after 24h is obs&amp;nbsp; 4，but between these two records is a record with AVALCN=0,so&amp;nbsp; obs 1&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;in obs 2&amp;nbsp;,the value is 1 and the record after 24h is obs&amp;nbsp; 5，but between these two records is a record with AVALCN=0,so&amp;nbsp; obs 2&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;in obs 3,AVALCN=0 ,,so&amp;nbsp; obs 2&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;in obs 4,,the value is 1 and the record after 24h is obs&amp;nbsp; 6，the value between these two records is 1 ,so&amp;nbsp; obs 4&amp;nbsp; is Y;&lt;/P&gt;&lt;P&gt;in obs 5,the value is 1 ，no records were obtained after 24h,so&amp;nbsp; obs 5&amp;nbsp; is N;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image_2024_05_16T07_30_22_310Z.png" style="width: 291px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96568i19D4775DB88D3A04/image-size/large?v=v2&amp;amp;px=999" role="button" title="image_2024_05_16T07_30_22_310Z.png" alt="image_2024_05_16T07_30_22_310Z.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 08:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928629#M83452</guid>
      <dc:creator>ShanshanCheng</dc:creator>
      <dc:date>2024-05-16T08:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: find the first record the value is 1 and  lasted 24 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928631#M83453</link>
      <description>&lt;P&gt;You can use a hash table to do "look forward" your data.&lt;/P&gt;
&lt;P&gt;I assume you probably need it in groups by subjectid, so I added them in example code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do subjectID = 1 to 3;
    do obs =1 to 15;
      avalcn=ranuni(42)&amp;lt;0.8;
      adtm + 7*60*60+17;
      output; 
    end;
  end;
  format adtm e8601dt20.;
run;
proc print;
run;

data want;
  declare hash H();
  H.defineKey("i");
  H.defineData("v","d","l");
  H.defineDone();
  do until(eof);
    set have end=eof;
    by subjectID;
    i+1;
    H.add(key:i, data:avalcn, data:adtm, data:last.subjectID);
  end;


  do until(eof1);
    set have end=eof1 curobs=curobs;
    by subjectID;

    flag="N";
    l=0;
    v=avalcn; 
    d=adtm;
    if not Found then
      do i=curobs by 1 until(l);
        H.find(key:i);
        if 0=v then leave;
        if abs(adtm-d)&amp;gt;86400 then 
          do;
            flag="Y";
            Found = 1;
            /* obs_contributed_to_flagY=i; */
            leave;
          end;
      end;

    output;
    if last.subjectID then Found = 0;
  end;
stop;
drop i v d l Found;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 09:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928631#M83453</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-05-16T09:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: find the first record the value is 1 and  lasted 24 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928632#M83454</link>
      <description>&lt;P&gt;Version with arrays just for completeness:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputX("N",n);
  stop;
  set have nobs=n;
run;
data want2;
  
  array v[&amp;amp;n.] _temporary_;
  array d[&amp;amp;n.] _temporary_;
  array l[&amp;amp;n.] _temporary_;
  do until(eof);
    set have end=eof curobs=i;
    by subjectID;
    
    v[i] = avalcn; 
    d[i] = adtm;
    l[i] = last.subjectID;
  end;


  do until(eof1);
    set have end=eof1 curobs=curobs;
    by subjectID;

    flag="N";
    if not Found then
      do i=curobs by 1 until(l[i]);
        if 0=v[i] then leave;
        if abs(adtm-d[i])&amp;gt;86400 then 
          do;
            flag="Y";
            Found = 1;
            /* obs_contributed_to_flagY=i; */
            leave;
          end;
      end;

    output;
    if last.subjectID then Found = 0;
  end;
stop;
drop Found;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 09:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928632#M83454</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-05-16T09:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: find the first record the value is 1 and  lasted 24 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928637#M83455</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465138"&gt;@ShanshanCheng&lt;/a&gt;&amp;nbsp;Make it a habit to provide sample data as text - ideally shared via a data step that creates it. Screenshots are not very useful if you're after tested code as solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does below return what you're after?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dlm=',' dsd;
  input subject_id:$5. avalcn:best32. adtm:e8601dt.;
  format adtm datetime20.;
  datalines;
A,1,2024-04-14T01:36
A,1,2024-04-14T08:36
A,0,2024-04-14T20:05
A,1,2024-04-15T08:38
A,1,2024-04-15T20:03
A,1,2024-04-16T08:50
B,1,2024-05-14T01:36
B,1,2024-05-14T08:36
B,1,2024-05-15T08:38
B,1,2024-05-15T20:03
B,1,2024-05-16T08:50
;

data want(drop=_:);
  set have nobs=_nobs;
  by subject_id adtm;
  if avalcn=0 then return;
  retain _start_obs;
  _start_obs=max(_start_obs,_n_+1);

  do i=_start_obs to _nobs;
    set have(keep=subject_id avalcn adtm
             rename=(subject_id=_subject_id avalcn=_avalcn adtm=_adtm))
            point=i;
    if _avalcn=0 or _subject_id ne subject_id then leave;
    if _adtm-adtm&amp;gt;=24*3600 then
      do;
        select_flag=1;
        _start_obs=i;
        leave;
      end;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&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="Patrick_0-1715855826634.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96570iEB7561DF2A244DAA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1715855826634.png" alt="Patrick_0-1715855826634.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 10:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928637#M83455</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-16T10:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: find the first record the value is 1 and  lasted 24 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928786#M83458</link>
      <description>&lt;P&gt;&amp;nbsp;Sorry ,the next question I'll use datalines provide test data instead of screenshots，thank you for your answer and solving my problem.&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2024 03:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/find-the-first-record-the-value-is-1-and-lasted-24-hours/m-p/928786#M83458</guid>
      <dc:creator>ShanshanCheng</dc:creator>
      <dc:date>2024-05-17T03:14:01Z</dc:date>
    </item>
  </channel>
</rss>

