I have the following code that I want to be able to run as an array (or a macro) within a larger datastep.
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.
As you can see I have the condition that if the admission date is <= Dec 31, 2019 and the discharge date is >= Jan 1 2019 then create caldate and rcaldate.
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 rcaldate7, rcaldate18, rcaldate19 and rcaldate20 . Instead of copy and pasting this code four times, how can I create an array (or a macro) for the below code?
if (admit_dt<='31dec2019'd and disch_dt>='01jan2019'd ) then do;
if admit_dt<'01jan2019'd then caldate19='01jan2019'd;
else caldate19=admit_dt;
if dshy = . or disch_dt>='31dec2019'd then rcaldate19='31dec2019'd;
else if dshy ne . and disch_dt=< '31dec2019'd then rcaldate19= srdate;
end;Thank you!
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<=end_date and disch_dt>= start_date ) then do;
if admit_dt<start_date then _caldate(Year)=start_date;
else _caldate(Year)=admit_dt;
if dshy = . or disch_dt>=end_date then _rcaldate(year)=end_date;
else if dshy ne . and disch_dt=< end_date then _rcaldate(Year)= srdate;
end;
end;
@sas_student1 wrote:
I have the following code that I want to be able to run as an array (or a macro) within a larger datastep.
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.
As you can see I have the condition that if the admission date is <= Dec 31, 2019 and the discharge date is >= Jan 1 2019 then create caldate and rcaldate.
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 rcaldate7, rcaldate18, rcaldate19 and rcaldate20 . Instead of copy and pasting this code four times, how can I create an array (or a macro) for the below code?
if (admit_dt<='31dec2019'd and disch_dt>='01jan2019'd ) then do; if admit_dt<'01jan2019'd then caldate19='01jan2019'd; else caldate19=admit_dt; if dshy = . or disch_dt>='31dec2019'd then rcaldate19='31dec2019'd; else if dshy ne . and disch_dt=< '31dec2019'd then rcaldate19= srdate; end;Thank you!
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<=end_date and disch_dt>= start_date ) then do;
if admit_dt<start_date then _caldate(Year)=start_date;
else _caldate(Year)=admit_dt;
if dshy = . or disch_dt>=end_date then _rcaldate(year)=end_date;
else if dshy ne . and disch_dt=< end_date then _rcaldate(Year)= srdate;
end;
end;
@sas_student1 wrote:
I have the following code that I want to be able to run as an array (or a macro) within a larger datastep.
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.
As you can see I have the condition that if the admission date is <= Dec 31, 2019 and the discharge date is >= Jan 1 2019 then create caldate and rcaldate.
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 rcaldate7, rcaldate18, rcaldate19 and rcaldate20 . Instead of copy and pasting this code four times, how can I create an array (or a macro) for the below code?
if (admit_dt<='31dec2019'd and disch_dt>='01jan2019'd ) then do; if admit_dt<'01jan2019'd then caldate19='01jan2019'd; else caldate19=admit_dt; if dshy = . or disch_dt>='31dec2019'd then rcaldate19='31dec2019'd; else if dshy ne . and disch_dt=< '31dec2019'd then rcaldate19= srdate; end;Thank you!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.