<?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: mark flag based on date difference in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721492#M223643</link>
    <description>&lt;P&gt;What have you tried so far?&lt;/P&gt;
&lt;P&gt;Questions like yours have been asked multiple, so you may want to read the answer given and try to adopt them to your needs.&lt;/P&gt;</description>
    <pubDate>Wed, 24 Feb 2021 08:17:34 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2021-02-24T08:17:34Z</dc:date>
    <item>
      <title>mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721488#M223639</link>
      <description>&lt;P&gt;hi&lt;/P&gt;&lt;P&gt;i have to give flag "Y" if variable var=2 for two or more consecutive days based on vairable date (Y for each record per ID if the condition is met)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;var&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;24FEB2021&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;25FEB2021&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;26FEB2021&lt;/TD&gt;&lt;TD&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;24FEB2021&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;25FEB2021&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for your help&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 07:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721488#M223639</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-24T07:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721491#M223642</link>
      <description>&lt;P&gt;What have you tried do far?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use a data step, with BY ID VAR.&lt;/P&gt;
&lt;P&gt;You can figure out if there are more than one observation for your by group by using IF NOT FIRST.VAR AND VAR = 2 THEN...&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 08:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721491#M223642</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2021-02-24T08:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721492#M223643</link>
      <description>&lt;P&gt;What have you tried so far?&lt;/P&gt;
&lt;P&gt;Questions like yours have been asked multiple, so you may want to read the answer given and try to adopt them to your needs.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 08:17:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721492#M223643</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-02-24T08:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721496#M223645</link>
      <description>if you don't want to help why are you posting ? I've tried to calculate time difference between them and then mark the flag but I don't know how can I check if it is consecutive, or to give flag for the first observation</description>
      <pubDate>Wed, 24 Feb 2021 08:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721496#M223645</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-24T08:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721497#M223646</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID=001, date=24FEB2021 is the first observation of ID=001, so there are no consecutive days, and I don't think the Y flag will be raised.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length id $3 var 8 date 8;
  format date date9.;
  input id var date date9.;
datalines;
001 2 24FEB2021
001 2 25FEB2021
001 2 26FEB2021
002 2 24FEB2021
002 1 25FEB2021
;
run;

proc sort data=have;
  by id date;
run;

data want(drop=dt);
 set have;
 by id date;
 length flag $1;
 retain dt;
 if first.id then dt=0;
 flag=ifc(dt+1=date,'Y','');
 dt=date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Feb 2021 08:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721497#M223646</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-24T08:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721499#M223647</link>
      <description>no, it's wrong .&lt;BR /&gt;you should have "Y" in ID001 24feb2021 cause the question is "if var is 2 for two or more consecutive days, mark all observations within this ID as flag="Y";&lt;BR /&gt;and you have flag on ID002 25FEB2021 and it shouldn't be cause var=1&lt;BR /&gt;</description>
      <pubDate>Wed, 24 Feb 2021 08:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721499#M223647</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-24T08:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721502#M223648</link>
      <description>&lt;P&gt;OK.&lt;/P&gt;
&lt;P&gt;You could process consecutive days in ascending order, do the same process in descending order, and then return to ascending order.&lt;/P&gt;
&lt;P&gt;There's some sorting involved, but it's simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length id $3 var 8 date 8;
  format date date9.;
  input id var date date9.;
datalines;
001 2 24FEB2021
001 2 25FEB2021
001 2 26FEB2021
002 2 24FEB2021
002 1 25FEB2021
;
run;

proc sort data=have;
  by id date;
run;

data next(drop=dt v);
  set have;
  by id date;
  length flag $1;
  retain dt v;
  if first.id then dt=0;
  if dt+1=date and v=2 and v=var then flag='Y';
  dt=date;
  v=var;
run;

proc sort data=next;
  by id descending date;
run;

data want(drop=dt v);
  set next;
  by id descending date;
  retain dt v;
  if first.id then dt=0;
  if dt-1=date and v=2 and v=var then flag='Y';
  dt=date;
  v=var;
run;

proc sort data=want;
  by id date;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Feb 2021 08:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721502#M223648</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-02-24T08:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: mark flag based on date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721507#M223650</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107827"&gt;@Jedrzej&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;if you don't want to help why are you posting ? I've tried to calculate time difference between them and then mark the flag but I don't know how can I check if it is consecutive, or to give flag for the first observation&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please show us your code, as we have gotten quite wary of freeloaders trying to cheat on their homework or similar.&lt;/P&gt;
&lt;P&gt;We want to&amp;nbsp;&lt;EM&gt;help&lt;/EM&gt;, but we're not in the business of doing other's work for free. If you need a consultant's work, hire one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But here's a code example using a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ var date :date9.;
format date yymmdd10.;
datalines;
001 2 24FEB2021
001 2 25FEB2021
001 2 26FEB2021
002 2 24FEB2021
002 1 25FEB2021
;

data want;
do until(last.id);
  set have;
  by id;
  if date - lag(date) = 1 and lag(var) = 2 and var = 2 and not first.id
  then flag = "Y";
end;
do until (last.id);
  set have;
  by id;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Feb 2021 09:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/mark-flag-based-on-date-difference/m-p/721507#M223650</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-24T09:15:35Z</dc:date>
    </item>
  </channel>
</rss>

