[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: forvalues and scalar: easy question |

Date |
Thu, 27 May 2004 12:07:51 +0100 |

How to do this was mentioned in passing in my posting "counting unique values across a set of variables" on Tuesday. You don't need a scalar here, or a prior -generate-, nor a prior -summarize-. You can do it directly with forval i = 1/`=_N' { Please see that posting for more explanation, and embedding in a real example. Your approach is correct in principle, except for trying to refer to the scalar m as if it were in a local macro m. So a first fix would be gen obs = _n sum obs local m = r(max) forval i = 1/`m' { Because small or occasionally large problems with various details here could arise in other contexts, I will spell out various issues. 1. -gen obs = _n- is a useful idiom for many problems. In really big datasets, you would be better off specifying a -long-: gen long obs = _n 2. -sum obs- will take up more effort than is needed just to get a maximum. Use the -meanonly- option (which despite its name yields the maximum as well). 3. We could pick up the result of the -summarize- directly. So your approach could be condensed to gen long obs = _n su obs, meanonly forval i = 1/`r(max)' { However, as said, I advise just using _N directly. This still leaves open the question of whether you really need a loop over observations. Most of the time, you don't. Nick n.j.cox@durham.ac.uk Justina.Fischer@unisg.ch since I have already disturbed a colleague who could not help me out I am going to confront you with my small problem: I want Stata to run a loop from 1 to the maximum of observations in my dataset. I tried: gen obs2 = _n sum obs2 scalar m = r(max) forvalues i=1/`m'{ .... } Which did not work. There is probably a very easy way to do that but I just do not know it. * * 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/

**Follow-Ups**:**Re: st: RE: forvalues and scalar: easy question***From:*Justina.Fischer@unisg.ch

- Prev by Date:
**st: Error with GLLAMM; Newton-Raphson** - Next by Date:
**Re: st: forvalues and scalar: easy question** - Previous by thread:
**st: Error with GLLAMM; Newton-Raphson** - Next by thread:
**Re: st: RE: forvalues and scalar: easy question** - Index(es):

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