Announcement

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

  • How to generate a dummy variable to indicate whether a variable has changed from the previous year

    I am trying to generate a variable in panel and I am having some issues. I would like to generate a dummy variable with value 1 if an individual has transitioned from any other employment status (jbstat!=1) to being self-employed (jbstat==1).

    I have a variable labelled 'pidp' that gives an individual's ID, variable 'jbstat' giving employment status and variable 'year' with values 2013 2015 2017 2019 for each individual (the data is in long format).

    I am unsure of how to generate a dummy variable that = 1 if the individual has gone from being not self-employed (jbstat!=1) in year t to being self-employed (jbstat==1) in year t+2, and = 0 if they haven't transitioned to being self-employed.

    Thank you in advance.

  • #2
    Code:
    by pidp (year), sort: gen wanted = (jbstat == 1 & jbstat[_n-1] != 1) if _n > 1
    Note that in the first year of a person's data it is impossible to know if jbstat has changed from before. So this code provides missing values in all first-observations. Thereafter, wanted = 1 when jbstat changes to 1, and 0 in other circumstances. Note also that if the person transitions more than once, each transition is marked with 1.

    I have written this code by supplementing your description of your data with some assumptions about it that are typically, but not always, true. If those assumptions are wrong, this code may fail in your data. In the future, to avoid wasting my time writing code for non-existent data, and yours, reading and trying out solutions that have no hope of working, please always show example data using the -dataex- command when you want help with code. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Hi Clyde, thank you for the code, it seems to have done the job. As for -dataex-, I appreciate the advice and will use it next time I have a question. Thanks again!

      Comment

      Working...
      X