Hello everyone

I'm trying to calculate the average price-to-book ratio of dividend-paying companies, using Stata version 16.
The goal is the following: for companies that pay a dividend and i have information about the price-to-book ratio, I want to calculate the average price-to-book ratio of these companies per month.
Therefore i have a dataset containing information whether the company paid dividends and another dataset containing the information about the price-to-book ratio of that company.

These are the datasets:

For the price-to-book ratio: file name: test_ptb

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int Year byte(Month Nr_289166) double(Nr_922924 Nr_982102 Nr_504689)
1999 1 . 3.22 .38 1.32
1999 2 . 3.66  .4 1.21
1999 3 . 4.05 .43 1.19
1999 4 .  4.6 .42 1.29
1999 5 . 4.47 .39 1.34
2000 1 . 6.48 .48  .71
2000 2 .  6.3 .48  .81
2000 3 . 6.78 .45  .77
2000 4 .  6.8 .45  .71
2000 5 . 7.44 .45   .8
end
for the dividend: file name: test_div

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int Year byte Month double(Nr_289166 Nr_922924) byte Nr_982102 double Nr_504689
1999 1   . .46 0 1.06
1999 2   . .46 0 1.06
1999 3   . .46 0 1.06
1999 4   . .46 0 1.06
1999 5   . .46 0 1.06
2000 1 3.5 .47 0 1.06
2000 2 3.5 .47 0 1.06
2000 3 3.5 .47 0 1.06
2000 4 3.5 .47 0 1.06
2000 5 3.5 .47 0 1.06
end

Now I would use frames for both data

frame create div
frame div: use test_div

frame create ptb
frame ptb: use test_ptb

I thought about creating a third frame, containing the values of ptb if in the file div the value = 1.

What I have tried:

frame change div
foreach i of varlist(Nr_289166-Nr_504689){
if `i'== 1 {
frame put Nr_289166-Nr_504689, into(new)
save new
}

I know that somehow the identification that year == year and month == month is missing, but i don't know how to include it in the command.

Thank you very much for your help!