Announcement

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

  • Creating 'active' variable for panel data

    Hi all, I have a question related to constructing a variable in panel.

    I want to create a variable that captures the activeness of the organization.

    I have a panel organization-year data where I have a variable that captures the # of events per year. Essentially my data looks like the following.

    organization year #_events
    1 1990 0
    1 1991 0
    1 1992 1
    1 1993 2
    1 1994 0
    1 1995 0
    1 1996 1

    I want to construct a variable that captures the activeness of the organization. I want it to be coded as 1 if the org had at least one event in past n years including the current year. For example, if it is 3, the 'active' will equal to 1 starting from 1992 to 1996. However, if n=2, the active will equal to 1 from 1992 to 1994, and 0 for 1995, and then 1 for 1996 again.

    I would think that I can use combination of by and sort but I have no clue how to do them. Please advice!

    Many thanks in advance.

    -S
    Last edited by Sukhun Kang; 09 Apr 2018, 22:55.

  • #2
    You can use rangestat (SSC) for this purpose. You look for the maximum in a moving window and then reduce it to a (0, 1) indicator. Here is an example.

    Code:
    clear 
    input organization year n_events
    1 1990 0
    1 1991 0
    1 1992 1
    1 1993 2
    1 1994 0
    1 1995 0
    1 1996 1
    end 
    
    rangestat (max) n2=n_events, int(year -1 0) by(org) 
    rangestat (max) n3=n_events, int(year -2 0) by(org) 
    
    replace n2 = n2 > 0 
    replace n3 = n3 > 0 
    
    list, sep(0) 
    
    
         +--------------------------------------+
         | organi~n   year   n_events   n2   n3 |
         |--------------------------------------|
      1. |        1   1990          0    0    0 |
      2. |        1   1991          0    0    0 |
      3. |        1   1992          1    1    1 |
      4. |        1   1993          2    1    1 |
      5. |        1   1994          0    1    1 |
      6. |        1   1995          0    0    1 |
      7. |        1   1996          1    1    1 |
         +--------------------------------------+
    For the announcement of rangestat, see https://www.statalist.org/forums/for...s-within-range

    For other examples, search this forum with rangestat as key word.

    Comment


    • #3
      Amazing. Thanks for your help!

      Comment

      Working...
      X