st: Running a foreach loop over a sequence of dates

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

st: Running a foreach loop over a sequence of dates

Augusto Cadenas
Hello,

I am trying to run a series of commands in a time series setting, and
interested in using variable time intervals (variable time windows, or
variable start/end dates). So ideally, I would like to loop with
-foreach- over a series of dates, but I cannot find the way of doing
this. To give you an idea of the kind of problem, here it is in
pseudo-code (not working):

*** begin pseudo-code

foreach t in 1992q1/2000q4 {
        reg y L.y L.x if tin(1960q1, `t')
        reg y L.y L.x if tin(`=`t'-20', `t')
}

*** end pseudo-code

I am sorry if this is a trivial question, but I really could not find
a way of performing this. Thanks for any hints.

AC
*
*   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/
Reply | Threaded
Open this post in threaded view
|

st: Re: Running a foreach loop over a sequence of dates

Martin Weiss-5
You cannot simply abbreviate your list of end dates like a -numlist- with a
slash and hope for Stata`s acquiescence... I would recommend you look at -h
rolling-

HTH
Martin
_______________________
----- Original Message -----
From: "Augusto Cadenas" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, December 03, 2008 10:02 PM
Subject: st: Running a foreach loop over a sequence of dates


> Hello,
>
> I am trying to run a series of commands in a time series setting, and
> interested in using variable time intervals (variable time windows, or
> variable start/end dates). So ideally, I would like to loop with
> -foreach- over a series of dates, but I cannot find the way of doing
> this. To give you an idea of the kind of problem, here it is in
> pseudo-code (not working):
>
> *** begin pseudo-code
>
> foreach t in 1992q1/2000q4 {
>        reg y L.y L.x if tin(1960q1, `t')
>        reg y L.y L.x if tin(`=`t'-20', `t')
> }
>
> *** end pseudo-code
>
> I am sorry if this is a trivial question, but I really could not find
> a way of performing this. Thanks for any hints.
>
> AC
> *
> *   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Re: Running a foreach loop over a sequence of dates

Michael Hanson
Ah, so true -- but by labeling it as "pseudo-code", Augusto indicated  
that he already knew that.  Nonetheless, looping over dates would be  
*extremely* high on my list of wishes & grumbles!  Perhaps in Stata  
11.... or even 10.2.  (Since, in the end, dates are just formatted  
numbers, this seems as if it should be possible in theory.)  There  
are applications in which something much more light-weight than -
rolling- would be appropriate.  Although I agree that the example  
below could likely be accomplished with -rolling-.

-- Mike

On Dec 3, 2008, at 4:05 PM, Martin Weiss wrote:

> You cannot simply abbreviate your list of end dates like a -
> numlist- with a slash and hope for Stata`s acquiescence... I would  
> recommend you look at -h rolling-
>
> HTH
> Martin
> _______________________
> ----- Original Message ----- From: "Augusto Cadenas"  
> <[hidden email]>
> To: <[hidden email]>
> Sent: Wednesday, December 03, 2008 10:02 PM
> Subject: st: Running a foreach loop over a sequence of dates
>
>
>> Hello,
>>
>> I am trying to run a series of commands in a time series setting, and
>> interested in using variable time intervals (variable time  
>> windows, or
>> variable start/end dates). So ideally, I would like to loop with
>> -foreach- over a series of dates, but I cannot find the way of doing
>> this. To give you an idea of the kind of problem, here it is in
>> pseudo-code (not working):
>>
>> *** begin pseudo-code
>>
>> foreach t in 1992q1/2000q4 {
>>        reg y L.y L.x if tin(1960q1, `t')
>>        reg y L.y L.x if tin(`=`t'-20', `t')
>> }
>>
>> *** end pseudo-code
>>
>> I am sorry if this is a trivial question, but I really could not find
>> a way of performing this. Thanks for any hints.
>>
>> AC
>> *
>> *   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Re: Running a foreach loop over a sequence of dates

Martin Weiss-5
BTW, Augusto did not even pretend that his was a -numlist-; if he had
thought it was one, he would have said something like "...of numlist
blabla". His example looks like "any_list" in the -h foreach- parlance. He
could of course use a loop beforehand to fill a -local- with the desired end
dates and use that in the -foreach- ...

HTH
Martin
_______________________
----- Original Message -----
From: "Michael Hanson" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, December 03, 2008 10:29 PM
Subject: Re: st: Re: Running a foreach loop over a sequence of dates


> Ah, so true -- but by labeling it as "pseudo-code", Augusto indicated
> that he already knew that.  Nonetheless, looping over dates would be
> *extremely* high on my list of wishes & grumbles!  Perhaps in Stata
> 11.... or even 10.2.  (Since, in the end, dates are just formatted
> numbers, this seems as if it should be possible in theory.)  There  are
> applications in which something much more light-weight than - rolling-
> would be appropriate.  Although I agree that the example  below could
> likely be accomplished with -rolling-.
>
> -- Mike
>
> On Dec 3, 2008, at 4:05 PM, Martin Weiss wrote:
>
>> You cannot simply abbreviate your list of end dates like a - numlist-
>> with a slash and hope for Stata`s acquiescence... I would  recommend you
>> look at -h rolling-
>>
>> HTH
>> Martin
>> _______________________
>> ----- Original Message ----- From: "Augusto Cadenas"
>> <[hidden email]>
>> To: <[hidden email]>
>> Sent: Wednesday, December 03, 2008 10:02 PM
>> Subject: st: Running a foreach loop over a sequence of dates
>>
>>
>>> Hello,
>>>
>>> I am trying to run a series of commands in a time series setting, and
>>> interested in using variable time intervals (variable time  windows, or
>>> variable start/end dates). So ideally, I would like to loop with
>>> -foreach- over a series of dates, but I cannot find the way of doing
>>> this. To give you an idea of the kind of problem, here it is in
>>> pseudo-code (not working):
>>>
>>> *** begin pseudo-code
>>>
>>> foreach t in 1992q1/2000q4 {
>>>        reg y L.y L.x if tin(1960q1, `t')
>>>        reg y L.y L.x if tin(`=`t'-20', `t')
>>> }
>>>
>>> *** end pseudo-code
>>>
>>> I am sorry if this is a trivial question, but I really could not find
>>> a way of performing this. Thanks for any hints.
>>>
>>> AC
>>> *
>>> *   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/
>


*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Re: Running a foreach loop over a sequence of dates

Augusto Cadenas
Thanks, Martin and Michael, both for your hints and for the exegetic
exercise on my words, which I found amusing.

-rolling- seems to be doing the job I want, especially if combined
with "[r]recursive". The problem I see there is that I can perform
only one command within the "frame" of this rolling window, not a
series of commands (like postestimation commands), as I was thinking
of when specifying it as a loop.

That's why I agree with Michael's points, and I find your solution,
Martin, to use a loop beforehand to fill a -local- with the desired
end dates very promising.

@ Martin: I did not really care about what kind of list, I was indeed
just trying to explain my goal in pseudo-code. So am I allowed to
rephrase my suggestion before, inventing a new type of list, call it
"timelist"? That would have been my dream (and to all German readers:
please note the "pseudo" in "pseudo-code"):

*** begin pseudo-code
foreach t of timelist 1992q1/2000q4 {
      reg y L.y L.x if tin(1960q1, `t')
}
*** end pseudo-code

Best, and thanks again! AC

On Wed, Dec 3, 2008 at 4:34 PM, Martin Weiss <[hidden email]> wrote:

> BTW, Augusto did not even pretend that his was a -numlist-; if he had
> thought it was one, he would have said something like "...of numlist
> blabla". His example looks like "any_list" in the -h foreach- parlance. He
> could of course use a loop beforehand to fill a -local- with the desired end
> dates and use that in the -foreach- ...
>
> HTH
> Martin
> _______________________
> ----- Original Message ----- From: "Michael Hanson" <[hidden email]>
> To: <[hidden email]>
> Sent: Wednesday, December 03, 2008 10:29 PM
> Subject: Re: st: Re: Running a foreach loop over a sequence of dates
>
>
>> Ah, so true -- but by labeling it as "pseudo-code", Augusto indicated that
>> he already knew that.  Nonetheless, looping over dates would be *extremely*
>> high on my list of wishes & grumbles!  Perhaps in Stata 11.... or even 10.2.
>>  (Since, in the end, dates are just formatted numbers, this seems as if it
>> should be possible in theory.)  There  are applications in which something
>> much more light-weight than - rolling- would be appropriate.  Although I
>> agree that the example  below could likely be accomplished with -rolling-.
>>
>> -- Mike
>>
>> On Dec 3, 2008, at 4:05 PM, Martin Weiss wrote:
>>
>>> You cannot simply abbreviate your list of end dates like a - numlist-
>>> with a slash and hope for Stata`s acquiescence... I would  recommend you
>>> look at -h rolling-
>>>
>>> HTH
>>> Martin
>>> _______________________
>>> ----- Original Message ----- From: "Augusto Cadenas"
>>> <[hidden email]>
>>> To: <[hidden email]>
>>> Sent: Wednesday, December 03, 2008 10:02 PM
>>> Subject: st: Running a foreach loop over a sequence of dates
>>>
>>>
>>>> Hello,
>>>>
>>>> I am trying to run a series of commands in a time series setting, and
>>>> interested in using variable time intervals (variable time  windows, or
>>>> variable start/end dates). So ideally, I would like to loop with
>>>> -foreach- over a series of dates, but I cannot find the way of doing
>>>> this. To give you an idea of the kind of problem, here it is in
>>>> pseudo-code (not working):
>>>>
>>>> *** begin pseudo-code
>>>>
>>>> foreach t in 1992q1/2000q4 {
>>>>       reg y L.y L.x if tin(1960q1, `t')
>>>>       reg y L.y L.x if tin(`=`t'-20', `t')
>>>> }
>>>>
>>>> *** end pseudo-code
>>>>
>>>> I am sorry if this is a trivial question, but I really could not find
>>>> a way of performing this. Thanks for any hints.
>>>>
>>>> AC
>>>> *
>>>> *   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/
>>
>
>
> *
> *   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Re: Running a foreach loop over a sequence of dates

Brian P. Poi
In reply to this post by Martin Weiss-5
> ----- Original Message ----- From: "Augusto Cadenas"
> <[hidden email]>
> To: <[hidden email]>
> Sent: Wednesday, December 03, 2008 10:02 PM
> Subject: st: Running a foreach loop over a sequence of dates
>
> Hello,
>
> I am trying to run a series of commands in a time series setting, and
> interested in using variable time intervals (variable time  windows, or
> variable start/end dates). So ideally, I would like to loop with
> -foreach- over a series of dates, but I cannot find the way of doing
> this. To give you an idea of the kind of problem, here it is in
> pseudo-code (not working):
>
> *** begin pseudo-code
>
> foreach t in 1992q1/2000q4 {
>      reg y L.y L.x if tin(1960q1, `t')
>      reg y L.y L.x if tin(`=`t'-20', `t')
> }

  How about this:

    forvalues t = `=q(1992q1)'/`=q(2000q4)' {
       reg y L.y L.x if date >= q(1960q1) & date <= `t'
       reg y L.y L.x if date >= `t'-20 & date <= `t'
    }


  where date is your quarterly date variable.

    -- Brian Poi
    -- [hidden email]

*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Re: Running a foreach loop over a sequence of dates

Augusto Cadenas
That's a lovely solution! Thank you very much.

AC

On Wed, Dec 3, 2008 at 5:52 PM, Brian P. Poi <[hidden email]> wrote:

>> ----- Original Message ----- From: "Augusto Cadenas"
>> <[hidden email]>
>> To: <[hidden email]>
>> Sent: Wednesday, December 03, 2008 10:02 PM
>> Subject: st: Running a foreach loop over a sequence of dates
>>
>> Hello,
>>
>> I am trying to run a series of commands in a time series setting, and
>> interested in using variable time intervals (variable time  windows, or
>> variable start/end dates). So ideally, I would like to loop with
>> -foreach- over a series of dates, but I cannot find the way of doing
>> this. To give you an idea of the kind of problem, here it is in
>> pseudo-code (not working):
>>
>> *** begin pseudo-code
>>
>> foreach t in 1992q1/2000q4 {
>>     reg y L.y L.x if tin(1960q1, `t')
>>     reg y L.y L.x if tin(`=`t'-20', `t')
>> }
>
>  How about this:
>
>   forvalues t = `=q(1992q1)'/`=q(2000q4)' {
>      reg y L.y L.x if date >= q(1960q1) & date <= `t'
>      reg y L.y L.x if date >= `t'-20 & date <= `t'
>   }
>
>
>  where date is your quarterly date variable.
>
>   -- Brian Poi
>   -- [hidden email]
>
> *
> *   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Re: Running a foreach loop over a sequence of dates

Martin Weiss-5
In reply to this post by Brian P. Poi
But note -help dates and times-

        "Historical note:  pseudofunctions ... tq(), and th() used to be
called ... q(), and h().  Those names still work but are considered
        anachronisms."


HTH
Martin
_______________________
----- Original Message -----
From: "Brian P. Poi" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, December 03, 2008 11:52 PM
Subject: Re: st: Re: Running a foreach loop over a sequence of dates


>> ----- Original Message ----- From: "Augusto Cadenas"
>> <[hidden email]>
>> To: <[hidden email]>
>> Sent: Wednesday, December 03, 2008 10:02 PM
>> Subject: st: Running a foreach loop over a sequence of dates
>>
>> Hello,
>>
>> I am trying to run a series of commands in a time series setting, and
>> interested in using variable time intervals (variable time  windows, or
>> variable start/end dates). So ideally, I would like to loop with
>> -foreach- over a series of dates, but I cannot find the way of doing
>> this. To give you an idea of the kind of problem, here it is in
>> pseudo-code (not working):
>>
>> *** begin pseudo-code
>>
>> foreach t in 1992q1/2000q4 {
>>      reg y L.y L.x if tin(1960q1, `t')
>>      reg y L.y L.x if tin(`=`t'-20', `t')
>> }
>
>  How about this:
>
>    forvalues t = `=q(1992q1)'/`=q(2000q4)' {
>       reg y L.y L.x if date >= q(1960q1) & date <= `t'
>       reg y L.y L.x if date >= `t'-20 & date <= `t'
>    }
>
>
>  where date is your quarterly date variable.
>
>    -- Brian Poi
>    -- [hidden email]
>
> *
> *   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/