<?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: Array code with date as an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-code-with-date-as-an-array/m-p/794904#M254916</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _caldate(2017:2020) caldate17-caldate20;
array _rcaldate(2017:2020) rcaldate17-rcaldate20;

do year=2017 to 2020;
start_date= mdy(1, 1, year);
end_date = mdy(12, 31, year);


 if (admit_dt&amp;lt;=end_date  and disch_dt&amp;gt;= start_date ) then do;
 
  if admit_dt&amp;lt;start_date then _caldate(Year)=start_date;
else _caldate(Year)=admit_dt;

      if dshy = . or disch_dt&amp;gt;=end_date then _rcaldate(year)=end_date;
 else if dshy  ne . and disch_dt=&amp;lt; end_date then _rcaldate(Year)= srdate;

 end;

end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;Declare your arrays&lt;/LI&gt;
&lt;LI&gt;Index using years to make your life easier&lt;/LI&gt;
&lt;LI&gt;Create start/end date separately to make the code easier to follow and more efficient&lt;/LI&gt;
&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have the following code that I want to be able to run as an array (or a macro) within a larger datastep.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the following code I have an admission date (admit_dt) and Discharge date (disch_dt) where on the date condition I create two new variables, caldate and rcaldate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see I have the condition that if the admission date is &amp;lt;= Dec 31, 2019 and the discharge date is &amp;gt;= Jan 1 2019 then create&amp;nbsp;caldate and&amp;nbsp;rcaldate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is that I would like to run this for a 4 years of data (2017, 2018, 2019 and 2020) and create the variable caldate17, caldate18, caldate19 and caldate20 and then&amp;nbsp;rcaldate7,&amp;nbsp;rcaldate18,&amp;nbsp;rcaldate19 and&amp;nbsp;rcaldate20 . Instead of copy and pasting this code four times, how can I create an array (or a macro) for the below code?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt; if (admit_dt&amp;lt;='31dec2019'd  and disch_dt&amp;gt;='01jan2019'd ) then do;
 
  if admit_dt&amp;lt;'01jan2019'd then caldate19='01jan2019'd;
else caldate19=admit_dt;

      if dshy = . or disch_dt&amp;gt;='31dec2019'd then rcaldate19='31dec2019'd;
 else if dshy  ne . and disch_dt=&amp;lt; '31dec2019'd then rcaldate19= srdate;

 end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Feb 2022 01:31:25 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-02-08T01:31:25Z</dc:date>
    <item>
      <title>Array code with date as an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-code-with-date-as-an-array/m-p/794903#M254915</link>
      <description>&lt;P&gt;I have the following code that I want to be able to run as an array (or a macro) within a larger datastep.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the following code I have an admission date (admit_dt) and Discharge date (disch_dt) where on the date condition I create two new variables, caldate and rcaldate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see I have the condition that if the admission date is &amp;lt;= Dec 31, 2019 and the discharge date is &amp;gt;= Jan 1 2019 then create&amp;nbsp;caldate and&amp;nbsp;rcaldate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would like to do is that I would like to run this for a 4 years of data (2017, 2018, 2019 and 2020) and create the variable caldate17, caldate18, caldate19 and caldate20 and then&amp;nbsp;rcaldate7,&amp;nbsp;rcaldate18,&amp;nbsp;rcaldate19 and&amp;nbsp;rcaldate20 . Instead of copy and pasting this code four times, how can I create an array (or a macro) for the below code?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt; if (admit_dt&amp;lt;='31dec2019'd  and disch_dt&amp;gt;='01jan2019'd ) then do;
 
  if admit_dt&amp;lt;'01jan2019'd then caldate19='01jan2019'd;
else caldate19=admit_dt;

      if dshy = . or disch_dt&amp;gt;='31dec2019'd then rcaldate19='31dec2019'd;
 else if dshy  ne . and disch_dt=&amp;lt; '31dec2019'd then rcaldate19= srdate;

 end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Feb 2022 01:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-code-with-date-as-an-array/m-p/794903#M254915</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2022-02-08T01:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Array code with date as an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-code-with-date-as-an-array/m-p/794904#M254916</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _caldate(2017:2020) caldate17-caldate20;
array _rcaldate(2017:2020) rcaldate17-rcaldate20;

do year=2017 to 2020;
start_date= mdy(1, 1, year);
end_date = mdy(12, 31, year);


 if (admit_dt&amp;lt;=end_date  and disch_dt&amp;gt;= start_date ) then do;
 
  if admit_dt&amp;lt;start_date then _caldate(Year)=start_date;
else _caldate(Year)=admit_dt;

      if dshy = . or disch_dt&amp;gt;=end_date then _rcaldate(year)=end_date;
 else if dshy  ne . and disch_dt=&amp;lt; end_date then _rcaldate(Year)= srdate;

 end;

end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;Declare your arrays&lt;/LI&gt;
&lt;LI&gt;Index using years to make your life easier&lt;/LI&gt;
&lt;LI&gt;Create start/end date separately to make the code easier to follow and more efficient&lt;/LI&gt;
&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have the following code that I want to be able to run as an array (or a macro) within a larger datastep.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the following code I have an admission date (admit_dt) and Discharge date (disch_dt) where on the date condition I create two new variables, caldate and rcaldate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see I have the condition that if the admission date is &amp;lt;= Dec 31, 2019 and the discharge date is &amp;gt;= Jan 1 2019 then create&amp;nbsp;caldate and&amp;nbsp;rcaldate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is that I would like to run this for a 4 years of data (2017, 2018, 2019 and 2020) and create the variable caldate17, caldate18, caldate19 and caldate20 and then&amp;nbsp;rcaldate7,&amp;nbsp;rcaldate18,&amp;nbsp;rcaldate19 and&amp;nbsp;rcaldate20 . Instead of copy and pasting this code four times, how can I create an array (or a macro) for the below code?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt; if (admit_dt&amp;lt;='31dec2019'd  and disch_dt&amp;gt;='01jan2019'd ) then do;
 
  if admit_dt&amp;lt;'01jan2019'd then caldate19='01jan2019'd;
else caldate19=admit_dt;

      if dshy = . or disch_dt&amp;gt;='31dec2019'd then rcaldate19='31dec2019'd;
 else if dshy  ne . and disch_dt=&amp;lt; '31dec2019'd then rcaldate19= srdate;

 end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Feb 2022 01:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-code-with-date-as-an-array/m-p/794904#M254916</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-08T01:31:25Z</dc:date>
    </item>
  </channel>
</rss>

