----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str2 podn float(ladn uw)
"AL" 137  .6530787
"AL" 133 .09962858
"AL" 133  .7349375
"AL" 137  .3416429
"AL" 137  .7651829
"B"  282  .9254544
"B"  282  .3284793
"B"  282   .601528
"B"  282 .05638361
"B"  282  .9922161
"B"  282  .7884781
"B"  282 .19412884
"B"  282 .05713714
"B"  282  .4019801
"B"  282 .10262794
"B"  286  .9294506
"B"  282  .7320663
"B"  282  .3816186
"B"  252  .8174805
"B"  282 .22413313
"B"  252   .489517
"B"  284  .8588205
"B"  285  .6861753
"B"  285  .4354155
"B"  285  .9446495
"B"  287  .4540383
"B"  282  .7198554
"B"  221  .6386167
"B"  282  .6791545
"B"  286 .26081806
"B"  286  .6180484
"B"  243 .58136946
"B"  254  .6579786
"BA"  22 .13102111
"BA" 209  .7721775
"BA"  54 .51663834
"BA"  54  .6716543
"BA"  54  .4881878
"BA"  22  .7893392
"BA" 211  .3827247
"BA" 211  .4432041
"BA" 209  .8036636
"BA" 209 .04741055
"BA" 211  .4632615
"BB"   8   .655027
"BB" 150 .32793915
"BB" 155 .23481174
"BB"   8  .2788624
"BB" 158  .6979516
"BB" 153  .4087807
"BB" 157  .5190807
"BB" 155  .8759601
"BB" 155  .1548233
"BD" 291  .2143494
"BD" 289  .9403666
"BD" 289  .8223539
"BD" 289 .11206491
"BD" 289  .3476477
"BD" 190 .06617011
"BD" 289  .8340919
"BD" 190  .5662497
"BD" 289  .8524801
"BD" 289    .79358
"BD" 289  .4145629
"BD" 289  .7217242
"BH"  28  .3480542
"BH"  29  .9046446
"BH"  91  .6097409
"BH"  91   .741581
"BH"  89  .3017222
"BH"  89  .4698355
"BH"  88  .7786833
"BH" 124  .6399533
"BH"  28  .3890726
"BH"  28 .40755025
"BL" 259 .04850375
"BL" 258  .3796008
"BL" 259 .07654884
"BL" 258  .9332509
"BL" 258  .9499664
"BL"   8 .19072783
"BL" 259 .21267034
"BN"  43 .15388475
"BN"  96  .7903761
"BN" 251  .7564719
"BN" 251  .6596532
"BN" 245  .4974095
"BN" 246  .6767726
"BN"  43 .05740402
"BN"  94 .15620527
"BN"  94   .741659
"BN"  94  .7825961
"BN"  98  .3825884
"BN"  98 .20780987
"BN"  43  .9423695
"BN" 245  .6629013
"BN"  96  .7783902
"BN"  96  .9061702
"BR" 299  .6617007
"BR" 299  .0472592
end
In my data ladn is nested within podn and I want to calculate the average of uw for each value of ladn across observations in that podn excluding those in that ladn. So, currently I am using code like this:

Code:
cap drop meanUW
gen meanUW=.
levelsof podn, local(pod)
foreach p of local pod {
    levelsof ladn if podn=="`p'" , local(ld)
    foreach l of local ld { 
        cap drop temp
        egen temp=mean(uw) if podn=="`p'" & ladn!=`l'
        replace meanUW=temp if podn=="`p'" & ladn!=`l'
        }
    }
But, as suggested in the manual for RANGESTAT this method is very slow indeed, and infeasibly so given that I have several million observations and will need to do this multiple times. I have tried to use RANGESTAT to do this, following the examples in the help files, but I think that because I want to exclude a group rather than an observation I am having problems. I tried this:

rangestat (mean) uw, interval(ladn 0 0) by(podn) excludeself

But, this doesn't give me the average by ladn but by observation. I would be grateful for any suggestions on how to use RANGESTAT or anything else to achieve a faster solution.

Best wishes,

Stu