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

# Re: st: How to identify two consequtives values of a variable

 From Nick Cox To statalist@hsphsun2.harvard.edu, "Pablo.CARVALLO" Subject Re: st: How to identify two consequtives values of a variable Date Fri, 22 Jul 2011 17:27:48 -0500

```Pablo replied directly to me. Please note the request to close threads
publicly.

I started writing

if abs(x[_n-1]) == 1

as a way of catching both 1 and -1 and realised that the "== 1" was
not needed. What you have is equivalent for your data.

Nick

On Fri, Jul 22, 2011 at 5:16 PM, Pablo.CARVALLO <pablocepal@gmail.com> wrote:
> Thanks Nick, I didn't understand your first instruction, but I tried
> with a little modified version of your routine, and it works properly:
>
> gen previous=x[_n-1] if x[_n-1]!=0
> replace previous=previous[_n-1] if missing(previous)
> gen y1= previous==x
>
> Regards,
>
> Pablo
>
>
> . gen previous = x[_n-1] if abs(x[_n-1])
> . replace previous = previous[_n-1] if missing(previous)
> . gen y = x == 1 & previous ==1
>
>
> Nick
>
> On Fri, Jul 22, 2011 at 12:06 PM, Pablo.CARVALLO <pablocepal@gmail.com> wrote:
>> Hi Statalist,
>>
>> I create a variable x which states a local max (1), a local min(-1),
>> and 0 in other case. If x==1, the following value may be 0 or -1, but
>> it cannot be 1 again. For example, an 'ideal' x may be:
>>
>> x
>> --
>> 0
>> 0
>> 1
>> 0
>> 0
>> 0
>> 0
>> -1
>> 0
>> 0
>> 1
>> -1
>> 0
>> 0
>> 0
>> 1
>>
>> But I have something like this:
>>
>> x
>> --
>> 0
>> 0
>> 1
>> 0
>> 0
>> 0
>> 0
>> 1 ----> this value is wrong, because previous x different from 0 was 1.
>> 0
>> 0
>> -1
>> 1
>> 0
>> 0
>> 0
>> -1
>>
>> I need a variable 'y' which tells me when x takes this wrong values:
>>
>> x     y
>> ---------
>> 0     0
>> 0     0
>> 1     0
>> 0     0
>> 0     0
>> 0     0
>> 0     0
>> 1     1 ---> y==1
>> 0     0
>> 0     0
>> -1    0
>> 1     0
>> 0     0
>> 0     0
>> 0     0
>> -1    0
>>
>

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