Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Looping over observations that do not start from 1
From
George Chioran <[email protected]>
To
[email protected]
Subject
st: Looping over observations that do not start from 1
Date
Sun, 2 May 2010 08:32:00 -0700 (PDT)
Dear Statalisters,
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
}
Thank you a lot for the help,
George
*
* 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/