Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Observations withinin group that differ on a specific year

    Hello!

    I'm trying to create two dummy variables, namely:
    - Dummy equal to 1 if the first sample year (1989) is the first year with a new auditor
    - Dummy equal to 1 if the last sample year (1992) is followed by an auditor change

    So in my example below, the variable 'au_n' has values ranging from 1 to 20 where each number represents a specific auditor.
    Now I would like to find out if 1988 had a different au_n number than 1989 for my first dummy variable;
    And if 1993 has a different au_n number than 1992 for my second dummy variable.

    I have tried following https://www.stata.com/support/faqs/d...ions-in-group/
    But it was not exactly what I was looking for, as I specifically wanted to check the change in the above mentioned years for my dummy variables.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 gvkey double fyear byte au_n
    "001004" 1987  6
    "001004" 1988  6
    "001004" 1989  6
    "001004" 1990  6
    "001004" 1991  6
    "001004" 1992  6
    "001009" 1988  4
    "001009" 1989  4
    "001009" 1990  4
    "001009" 1991  4
    "001009" 1992  4
    "001009" 1993  4
    "001010" 1988 20
    "001010" 1989  6
    "001010" 1990  6
    "001010" 1991  6
    "001010" 1992  6
    "001010" 1993  6
    end
    Hope it's clear what I mean, thank you in advance!

  • #2
    As subscripting (using _N and _n) should not be used with egen, you need to do this in two lines.

    Code:
    bys gvkey (fyear): gen wanted1= au_n[_n-1]!= au_n & fyear==1989 & fyear[_n-1]==1988
    bys gvkey (wanted1): replace wanted1=wanted1[_N]
    
    bys gvkey (fyear): gen wanted2= au_n[_n+1]!= au_n & fyear==1992 & fyear[_n+1]==1993
    bys gvkey (wanted2): replace wanted2=wanted2[_N]
    Also, you could also use time series operators:

    Code:
    encode gvkey, g(company)
    xtset company fyear
    gen wanted1= L.au_n!= au_n & fyear==1989& fyear[_n-1]==1988
    bys company (wanted1): replace wanted1=wanted1[_N]
    sort company fyear
    gen wanted2= F.au_n!= au_n & fyear==1992&  fyear[_n+1]==1993
    bys company (wanted2): replace wanted2=wanted2[_N]
    Last edited by Andrew Musau; 15 May 2022, 08:59.

    Comment


    • #3
      Thank you so much! It worked perfectly!

      Comment

      Working...
      X