# st: RE: [Re: How to program a loop to calculate the value of an observation]

 From "Nick Cox" To Subject st: RE: [Re: How to program a loop to calculate the value of an observation] Date Sun, 4 Feb 2007 17:26:51 -0000

```A visceral reaction is that this still looks much more
complicated than it need be. And the speed sounds horrendous
for what is presumably a descriptive statistic. Loosely similar indices tend
to take perhaps 3-7 lines of Stata code and to take somewhere between
a blink and a wink in terms of time.

No one has suggested an alternative, for which there could be
several explanations. I don't feel strong enough to try to reverse
engineer the recipe from your code, and a quick Google did not reveal
a single obvious source for this index. If you can give
a precise definition or a precise reference for such, some Stata programmers
_may_ be better equipped to advise.

Nick
n.j.cox@durham.ac.uk

Andreas Reinstaller
>
> I have found a solution that does not strike me to be particularly
> elegant, but that works. I just use the indices of the variables in
> Stata, where NoEnt_nsc1 TO_nsc1 TO_Nace NoEmpl_nsc1 LET are the
> variables I use:
>
> In a simple do file I have this code
> --------------------------------------------------begin calc
> ----------------------------------
> local size=_N
>
> /* Ave_HHI nsc1 */
>
> sort country time SecCode sizeclass
> generate ntx=_n
> generate newntx=.
> local i = 1
> forvalues i = 1(1)`size' {
>
>     local nof = NoEnt_nsc1[`i']
>     if `nof'==. local nof = 0
>     local szs = 0
>
>     if `nof' > 0 {
>         if `nof' == 1 {
>         local szs =
> (NoEnt_nsc1[`i']*(100*(TO_nsc1[`i']/NoEnt_nsc1[`i'])/TO_Nace[`
> i'] )^2)
>         }
>         else{
>             local j = 0
>             forvalues j = 1(1)`nof' {
>                 local sz = 0
>                 local sz =
> ((100*((TO_nsc1[`i']/NoEmpl_nsc1[`i']*LET[`i'])+(2*`j'*(TO_nsc1[`i']
> -(NoEnt_nsc1[`i']*(TO_nsc1[`i']/NoEmpl_nsc1[`i']*LET[`i'])))/(
> NoEnt_nsc1[`i']*(NoEnt_nsc1[`i']-1))))/
> TO_Nace[`i'])^2)
>                 local szs = `szs' + `sz'
>             }
>         }
>     }
>     qui by country time SecCode sizeclass: replace newntx = `szs' if
> ntx==`i'
> }
>
> by country time SecCode: egen Ave_HHI_nsc1=sum(newntx)
>
> ---------------------------------------------------------------- end
> calc --------------------------------------------
>
> For 12000 obs it takes about 15 minutes to run on a dual core pentium
> with 2.8ghz and Windows XP in Stata 8.2.
>
> If somebody has ideas about how to improve the speed, I
> should be grateful.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```