Search code examples
statisticsstataeconomics

Stata: How to find firms which changed the status in the time-series dataset


I have a data set containing information about different firms' performance in time. It has variables: firm id, year, firm's exporting status (basically, 1 if it is exporter). I want to know how many companies (in a specific group, during specific time interval) made a exporting decision, e.g. changed exporting status from 0 to 1. I would like to avoid brute force approach by conversion to csv and then parsing by python script.


Solution

  • Here's one way that uses lag operators:

    clear
    
    input firm_id   t   exp
    0   1   0
    0   2   1
    1   1   0
    1   2   1
    1   3   1
    2   1   0
    2   2   1
    2   3   0
    3   1   1
    3   2   1
    4   1   0
    4   2   0
    end
    
    xtset firm_id t
    gen start_exp = cond(exp==1 & L.exp==0,1,0)
    bys t: sum start_exp