<?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: how to add dates in an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/884921#M349616</link>
    <description>&lt;P&gt;I would avoid two digit years, even in variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you want to create variables named DATE1_2018 to DATE1_2023 and DATE2_2018 to DATE2_2023?&amp;nbsp; That is easy with ARRAY statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array date1 DATE1_2018 - DATE1_2023;
array date2 DATE2_2018 - DATE2_2023;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you seem to want to loop by YEAR perhaps you should define the arrays so you can actually use the YEAR as the index.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array date1 [2018:2023] DATE1_2018 - DATE1_2023;
array date2 [2018:2023] DATE2_2018 - DATE2_2023;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just use a DO loop with YEAR&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do year=2018 to 2023;
  date1[year] = max(admit_dt,mdy(1,1,year));
  date2[year] = min(dsch_dt,mdy(12,31,year));
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might have add more logic to handle cases that do not cross the year of interest at all.&amp;nbsp; For example 01MAR2019 to 02APR2019 would only cross the year 2019 so you need to decide what values you want for 2018 and the other years.&lt;/P&gt;</description>
    <pubDate>Sat, 15 Jul 2023 20:10:37 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-07-15T20:10:37Z</dc:date>
    <item>
      <title>how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883362#M349013</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a SAS code where I am calculating dates of multiple years (between 2018 and 2023). I don't want to have to repeat the code for each year and was wondering how I could convert this into an array. Note that this isn't the full code, just part of it, I figured if folks here can assist with the smaller code I maybe able to do it for the bigger code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code where I want to run an array for each year. So 2023 would be replaced with 2018, then 2019 and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if admit_dt lt '01jan2023'd then date1='01jan2023'd ; else date1= admit_dt;

if dsch_dt eq . or dsch_dt gt '31dec2023'd then date2='31dec2023'd; 
else if dsch_dt ne . and dsch_dt le '31dec2023'd then date2= dsch_dt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Suggestions would be great!&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 22:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883362#M349013</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2023-07-03T22:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883369#M349016</link>
      <description>It's really hard to advise unless you show us a portion of the data, the desired output for the portion of the data that you have shown us, and more of the logic involving dates.&lt;BR /&gt;&lt;BR /&gt;I will state that I can't see how an array fits in here, and I would advise you to not insist on a tool like arrays, when there may be other possible solutions.</description>
      <pubDate>Tue, 04 Jul 2023 00:42:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883369#M349016</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-04T00:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883381#M349022</link>
      <description>&lt;P&gt;I don't see how arrays have anything to do with your IF/THEN logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Arrays are a tool for indexing into a list of VARIABLES.&lt;/P&gt;
&lt;P&gt;What variables would you reference with the array?&lt;/P&gt;
&lt;P&gt;Which of the hard coded variable names in your IF/THEN logic would the array reference replace?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 03:10:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883381#M349022</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-04T03:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883391#M349026</link>
      <description>&lt;P&gt;Your statements can be simplified like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date1 = max('01jan2023'd,admit_dt);
date2 = ifn(dsch_dt = .,'31dec2023'd,min('31dec2023'd,dsch_dt));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the conditions are vastly different, I see no way how the use of an array (and a loop) could help.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 08:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883391#M349026</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-04T08:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883573#M349101</link>
      <description>&lt;P&gt;Are you actually trying to get the admission and discharge dates by year, so that you have an observation for each year with a valid period?&lt;/P&gt;
&lt;P&gt;Then something like this may work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  do year=2018 to 2023;
    date1=mdy(1,1,year);
    if .&amp;lt;dsch_dt&amp;lt;date1 then leave; /* no more output for this observation */
    date2=mdy(12,31,year);
    if admit_dt&amp;gt;date2 then continue /* no output for this year */
    date1=max(date1,admit_dt);
    date2=min(dsch_dt,date2);
    output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2023 13:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/883573#M349101</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-07-05T13:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/884920#M349615</link>
      <description>&lt;P&gt;Hello. Thank you for the reply, I realize I wasn't specific enough.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create two date variables for each year parameter that I run. So in the example below I am creating date1_20 and date2_20 for the 2020 year parameter. Then I want to run the similar code but for 2021, where I am creating two new variable date1_21 and date2_20 and so on. So instead of running multiple if/then I figured there must be a way to run this as an array. I only gave examples for three years but I would want to do this for year 2018 to 2023.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/** 2020**/
if admit_dt lt '01jan2020'd then date1='01jan2020'd ; else date1_20= admit_dt;

if dsch_dt eq . or dsch_dt gt '31dec2020'd then date2_20='31dec2020'd; 
else if dsch_dt ne . and dsch_dt le '31dec2020'd then date2_20= dsch_dt;

/* 2021**/
if admit_dt lt '01jan2021'd then date1='01jan2021'd ; else date1_21= admit_dt;

if dsch_dt eq . or dsch_dt gt '31dec2021'd then date2_21='31dec2021'd; 
else if dsch_dt ne . and dsch_dt le '31dec2021'd then date2_21= dsch_dt;

/* 2022**/
if admit_dt lt '01jan2022'd then date1='01jan2022'd ; else date1_22= admit_dt;

if dsch_dt eq . or dsch_dt gt '31dec2022'd then date2_22='31dec2022'd; 
else if dsch_dt ne . and dsch_dt le '31dec2022'd then date2_22= dsch_dt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Jul 2023 19:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/884920#M349615</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2023-07-15T19:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to add dates in an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/884921#M349616</link>
      <description>&lt;P&gt;I would avoid two digit years, even in variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you want to create variables named DATE1_2018 to DATE1_2023 and DATE2_2018 to DATE2_2023?&amp;nbsp; That is easy with ARRAY statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array date1 DATE1_2018 - DATE1_2023;
array date2 DATE2_2018 - DATE2_2023;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you seem to want to loop by YEAR perhaps you should define the arrays so you can actually use the YEAR as the index.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array date1 [2018:2023] DATE1_2018 - DATE1_2023;
array date2 [2018:2023] DATE2_2018 - DATE2_2023;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just use a DO loop with YEAR&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do year=2018 to 2023;
  date1[year] = max(admit_dt,mdy(1,1,year));
  date2[year] = min(dsch_dt,mdy(12,31,year));
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might have add more logic to handle cases that do not cross the year of interest at all.&amp;nbsp; For example 01MAR2019 to 02APR2019 would only cross the year 2019 so you need to decide what values you want for 2018 and the other years.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jul 2023 20:10:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-add-dates-in-an-array/m-p/884921#M349616</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-15T20:10:37Z</dc:date>
    </item>
  </channel>
</rss>

