Announcement

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

  • gen new variables

    I am using a panel dataset, how can I create a new variable that is equal to 1 if UNITID ever changed for var1? (And =0 if the UNITID never changed for var1)

    Also, how can I create a new variable that is equal to the "Year" that the change occurred for each UNITID that changed?

    I am trying to create a difference in difference regression, but my Stata knowledge is very limited. thank you in advance.

  • #2
    On a guess that your data contains variables UNITID, year, and var1, that UNITID and year together identify unique observations, and that for each value of UNITID if we arrange the observations in chronological order, the values of var1 are either all the same, or they are all the same up to some year, and then after that they take on a different value which remains constant until the end:
    Code:
    by UNITID (var1), sort: gen byte it_changed = var1[1] != var1[_N]
    by UNITID (year), sort: egen when_it_changed = min(cond(var1 != var1[1], year, .))
    Note: the first command will work even if most of what I have imagined about your data is wrong. The second command, however, will produce incorrect results if var1 can change more than once among the observations of the same UNITID. (Actually, in that circumstance the question posed is meaningless anyway.) Also, you did not say what to do for the second question with those UNITIDs where var1 never changes: this code will return missing value there.

    My guess about your data is based on my experience that data sets like this are commonly encountered and might provoke the kind of questions you asked. But if I have guessed wrong, then we have both wasted our time. In the future, it is best to avoid guesswork by actually showing example data from your Stata data set when asking for help with code. The helpful way to do that is by using the -dataex- command. If you are running version 18, 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      See also https://www.stata.com/support/faqs/d...ions-in-group/ and https://journals.sagepub.com/doi/pdf...867X1101100210 as context for these commands.

      Comment

      Working...
      X