Hello Everyone,

I am new to STATA and would really appreciate your help with a problem I am stuck on. I have a dataset A which looks like this:
id date_time f_id
1 14jan2020 06:34:43 other
1 14jan2020 18:11:13 B
1 14jan2020 20:32:56 C
1 17jan2020 13:45:29 exit

I have a dataset B which looks like this:
date hour f_id weight
08 Jan 20 0 A 0.94
08 Jan 20 1 A 0.92
08 Jan 20 2 A 0.91
.
.
.
.
08 Jan 20 23 A 0.94
08 Jan 20 0 B 0.87
08 Jan 20 1 B 0.89
.
.
08 Jan 20 23 B 0.78
(and so I have weights for each hour of each f_id for each day in that year)

What I want to accomplish is that for every id in dataset A, I want to calculate a variable weighted_sum which for id 1 would look like 49(mins spent in that hour)*(weight for the 18th hour of f_id B on 14th jan 2020) + 60*(weight for the 19th hour of f_id B on 14th jan 2020) + 32*(weight for the 20th hour of f_id B on 14th jan 2020) + 28*(weight for the 20th hour of f_id C on 14th jan 2020) + ....+ 45*(weight for the 13th hour of f_id C on 17th jan 2020).

I am confused about how I should loop to subtract times, get hours, multiply appropriate hourly weights, and adding them. Any help would be greatly appreciated!! Thanks!!

Best,
Joe