Bookmark and Share

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: AW: Looping over observations that do not start from 1


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: Looping over observations that do not start from 1
Date   Sun, 2 May 2010 17:38:13 +0200

<> 

What kind of results specific to months are you trying to obtain? Several
Stata commands come to mind when looking at your code; among them:
-preserve/restore-, -collapse- and -capture-, with the last one being able
to keep a do-file running even in case of an error. An excerpt from your
dataset would be helpful, I think.



HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von George Chioran
Gesendet: Sonntag, 2. Mai 2010 17:32
An: [email protected]
Betreff: st: Looping over observations that do not start from 1


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/


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index