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

# Re: st: Creating a dummy variable under certain conditions

 From Nick Cox <[email protected]> To [email protected] Subject Re: st: Creating a dummy variable under certain conditions Date Sat, 2 Jun 2012 13:47:46 +0100

```Use the -sum()- function to produce cumulative sums:

bysort gvkey (date) : gen cuviol = sum(viol)

Then the previous four observations were 0 if and only if the
cumulative sum did not change over that interval. This gives you a way

by gvkey : gen myindicator = cuviol[_n-1] == cuviol[_n-4]

Nick (on behalf of SAND*)

* Society for the Abolition of the Name Dummy

On Sat, Jun 2, 2012 at 1:06 PM, sebas nicaise <[email protected]> wrote:

> I am struggeling with creating a new dummy variable. I have a very large (unbalanced) panel dataset that looks like this:
>
>
>
> date gvkey viol
> 1997q1 1021 0
> 1997q2 1021 0
> 1997q3 1021 0
> 1997q4 1021 0
> 1998q1 1021 1
> 1998q2 1021 0
> 1998q3 1021 0
> 1998q4 1021 1
> 1999q1 1021 0
>
> 1999q2 1021 0
>
> 1999q3 1021 0
> 1999q2 1023 0
> 1999q3 1023 1
> 1999q4 1023 0
> 2000q1 1023 0
> 2000q2 1023 1
>
>
> Where date is calender quarter, gvkey is the company identifier and viol a dummy which equals 1 if the firm is in violation of a covenant in a private debt contract.
>
>
>
> The problem is that I have to create a new dummy variable ( new violation) which meets certain conditions.
>
> The new violation dummy is only allowed to be 1 if in the previous 4 quarters of a certain company (gvkey) is 0.
>
>
>
> This means for example that:
>
>
>
> date gvkey viol Newviolation
> 1997q1 1021 0       0
> 1997q2 1021 0       0
> 1997q3 1021 0       0
> 1997q4 1021 0       0
> 1998q1 1021 1       1
> 1998q2 1021 0       0
> 1998q3 1021 0       0
> 1998q4 1021 1       0
> 1999q1 1021 0       0
>
> 1999q2 1021 0       0
>
> 1999q3 1021 0       0
> 1999q2 1023 0       0
> 1999q3 1023 1       0
> 1999q4 1023 0       0
> 2000q1 1023 0       0
> 2000q2 1023 1       0
>
>
>
> I do not know how to tell Stata to create this variable, Hope someone is so kind to help me.

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