<?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: Creating a new variable/flag based on flag indicator with latest date within a range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848729#M335543</link>
    <description>&lt;P&gt;Please post your example data as a data step, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards missover;
  input studyID baselinedate mmddyy10. +1 Flag1	Date1 mmddyy10. +1 Flag2 Date2 mmddyy10. +1 Flag3 Date3 mmddyy10.;
  format baselinedate Date1 Date2 Date3 mmddyy10.;
cards;
1 01/10/2000 1 02/04/2000 0 05/10/2000 1 06/17/2000
2 04/29/2008 1 07/07/2008 0 08/08/2008	 	 
3 09/13/2004 1 09/29/2004 0 05/05/2008	 	 
;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that the dates are sorted (as they are in your example) this should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array flags(*) flag1-flag3;
  array dates(*) date1-date3;
  new_variable=0;
  do _N_=1 to dim(flags);
    if missing(dates(_N_)) then leave;
    if intck('YEAR',baselinedate,dates(_N_))&amp;lt;1 then
      new_variable=flags(_N_);
    else leave;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not quite sure what you want to do with dates exactly one year after the baseline, in the above code they are not considered.&lt;/P&gt;
&lt;P&gt;If you want to include them, change the IF statement to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    if intck('YEAR',baselinedate,dates(_N_),'C')&amp;lt;=1 then&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 09 Dec 2022 14:58:48 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2022-12-09T14:58:48Z</dc:date>
    <item>
      <title>Creating a new variable/flag based on flag indicator with latest date within a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848610#M335502</link>
      <description>&lt;P&gt;Hi all, I've been spinning my wheels on this for a while and hoping to get some help. I have a series of flag indicators with associated dates, and need to select the last date within a range (a year from a baseline date), and then create a new variable based on a flag associated with that last date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data look kind of like this:&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;studyID&lt;/TD&gt;&lt;TD&gt;baselinedate&lt;/TD&gt;&lt;TD&gt;Flag1&lt;/TD&gt;&lt;TD&gt;Date1&lt;/TD&gt;&lt;TD&gt;Flag2&lt;/TD&gt;&lt;TD&gt;Date2&lt;/TD&gt;&lt;TD&gt;Flag3&lt;/TD&gt;&lt;TD&gt;Date3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01/10/2000&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;02/04/2000&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;05/10/2000&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;06/17/2000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;04/29/2008&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;07/07/2008&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;08/08/2008&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;09/13/2004&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;09/29/2004&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;05/05/2008&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&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;So, for all of them, I want to scan Date1, Date2, and Date3 for dates that are within a year of the baselinedate. For the first studyID, because all three dates are within a year, and the latest flag (Flag3)=1, I then want NewVariable=1. For studyID=2, both dates are within a year of baseline but because latest flag Flag2=0, I want NewVariable=2. For studyID=3, only Date1 is with the year and because that flag is 1, then NewVariable=1 (and Flag2/Date2 are ignored as they are past the year mark).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to do this within a data step but I am feeling like the conditionals involved here might be too complex? But not sure how else to approach. Any help would be much appreciated. Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 19:04:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848610#M335502</guid>
      <dc:creator>epialy</dc:creator>
      <dc:date>2022-12-08T19:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable/flag based on flag indicator with latest date within a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848635#M335507</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */

data want;
    set have;
    array d date1-date3;
    array f flag1-flag3;
    do i = 1 to dim(d);
        if d(i)-baselinedate &amp;gt; 365 then d(i)=.;
    end;
    max_date_within_1yr = max(of d(*));
    flag_max_date = f(max_date_within_1yr,whichn(of d(*)));
    drop i;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you set dates to beyond 1 year equal to missing, the calculations are much simpler. After the code sets these dates beyond 1 year to missing, we find the max value, and then we find the flag associated with that value. That gives you the 0 or 1 for that max date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want tested code, please provide data as working SAS data step code, which you can type in yourself, or which you can get from &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt;. We cannot work from data that is a screen capture or file attachment.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 20:10:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848635#M335507</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-08T20:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable/flag based on flag indicator with latest date within a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848729#M335543</link>
      <description>&lt;P&gt;Please post your example data as a data step, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards missover;
  input studyID baselinedate mmddyy10. +1 Flag1	Date1 mmddyy10. +1 Flag2 Date2 mmddyy10. +1 Flag3 Date3 mmddyy10.;
  format baselinedate Date1 Date2 Date3 mmddyy10.;
cards;
1 01/10/2000 1 02/04/2000 0 05/10/2000 1 06/17/2000
2 04/29/2008 1 07/07/2008 0 08/08/2008	 	 
3 09/13/2004 1 09/29/2004 0 05/05/2008	 	 
;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that the dates are sorted (as they are in your example) this should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array flags(*) flag1-flag3;
  array dates(*) date1-date3;
  new_variable=0;
  do _N_=1 to dim(flags);
    if missing(dates(_N_)) then leave;
    if intck('YEAR',baselinedate,dates(_N_))&amp;lt;1 then
      new_variable=flags(_N_);
    else leave;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not quite sure what you want to do with dates exactly one year after the baseline, in the above code they are not considered.&lt;/P&gt;
&lt;P&gt;If you want to include them, change the IF statement to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    if intck('YEAR',baselinedate,dates(_N_),'C')&amp;lt;=1 then&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Dec 2022 14:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848729#M335543</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-12-09T14:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable/flag based on flag indicator with latest date within a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848730#M335544</link>
      <description>&lt;P&gt;Your code&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;if d(i)-baselinedate &amp;gt; 365 then d(i)=.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;is a bit problematic, regarding leap years (and actually all the years in the example data are leap years, with 366 days).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it is better to use the INTCK function.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 14:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/848730#M335544</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-12-09T14:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable/flag based on flag indicator with latest date within a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/849284#M335773</link>
      <description>&lt;P&gt;Thank you! I was able to tweak this for exactly what I needed.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2022 23:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-flag-based-on-flag-indicator-with-latest/m-p/849284#M335773</guid>
      <dc:creator>epialy</dc:creator>
      <dc:date>2022-12-12T23:41:46Z</dc:date>
    </item>
  </channel>
</rss>

