I have a hospital record dataset with data on (i) the diagnosis of the patient, (ii) the procedure performed, (iii) the year/month in which the procedure has been performed as well as (iv) the year/month in which such procedure was made possible for that given diagnosis and (v) the final year/month for which the procedure was available for that diagnosis. For instance, a procedure that was available from Jan/2008 until Jun/2012 and was performed in Feb/2012 would have Feb/2012 as (iii), Jan/2008 as (iv) and Jun/2012 as (v). Basically, (iii) will always be in between (iv) and (v). The months (iv) and (v) are uniquely identified by the code of the procedure and the diagnosis for which it is being performed. Names of variables are: (i) cid_pri, (ii) proc, (iii) int_ini, (iv) proc_ini, (v) proc_fim.

Below is a subsample of the dataset for two different diagnosis.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str4 cid_pri long proc float(int_ini proc_ini proc_fim)
"G573" 301060070 607 576 683
"G573" 301060070 659 576 683
"G573" 301060070 659 576 683
"G573" 301060088 658 576 683
"G573" 301060088 674 576 683
"G573" 403020050 677 576 683
"G573" 403020077 660 576 683
"G573" 408050390 577 576 683
"G573" 408050390 577 576 683
"G573" 408050390 582 576 683
"G573" 408050390 583 576 683
"G573" 408050390 592 576 683
"G573" 408050390 601 576 683
"G573" 408050390 611 576 683
"G573" 408050390 613 576 683
"G573" 408050390 617 576 683
"G573" 408050390 619 576 683
"G573" 408050390 623 576 683
"G573" 408050390 624 576 683
"G573" 408050390 630 576 683
"G573" 408050390 632 576 683
"G573" 408050390 638 576 683
"G573" 408050390 639 576 683
"G573" 408050390 642 576 683
"G573" 408050390 643 576 683
"G573" 408050390 644 576 683
"G573" 408050390 649 576 683
"G573" 408050390 652 576 683
"G573" 408050390 657 576 683
"G573" 408050390 659 576 683
"G573" 408050390 661 576 683
"G573" 408050390 666 576 683
"G573" 408050390 676 576 683
"G573" 408050390 677 576 683
"G573" 408050390 679 576 683
"G573" 408050390 680 576 683
"G573" 408050390 680 576 683
"G573" 408060549 583 576 683
"G573" 408060549 594 576 683
"G573" 408060549 619 576 683
"G573" 408060549 630 576 683
"G573" 408060549 634 576 683
"G573" 408060549 639 576 683
"G573" 408060549 642 576 683
"G573" 408060549 646 576 683
"G573" 408060549 649 576 683
"G573" 408060549 652 576 683
"G573" 415010012 596 576 683
"G573" 415020034 635 586 683
"G573" 415030013 671 576 683
"G900" 301060010 651 576 683
"G900" 301060010 652 576 683
"G900" 301060010 677 576 683
"G900" 301060010 680 576 683
"G900" 301060010 682 576 683
"G900" 301060070 606 576 683
"G900" 301060070 609 576 683
"G900" 301060070 616 576 683
"G900" 301060070 666 576 683
"G900" 301060088 583 576 683
"G900" 301060088 585 576 683
"G900" 301060088 589 576 683
"G900" 301060088 605 576 683
"G900" 301060088 606 576 683
"G900" 301060088 610 576 683
"G900" 301060088 614 576 683
"G900" 301060088 615 576 683
"G900" 301060088 617 576 683
"G900" 301060088 620 576 683
"G900" 301060088 627 576 683
"G900" 301060088 630 576 683
"G900" 301060088 632 576 683
"G900" 301060088 636 576 683
"G900" 301060088 641 576 683
"G900" 301060088 645 576 683
"G900" 301060088 647 576 683
"G900" 301060088 648 576 683
"G900" 301060088 655 576 683
"G900" 301060088 657 576 683
"G900" 301060088 662 576 683
"G900" 301060088 666 576 683
"G900" 301060088 670 576 683
"G900" 301060088 671 576 683
"G900" 301060088 678 576 683
"G900" 303040130 679 576 683
"G900" 303040149 664 576 683
"G900" 303040149 670 576 683
"G900" 303040190 670 576 683
"G900" 303110023 660 576 683
"G900" 303130059 666 576 683
"G900" 304100013 681 576 683
"G900" 304100021 668 609 683
"G900" 407040161 631 576 683
"G900" 414020413 611 605 683
"G900" 414020413 639 605 683
"G900" 414020413 645 605 683
"G900" 414020413 652 605 683
"G900" 414020413 656 605 683
"G900" 414020413 667 605 683
"G900" 415020077 666 650 683
end
format %tm int_ini
format %tm proc_ini
format %tm proc_fim
I would like to compute the share of hospitalizations that was performed with each procedure for a given diagnosis while such procedure was available for such diagnosis.

The numerator is easy to compute (see code below). I am having a hard time computing the denominator. Basically, I need to write a command that counts the number of hospitalizations for each diagnosis (variable cid_pri) which occurred in a year/month (variable int_ini) that is in between the time period for which that performed procedure was available (proc_ini and proc_fim). For instance, in the first row we should have the number of all hospitalizations in the dataset for which cid_pri=="G573" and int_ini is in between Jan/2008 and Dec/2016 (once this is the period for which the procedure in row 1 is available in the hospital).

Code:
bys cid_pri proc: egen nobs = count(proc)
Many thanks!