BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_student1
Quartz | Level 8

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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;
  • Declare your arrays
  • Index using years to make your life easier
  • Create start/end date separately to make the code easier to follow and more efficient

@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! 


 

View solution in original post

1 REPLY 1
Reeza
Super User
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;
  • Declare your arrays
  • Index using years to make your life easier
  • Create start/end date separately to make the code easier to follow and more efficient

@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! 


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 435 views
  • 0 likes
  • 2 in conversation