I need to calculate the number of business days between two dates (variables:investigationstartdate and reporteddate) . I generated a business calendar, called bizcal.stbcal and saved to my ado folder on my C drive, to exclude weekends and Canadian Statutory holidays.
The issue I'm facing is that some of the dates in the source data (investigationstartdate and reporteddate) occur on weekends/holidays, so that when I use the following code, the respective business calendar dates for these variables (variable names with the suffix_biz) are missing.
I would like to generate a loop, so that if the business dates are missing, to add +1 days to the date in the source variables, and repeat until the respective business calendar date is not missing. (i.e., finding the next available date in the business calendar).
Here is the code that I used
And here is an example of the data:
Let me know if anyone has thoughts on how to accomplish this.
The issue I'm facing is that some of the dates in the source data (investigationstartdate and reporteddate) occur on weekends/holidays, so that when I use the following code, the respective business calendar dates for these variables (variable names with the suffix_biz) are missing.
I would like to generate a loop, so that if the business dates are missing, to add +1 days to the date in the source variables, and repeat until the respective business calendar date is not missing. (i.e., finding the next available date in the business calendar).
Here is the code that I used
Code:
bcal dir
bcal load bizcal
***2.A.4.0 Generate business days version of applicable date fields
gen investigationstartdate_biz=bofd("bizcal", investigationstartdate)
format %tbbizcal:CCYY-NN-DD investigationstartdate_biz
gen reporteddate_biz=bofd("bizcal", reporteddate)
format %tbbizcal:CCYY-NN-DD reporteddate_biz
***2.A.4.0.1 Dea
***2.A.4.0 Generate date difference from investigationstartdate & reporteddate from Business Cal
gen invest_report_diff=datediff(investigationstartdate_biz, reporteddate_biz, "day")
gen problemid4 = 0
replace problemid4 = 1 if invest_report_diff>0
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long investigationid float(reporteddate investigationstartdate investigationstartdate_biz reporteddate_biz invest_report_diff) 845218 19872 19507 105 366 261 395564 19726 19666 218 262 44 720675 19731 19725 261 265 4 395535 19725 19725 261 261 0 720429 19725 19725 261 261 0 395505 19725 19725 261 261 0 720319 19736 19726 262 268 6 395536 19726 19726 262 262 0 824026 19726 19726 262 262 0 395651 19726 19729 263 262 -1 720404 19729 19729 263 263 0 823128 19729 19729 263 263 0 720392 19729 19729 263 263 0 824031 19732 19730 264 266 2 720513 19730 19730 264 264 0 395695 19730 19730 264 264 0 720696 19730 19730 264 264 0 720451 19730 19730 264 264 0 824028 19730 19730 264 264 0 720531 19730 19730 264 264 0 395719 19730 19730 264 264 0 395722 19730 19730 264 264 0 720525 19730 19730 264 264 0 721161 19736 19731 265 268 3 824040 19731 19731 265 265 0 395775 19731 19731 265 265 0 720688 19731 19731 265 265 0 721337 19736 19731 265 268 3 824043 19731 19731 265 265 0 720641 19731 19731 265 265 0 824202 19732 19732 266 266 0 720864 19732 19732 266 266 0 720807 19732 19732 266 266 0 720874 19732 19732 266 266 0 720860 19732 19732 266 266 0 720793 19731 19732 266 265 -1 720792 19732 19732 266 266 0 395819 19732 19732 266 266 0 721709 19738 19732 266 270 4 720855 19732 19732 266 266 0 720841 19732 19732 266 266 0 823931 19732 19732 266 266 0 720870 19732 19732 266 266 0 825335 19733 19733 267 267 0 824195 19733 19733 267 267 0 824285 19732 19733 267 266 -1 824189 19731 19733 267 265 -2 720911 19733 19733 267 267 0 824248 19733 19733 267 267 0 824243 19733 19733 267 267 0 824739 19733 19733 267 267 0 721004 19733 19733 267 267 0 824187 19732 19733 267 266 -1 721565 19738 19733 267 270 3 824205 19733 19733 267 267 0 721216 19733 19736 268 267 -1 721157 19736 19736 268 268 0 721270 19736 19736 268 268 0 721738 19739 19736 268 271 3 824839 19736 19736 268 268 0 721189 19736 19736 268 268 0 395913 19736 19736 268 268 0 721172 19736 19736 268 268 0 721249 19736 19736 268 268 0 721197 19736 19736 268 268 0 824841 19736 19736 268 268 0 824637 19736 19736 268 268 0 721176 19736 19736 268 268 0 721171 19736 19736 268 268 0 721192 19736 19736 268 268 0 721217 19736 19736 268 268 0 722101 19740 19736 268 272 4 721568 19738 19737 269 270 1 721425 19737 19737 269 269 0 824930 19736 19737 269 268 -1 721442 19737 19737 269 269 0 825345 19737 19737 269 269 0 395971 19737 19737 269 269 0 721568 19738 19737 269 270 1 824979 19733 19737 269 267 -2 825163 19737 19737 269 269 0 825146 19737 19737 269 269 0 825339 19737 19737 269 269 0 721430 19737 19737 269 269 0 722096 19740 19737 269 272 3 825099 19737 19737 269 269 0 722322 19737 19737 269 269 0 396161 19739 19737 269 271 2 721977 19738 19737 269 270 1 722093 19740 19737 269 272 3 721572 19738 19738 270 270 0 825411 19738 19738 270 270 0 825385 19738 19738 270 270 0 826009 19738 19738 270 270 0 721563 19738 19738 270 270 0 721563 19738 19738 270 270 0 721571 19738 19738 270 270 0 396077 19738 19738 270 270 0 721601 19738 19738 270 270 0 721619 19738 19738 270 270 0 end format %tdCCYY-NN-DD reporteddate format %tdCCYY-NN-DD investigationstartdate format %tbbizcal:CCYY-NN-DD investigationstartdate_biz format %tbbizcal:CCYY-NN-DD reporteddate_biz

Comment