Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: RE: Looping over observations that do not start from 1


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Looping over observations that do not start from 1
Date   Mon, 3 May 2010 17:32:43 +0100

This query provoked various correspondence with Martin Weiss. George
didn't answer Martin's questions and what he is trying to do remains
unclear to me. However, my instinct is that whatever he wants can be
done much more efficiently.
 
Various notes: 

1. The construct (e.g.) 

local i = 1
while `i' <= `years' {
	local i = `i' +1
}

is much more easily implemented as 

forval i = 1/`years' { 

} 

2. After -summarize-, r(min) and r(max) are more transparent than the
outdated _result(5) and _result(6). 

3. -egen, group()- on -year month- would create a variable starting at
1. 

Nick 
n.j.cox@durham.ac.uk 

George Chioran

I have a data set that spans from april 2008 to june 2009. I want to
loop over the the months in this data set by year. However if I group
the months, in year 2008 I have a month variable that starts from 4 and
lasts to 12.

While looping over years, I wanted to know if there is a way to loop
over the month variable even if it starts from 4.

I was thinking that this could be done if there were a command to skip a
repetition from a loop if there are no observations in the database (as
with each loop I am keeping the observations that are equal to the
current number, thus in year 2008, there are no months 1,2 and 3, so
keep if month = `i' would yield an empty table). In my case, the loop
would have to skip the first 3 repetitions and start from 4 to run my
program. 

My program is similar to this:

egen year = group(year)
su year
local years = _result(6)
local i = 1
while `i' <= `years' {
save file1, replace
keep if year == `i'
su month
local months = _result(6)
local j = 1
while `j' <= `months' {
save file2, replace
keep if month = `j'
local `j' = result from equation
use file2, clear
local j = `j' +1
}
use file1, clear
local i = `i'+1
}


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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