# Re: st: Programming question

 From David Kantor To statalist@hsphsun2.harvard.edu Subject Re: st: Programming question Date Fri, 04 Mar 2005 12:29:35 -0500

```At 12:00 PM 3/4/2005 -0500, Cameron Hooper wrote:
```
A novice programming question. Say I have the following data

id x
1 1
1 2
1 4
2 2
2 8
2 6
2 2

I want to write a program to loop through each company (represented by id) and then within each company loop through each observation. Something like:

forvalues i = 1(1)<number of companies> {
for values j = 1(1)<number of observations for company `i'> {
<do stuff>
}
}

To find the number of companies in the sample I've used:

tempvar numco
egen `numco '= max(id)
local nc = `numco'[1]

This is cumbersome, but it (seems) to work. I have not been able to find the <number of observations for company `i'>. I've tried things like

tempvar nobs
by id, sort: generate `nobs' = _N

Placed outside the loop this gets me part of the way there, but I don't know how to use this information to control the inner loop. I'm sure I'm missing something obvious. Thanks.

Cameron
If you tell us more about what you want to do, then we may find that you really don't need to do all that explicit looping. Experience has shown that it is very rare that you really need such looping.

Putting that aside, you should note that your command,
egen `numco' = max(id)

is valid only if your id values are a contiguous sequence of integers from 1 to whatever. If there are gaps, then it is invalid. (Suppose the values were 1,2, and 4; then `numco' would be 4, but there are 3 distinct values.) There are ways to get the number of distinct values, if you want, but it may be awkward to go from a counting index to the corresponding value in the generalized situation.

by id, sort: generate `nobs' = _N
looks fine. And its result can be referenced within the inner loop if desired, but again, tell us what you are trying to do. There may be an easier solution.

(Note that your nested loop is equivalent to a single loop through all observations, but with some consideration of the changes from one id value to another.)

-- David

David Kantor
Institute for Policy Studies
Johns Hopkins University
dkantor@jhu.edu
410-516-5404
*
* 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/