Quartz | Level 8

## how to add dates in an array

Hello,

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.

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.

``````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;``````

Suggestions would be great!

Thanks!

6 REPLIES 6
Diamond | Level 26

## Re: how to add dates in an array

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.

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.
--
Paige Miller
Super User

## Re: how to add dates in an array

I don't see how arrays have anything to do with your IF/THEN logic.

Arrays are a tool for indexing into a list of VARIABLES.

What variables would you reference with the array?

Which of the hard coded variable names in your IF/THEN logic would the array reference replace?

Quartz | Level 8

## Re: how to add dates in an array

Hello. Thank you for the reply, I realize I wasn't specific enough.

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.

``````/** 2020**/

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 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 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;``````
Super User

## Re: how to add dates in an array

I would avoid two digit years, even in variable names.

So you want to create variables named DATE1_2018 to DATE1_2023 and DATE2_2018 to DATE2_2023?  That is easy with ARRAY statements:

``````array date1 DATE1_2018 - DATE1_2023;
array date2 DATE2_2018 - DATE2_2023;``````

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.

``````array date1 [2018:2023] DATE1_2018 - DATE1_2023;
array date2 [2018:2023] DATE2_2018 - DATE2_2023;``````

Now just use a DO loop with YEAR

``````do year=2018 to 2023;
date2[year] = min(dsch_dt,mdy(12,31,year));
end;``````

You might have add more logic to handle cases that do not cross the year of interest at all.  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.

Super User

## Re: how to add dates in an array

Your statements can be simplified like this:

``````date1 = max('01jan2023'd,admit_dt);
date2 = ifn(dsch_dt = .,'31dec2023'd,min('31dec2023'd,dsch_dt));
``````

Since the conditions are vastly different, I see no way how the use of an array (and a loop) could help.

Meteorite | Level 14

## Re: how to add dates in an array

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?

Then something like this may work:

``````data want;
set have;
do year=2018 to 2023;
date1=mdy(1,1,year);
if .<dsch_dt<date1 then leave; /* no more output for this observation */
date2=mdy(12,31,year);
if admit_dt>date2 then continue /* no output for this year */