BookmarkSubscribeRSS Feed
sas_student1
Quartz | Level 8

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
PaigeMiller
Diamond | Level 26
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
Tom
Super User Tom
Super User

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?

sas_student1
Quartz | Level 8

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

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;
  date1[year] = max(admit_dt,mdy(1,1,year));
  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.

Kurt_Bremser
Super User

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.

s_lassen
Meteorite | Level 14

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 */
    date1=max(date1,admit_dt);
    date2=min(dsch_dt,date2);
    output;
    end;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1262 views
  • 3 likes
  • 5 in conversation