Statalist


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

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


From   Sander <[email protected]>
To   [email protected]
Subject   Re: st: Re: Foreach when dealing with a list of observations
Date   Mon, 31 Mar 2008 11:04:48 +0200

Thank you. Mutcode can take on many values, but because you pointed me
in the right direction, I got it to work now. I am new to Stata and I
tried to take the approach I would take when programming. Thanks
again.

Sander

On Sat, Mar 29, 2008 at 7:59 PM, Michael Blasnik
<[email protected]> wrote:
> ...
>
>  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" <[email protected]>
>  To: <[email protected]>
>  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/
>
*
*   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