Hi everyone,
this question might be asked few times, but I did not find a solution online. I have a dataset 1, which has a date variable including business days (Mondays - Fridays) and weekends.

and dataset 2, which has a date variable including only business days.

I would like to merge dataset1 and dataset2 by id and date. Since dataset1's date has weekends, whenever it is the weekends, match it with last Friday or next Monday (or even the best, always the last Friday) from dataset2's date. So the merged dateset looks like this:

The original data has more than 1 million observations. Thank you very much for your help in advance.
this question might be asked few times, but I did not find a solution online. I have a dataset 1, which has a date variable including business days (Mondays - Fridays) and weekends.
and dataset 2, which has a date variable including only business days.
I would like to merge dataset1 and dataset2 by id and date. Since dataset1's date has weekends, whenever it is the weekends, match it with last Friday or next Monday (or even the best, always the last Friday) from dataset2's date. So the merged dateset looks like this:
The original data has more than 1 million observations. Thank you very much for your help in advance.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str9 person str12 id float date str1 index "XXX" "A" 19731 "1" "XXX" "A" 19732 "2" "XXX" "A" 19733 "3" "XXX" "A" 19734 "2" "XXX" "A" 19735 "4" "XXX" "A" 19736 "3" "XXX" "A" 19737 "4" "XXX" "A" 19738 "9" "XXX" "A" 19739 "2" "XXX" "B" 19740 "3" "XXX" "B" 19741 "2" "XXX" "B" 19742 "4" "XXX" "B" 19743 "6" "XXX" "B" 19744 "4" "XXX" "B" 19745 "6" end format %td date save "data1.dta", replace
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str12 id int date double price "A" 19724 1 "A" 19725 2 "A" 19726 3 "A" 19729 2 "A" 19730 4 "A" 19731 2 "A" 19732 4 "A" 19733 2 "A" 19736 3 "A" 19737 4 "A" 19738 4 "B" 19739 5 "B" 19740 5 "B" 19743 5 "B" 19744 5 "B" 19745 2 "B" 19746 0 "B" 19747 2 "B" 19750 4 "B" 19751 5 "B" 19752 4 "B" 19753 3 end format %td date
Comment