Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

Re: st: Looping over datetimes for simultaneous variable generation

 From Nick Cox <[email protected]> To "[email protected]" <[email protected]> Subject Re: st: Looping over datetimes for simultaneous variable generation Date Thu, 3 Oct 2013 12:56:46 +0100

```Might be as simple as

replace z = z[_n-1] + y  if mi(x) & z[_n-1] < x[_n-1]
replace y = y[_n-1] if mi(x) & z[_n-1] < x[_n-1]
replace x = x[_n-1] if mi(x) & z[_n-1] < x[_n-1]

Nick
[email protected]

On 3 October 2013 11:53, Paul Balcombe
<[email protected]> wrote:
> Thankyou for the response.
>
> I'll try to explain what I want to calculate more clearly:
> I have a dataset with observations of 4 variables every hour for one year. An  example of the data is below. I now want to calculate 5 minutely values in between this hourly data. At each 5 minute interval I want to estimate w, x, y and z with the following rules (not in stata language):
>
> At time t, replace the following variables if mi(x) and if z(t-1) < x(t-1):
> z(t) = z(t-1) + y(t)
> y(t) = y(t-1)
> x(t) = x(t-1)
>
> Where the if statements are false, do not generate new values for the variables.
>
> I need to calculate all variables at each time period, rather than estimate one variable at a time because they are dependent on each other at each time point.
>
> I hope  this is slightly clearer.
> Thanks,
> Paul
>
>
> datetime w x y z
> 19/08/2012 00:00 0 0 0 0
> 19/08/2012 00:05 . . . .
> 19/08/2012 00:10 . . . .
> 19/08/2012 00:15 . . . .
> 19/08/2012 00:20 . . . .
> 19/08/2012 00:25 . . . .
> 19/08/2012 00:30 . . . .
> 19/08/2012 00:35 . . . .
> 19/08/2012 00:40 . . . .
> 19/08/2012 00:45 . . . .
> 19/08/2012 00:50 . . . .
> 19/08/2012 00:55 . . . .
> 19/08/2012 01:00 3.4 0.2448 0.2833333 0.2833333
> 19/08/2012 01:05 . . . .
> 19/08/2012 01:10 . . . .
> 19/08/2012 01:15 . . . .
> 19/08/2012 01:20 . . . .
> 19/08/2012 01:25 . . . .
> 19/08/2012 01:30 . . . .
> 19/08/2012 01:35 . . . .
> 19/08/2012 01:40 . . . .
> 19/08/2012 01:45 . . . .
> 19/08/2012 01:50 . . . .
> 19/08/2012 01:55 . . . .
> 19/08/2012 02:00 0 0 0 0
>
>
>
> ________________________________________
> From: [email protected] [[email protected]] on behalf of Nick Cox [[email protected]]
> Sent: 03 October 2013 10:48
> To: [email protected]
> Subject: Re: st: Looping over datetimes for simultaneous variable generation
>
> There are several problems evident here, and I guess some latent too.
>
> You don't refer to any `datetime' macro within the loop, so at best it
> would do the same things, regardless of the loop.
>
> The syntax ' ' means nothing to Stata. You may wish to evaluate
> numeric constants on the fly, for which the syntax would be something
> like  `=  tc(19aug2012 0:00:00)’
>
> The constant tc(00:05:00) seems unlikely to be what you intend.
>
> For an arbitrary numlist you need -foreach- not -forvalues-.
>
> Those points aside, a better strategy might to explain what you want
> to calculate, with enough about your data and some example data for
> people to follow.
> Nick
> [email protected]
>
>
> On 3 October 2013 10:18, Paul Balcombe
> <[email protected]> wrote:
>> I have a timeseries data set and I want to generate a number of variables that are conditional on each other and their previous values, so I want to create a value for the set of variables at each time period in my data. I thought I should use a forvalues command, but this is new to me and I have so far been unsuccessful: invalid syntax. below is the command I wish to make:
>>
>> forvalues datetime= ‘tc(19aug2012 00:00:00)’ (‘tc(00:05:00)’) ‘tc(18aug2013 23:00:00)’ {
>>                 replace seen=seen[_n-1] if seen==. & seen3[_n-1]<seen[_n-1]
>>                 replace segaspow=segaspow[_n-1] if seen==. & seen3[_n-1]<seen[_n-1]
>>                 replace seen2=seen2[_n-1] if seen==. & seen3[_n-1]<seen[_n-1]
>>                 replace seen3=seen2+seen3[_n-1] if seen==. & seen3[_n-1]<seen[_n-1]
>> }
>>
>> I may be describing the datetime functions incorrectly.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```