Code:
input str1 firm  int year float(profits)
  
   "a"  2000 .
   "a"  2001 .
   "a"  2002 .
   "a" 2003 5
   "a"  2004 10 
   "a" 2005 15 
   "b"  2000 20 
   "b"  2001 25 
   "b"  2002 30 
   "b"  2003 35 
   "b"  2004 40 
   "b"  2005 45 
   "c"  2000 .
   "c"  2001 . 
   "c"  2002 5 
   "c"  2003 15
  "c" 2004 . 
   "c"  2005 .
   end
Based on the above sample set, I would like to classify firms into 2 groups, Group1 and Group2 based on 2 time periods. Period 1 denotes pre-regulation(years<2003; 2000, 2001 &2002) and post-regulation (>2002;2003,2004&2005). Group 1 comprises of those firms that reported profits at least 2 years in the pre-regulation period. Based on the example dataset, during the pre-regulation period(2000,2001,2002), firm "a" didn't report any profits, "b" reported profits during the 3 years, & "c" reported profits, but only once. Hence only "b" is the suitable candidate for Group1. Group2 comprises those firms which reported profits during the post-regulation period at least 2 times but not in the pre-regulation period.
"a" is the suitable one, since "b" reported profits at least 2 times in the pre-reg period it cannot be considered. For "c", it failed to report profits during the post-reg period at least 2 times, hence it must not be considered.

Code:
*I tried various commands that I collated from previous posts in stata 
encode firm,gen(id)
xtset id year
gen regulation=.
replace reg= 1 if year <2003
replace reg =2 if year>2002
egen dummy1=count(profits) if year<2003,by(id ) // for counting profits by id during the pre_reg
gen indicator = dummy1 > 2 & !missing(dummy1)  // to specify those firms which reported profits greater than 2 times
egen dummy2=count(profits) if year>2002,by(id ) // for counting profits by id during the post_reg
gen indicator2 = dummy2 > 2 & !missing(dummy2)  // to specify those firms which reported profits greater than 2 times 
gen mand_firm=1 if indicator==0 & indicator2==1 // for mandatory firms 
*in the above code, both "a" & "b" will be given the dummy "1" since this command can't exclude "b"
What should be the correct command to pull out those required firms?