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

re: st: foreach loop over a column

From   Kit Baum <>
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   |
An Introduction to Stata Programming |
   An Introduction to Modern Econometrics Using Stata  |

*   For searches and help try:

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