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, is already up and running.

[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" <>
To   <>
Subject   st: AW: Looping over observations that do not start from 1
Date   Sun, 2 May 2010 17:43:54 +0200


Btw, did you really run -egen year = group(year)- successfully? Should lead

" year already defined

in my book...

inp year
egen year=group(year)


-----Ursprüngliche Nachricht-----
[] Im Auftrag von George Chioran
Gesendet: Sonntag, 2. Mai 2010 17:32
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, 

*   For searches and help try:

*   For searches and help try:

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