Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Programming question


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: Programming question
Date   Fri, 4 Mar 2005 17:13:45 -0000

How about...

summ id, meanonly
forvalues i = 1/`r(max)' {
   qui count if id==`i'
   forvalues j = 1/`r(N)' {
     <do stuff>
   }
}

David

-----Original Message-----
From: Cameron Hooper [mailto:[email protected]]
Sent: 04 March 2005 17:01
To: [email protected]
Subject: st: Programming question


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


*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index