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   "Metcalfe, Paul" <[email protected]>
To   <[email protected]>
Subject   RE: st: combinations of while, if, by, and foreach commands
Date   Fri, 19 Sep 2003 09:55:47 +0100

Many thanks to both Gary and Nick for their help.  I used Gary's suggestion and it worked perfectly.

Paul


-----Original Message-----
From: Nick Cox [mailto:[email protected]]
Sent: Friday, September 19, 2003 12:49 AM
To: [email protected]
Subject: RE: st: combinations of while, if, by, and foreach commands


Gary Longton
> 
> 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

Yes, it's pretty clear I misread the problem! 
I suppose my excuse is that I didn't imagine that 
blocks of missings at the beginning could be of 
use or interest. 

Nick 
[email protected] 
*
*   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