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

From |
"Michael Blasnik" <michael.blasnik@verizon.net> |

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

Subject |
st: Re: Foreach when dealing with a list of observations |

Date |
Sat, 29 Mar 2008 13:59:04 -0400 |

...

First, you are going about this in a very unStatalike manner -- you do not need a loop. Second, your use of foreach is clearly not following the allowed syntax as shown in help foreach (no -if- allowed).

Here is a non-looping way of doing this, assuming that mutcode is either 0 or 1:

by rin (mutcode): gen byte evermut=(mutcode[_N]==1)

drop if evermut

which can be reduced to just 1 line if you don't want to check through to be sure that the proper observations are flagged:

by rin (mutcode): drop if mutcode[_N]==1

This approach won't work if mutcode takes on missing values. In that case, you may want to look at egen functions like max and total which could easily identify groups that meet your criteria.

Michael Blasnik

----- Original Message ----- From: "Sander" <avirulence@gmail.com>

To: <statalist@hsphsun2.harvard.edu>

Sent: Saturday, March 29, 2008 1:00 PM

Subject: st: Foreach when dealing with a list of observations

Hi, I was wondering if it's possible to go over a list of observations with the foreach command. What I mean is the following: I have a data set with persons identified by a (for the person) unique number, called rin. However, when personal characteristics of the people in the data set changed, a new record was made with the same identification number. Now the problem is, that if certain characteristics for a person have changed (which gives a mutation code of 1 (mutcode==1)), I want that person out of the data set. What I tried is the following. The list of identification numbers that I want out of the dataset can be obtained by this command: list rin if mutcode==1 However, I don't want to run "drop if rin==number" by hand, since there are many observations. Therefore, I tried the following: foreach x of l rin if mutcode==1 { drop if rin=="`x'" } Which doesn't work, unfortunately. I tried some other commands as well: foreach x of rin if mutcode==1 { drop if rin=="`x'" } and foreach x in l rin if mutcode==1 { drop if rin=="`x'" } Which also don't work. I'm probably doing something very wrong here, but I would really appreciate it if someone could take the time to help me out with this. Thanks, Sander

* * 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: Foreach when dealing with a list of observations***From:*Sander <avirulence@gmail.com>

**References**:**st: Foreach when dealing with a list of observations***From:*Sander <avirulence@gmail.com>

- Prev by Date:
**Re: st: Error using -prvalue- in Stata 10 (unrecognized command: _pebase)** - Next by Date:
**st: re: error using prvalue** - Previous by thread:
**st: Foreach when dealing with a list of observations** - Next by thread:
**Re: st: Re: Foreach when dealing with a list of observations** - Index(es):

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