Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: st: foreach loop over a column


From   Kit Baum <[email protected]>
To   [email protected]
Subject   re: st: foreach loop over a column
Date   Tue, 13 Oct 2009 20:22:54 -0400

<>
Kushan said

I have a column name permno in my dataset. It is a numerical column,
and I want to loop over it. This is what I did:

foreach num in permno {
       // do something
}

Martin explained why it didn't work, and suggested using -levelsof-. But under the assumption these are company permnos from CRSP, for instance, each observation has a unique value; they're just not consecutive integers.

If you want to do something to every observation, realize that Stata is not SAS. Any expression you give in a -generate- (or -replace-) statement will automatically apply to each observation. If you have a variable totassets, for instance,

gen lta = log (totassets)

will create the value log total assets for every observation. There is no need to loop over observations, and in many cases, it is a bad idea to do so. The principle is that commands operate on the whole current dataset unless modified by -in range- or -if exp-.

Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
An Introduction to Stata Programming | http://www.stata-press.com/books/isp.html
   An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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