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]

Re: st: AW: Looping over observations that do not start from 1


From   George Chioran <george_chioran@yahoo.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: AW: Looping over observations that do not start from 1
Date   Sun, 2 May 2010 08:54:07 -0700 (PDT)

my database looks like this: I want to loop over the month variable from year 2008, however the month in 2008 only starts from april. Thus I wanted to loop over it with a syntax similar to:

local j = 1 
while `j' <= 12 {
...
}
and my j to start from 4 somehow, without me naming it. I think I could do this by saying 
su month 
local j =  _result(5) 

and in 2009, this would start from 1. 
Yes, I think this would work. Thank you for the help. 

datefondsnaampricemonthyear
09apr2008Philips, 24.72542008
17apr2008KPN, 11.4242008
04apr2008TomTom21.54742008
08apr2008TomTom18.82842008
08apr2008TomTom18.82842008
08apr2008TomTom18.82842008
08apr2008TomTom18.82842008
08apr2008TomTom18.82842008
09apr2008TomTom18.3942008
09apr2008TomTom18.3942008
10apr2008TomTom17.69542008
10apr2008TomTom17.69542008
10apr2008TomTom17.69542008
10apr2008TomTom17.69542008
16apr2008TomTom17.45642008
16apr2008TomTom17.45642008
16apr2008TomTom17.45642008
17apr2008TomTom17.28242008
17apr2008TomTom17.28242008
17apr2008TomTom17.28242008
21apr2008TomTom17.06742008
21apr2008TomTom17.06742008
21apr2008TomTom17.06742008
21apr2008TomTom17.06742008



----- Original Message ----
From: Martin Weiss <martin.weiss1@gmx.de>
To: statalist@hsphsun2.harvard.edu
Sent: Sun, May 2, 2010 5:38:13 PM
Subject: st: AW: Looping over observations that do not start from  1


<> 

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: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von George Chioran
Gesendet: Sonntag, 2. Mai 2010 17:32
An: statalist@hsphsun2.harvard.edu
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/



      

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