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!
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?
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;
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.
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.
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;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.