Dear Statalist members,

My dataset is something like this

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte(firm industry qtr) float(var1 var2) byte check
 1 1 1 .55 .28 0
 2 1 1 .55 .28 0
 3 1 1 .55 .28 0
 4 1 1   .   . 1
 5 1 1 .55 .28 0
 1 1 2 .77 .63 0
 2 1 2 .77 .63 0
 3 1 2 .77 .63 0
 4 1 2 .77 .63 0
 5 1 2 .77 .63 0
 6 1 2   .   . 1
11 2 1 .97 .13 0
12 2 1 .97 .13 0
13 2 1   .   . 1
14 2 1 .97 .13 0
15 2 1 .97 .13 0
11 2 2 .78 .44 0
12 2 2 .78 .44 0
13 2 2   .   . 1
14 2 2 .78 .44 0
15 2 2 .78 .44 0
end
I want to generate mean values for each industry and quarter. I used the following code.

gen newvar1=.
gen newvar2=.

forval q=1(1)2{

forval i=1(1)2{

egen check1= mean(var1) if `i'==industry & `q'==qtr
egen check2= mean(var2) if `i'==industry & `q'==qtr

replace newvar1=check1 if `i'==industry & `q'==qtr
replace newvar2=check2 if `i'==industry & `q'==qtr

drop check1
drop check2
}
}

BUT

My actual dataset consists of around one million observations with many firms and quarters, so it takes too much time to complete. Maybe because of egen!!

My Question:

1. How can i estimate this mean in a quicker way?
2. Another related question is that if I want to replace the missing values in var1 and var2 with previous values from same industry and quarter, how can I do that? (Missing values are the one against which the check variable is equal to 1).

Kind Regards,
Azhar Mughal