Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: combinations of while, if, by, and foreach commands


From   Gary Longton <[email protected]>
To   [email protected]
Subject   Re: st: combinations of while, if, by, and foreach commands
Date   Thu, 18 Sep 2003 16:13:15 -0700

Metcalfe, Paul wrote:

Using Stata 8.1 SE, I'm trying to put together a loop for what I imagine should be quite a straightforward task.
The relevant part of my data looks like the following:

id cons time
5001574 32
5001574 31
5001574 0.278548 30
5001574 0.271683 29
5001574 0.378903 28
5001574 0.291933 27
5001574 0.319807 26
5001574 25
5001574 24
5001574 23
5001574 22
5001574 21
5001574 0.348804 20
5001574 0.247645 19
5001574 0.306516 18
5001574 0.303717 17
5001574 0.310532 16

I have about 8000 different id values in the full dataset, observed for different stretches of time with different numbers of gaps in the cons variable in different places across the set of ids.
What I would like to do is drop the observations at the end of the time series where cons=., but keep the observations in the middle. There are varying numbers of gaps in the cons time series for different ids, and I want to keep all of them except the observations at the end of the time series for each id. I've tried a number of different combinations of the while, if and foreach commands, but none of them has worked, so I hoped that someone on the list could help.
I think this can be done without looping.  -bysort- is your friend here.

    gen t_neg = -time
    bysort id (t_neg) : drop if sum(cons ~= .) == 0

- Gary




*
*   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