<?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: Use Loop + Macro (Maybe) to run if/then statement on dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819195#M323374</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/244240"&gt;@altatunc&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is a little difficult to figure out what you are trying to achieve. So my guess is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You have some data with observations containing a time interval Start_Date-End_Date spanning any period.&lt;/LI&gt;
&lt;LI&gt;You want to test the time interval against a consecutive list of years (one for each year in your code).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;for each observation, you want an output record for each year where October1. in the given year is present in the time interval.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If my guess is correct, then this code will work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format Start_Date End_Date date9.;
  ID = 1; Start_Date = '03apr2004'd; End_Date = '31dec2012'd; output;
  ID = 2; Start_Date = '01oct2018'd; End_Date = '31aug2020'd; output;
run;

data want;
  set have;
  do assigned_year = 2008 to 2021;
   if Start_Date &amp;lt;= mdy(10,1,assigned_year) &amp;lt;= End_Date then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="years.gif" style="width: 363px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72465iDD7F6057DD14995E/image-size/large?v=v2&amp;amp;px=999" role="button" title="years.gif" alt="years.gif" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Jun 2022 18:56:17 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2022-06-20T18:56:17Z</dc:date>
    <item>
      <title>Use Loop + Macro (Maybe) to run if/then statement on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819084#M323331</link>
      <description>&lt;P&gt;I have a data set with start and stop dates and i need to assign it a year based off some simple logic.&amp;nbsp; Given the years span unknown number of years, I'd like to embed this into a do loop or macro or both. Also just trying to understand loops and macros a bit more here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what the logic would look like for two years:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;If Start_Date &amp;lt;= "01OCT010"d and End_Date &amp;gt; "01OCT2010"d and Start_Date ne "31DEC9999"d
             then Assigned_Year = 2010;
If Start_Date &amp;lt;= "01OCT011"d and End_Date &amp;gt; "01OCT2011"d and Start_Date ne "31DEC9999"d
            then Assigned_Year = 2011; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In short, what is the best way to embed this into a loop/macro to assign this?&amp;nbsp; I can assign it a starting year or date but still struggling with the syntax just a bit.&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2022 01:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819084#M323331</guid>
      <dc:creator>altatunc</dc:creator>
      <dc:date>2022-06-20T01:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Use Loop + Macro (Maybe) to run if/then statement on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819101#M323340</link>
      <description>&lt;P&gt;And which assigned_year do you want for start_date=2010-01-01 and end_date=2012-12-31?&lt;/P&gt;
&lt;P&gt;2009, 2010, 2011 or 2012?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2022 06:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819101#M323340</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-20T06:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Use Loop + Macro (Maybe) to run if/then statement on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819167#M323361</link>
      <description>&lt;P&gt;First hint: Use of 2-digit years is a BAD IDEA. Google costs associated with Y2K. There is absolutely no reason to use 2-digit years in date literals "01OCT10"d really should be "01OCT2010"D&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second hint: If the use of '31DEC9999'd is for anything resembling invalid, not present, not recorded or such meaning then you may actually want to use a MISSING value in SAS instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just how many of these conditional values are to be assigned? &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;P&gt;&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, 20 Jun 2022 15:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819167#M323361</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-20T15:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Use Loop + Macro (Maybe) to run if/then statement on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819195#M323374</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/244240"&gt;@altatunc&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is a little difficult to figure out what you are trying to achieve. So my guess is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You have some data with observations containing a time interval Start_Date-End_Date spanning any period.&lt;/LI&gt;
&lt;LI&gt;You want to test the time interval against a consecutive list of years (one for each year in your code).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;for each observation, you want an output record for each year where October1. in the given year is present in the time interval.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If my guess is correct, then this code will work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format Start_Date End_Date date9.;
  ID = 1; Start_Date = '03apr2004'd; End_Date = '31dec2012'd; output;
  ID = 2; Start_Date = '01oct2018'd; End_Date = '31aug2020'd; output;
run;

data want;
  set have;
  do assigned_year = 2008 to 2021;
   if Start_Date &amp;lt;= mdy(10,1,assigned_year) &amp;lt;= End_Date then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="years.gif" style="width: 363px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72465iDD7F6057DD14995E/image-size/large?v=v2&amp;amp;px=999" role="button" title="years.gif" alt="years.gif" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2022 18:56:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819195#M323374</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-06-20T18:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: Use Loop + Macro (Maybe) to run if/then statement on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819250#M323399</link>
      <description>&lt;P&gt;Please post sample data and show the expected result so that we understand better what you are really doing.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jun 2022 07:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819250#M323399</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-06-21T07:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Use Loop + Macro (Maybe) to run if/then statement on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819332#M323417</link>
      <description>Thanks so much.  This is exactly what i was looking for and far simpler than i thought at that!&lt;BR /&gt;Thanks again,&lt;BR /&gt;AT</description>
      <pubDate>Tue, 21 Jun 2022 14:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Loop-Macro-Maybe-to-run-if-then-statement-on-dates/m-p/819332#M323417</guid>
      <dc:creator>altatunc</dc:creator>
      <dc:date>2022-06-21T14:41:48Z</dc:date>
    </item>
  </channel>
</rss>

