Announcement

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

  • Conditional on the value of one variable in panel data

    Dear List,

    I have, say, 10 waves (wave) of panel data with individuals (ID). There are 3 variables X, Y, Z. I want a table of Y by Z conditional on/IF (for all IDs), X=0 at wave=5.
    (I think I have figured out how to do this but it involves several preserve, keep, manipulations, save restore, & merge operations -- there must be a cleverer, simpler way.

    An help?

    Thanks
    Laurence

  • #2
    I want a table of Y by Z conditional on/IF (for all IDs), X=0 at wave=5.
    I can see two different ways of understanding this.

    1. If it is true that for every ID in the data set, X = 0 at wave = 5, produce a cross-tabulation of Y by Z for the entire data set. If for any ID in the data set, there is an X different from 0 at wave = 5, produce no output at all.

    Code:
    egen byte condition_fail = max(X != 0 & wave == 5)
    if !condition_fail {
        tab Y Z
    }
    Or
    2. Consider each ID separately. For any ID that has X = 0 when wave = 5, produce a cross tabulation of Y by Z for all the observations in that ID. For the other IDs, produce no output.
    Code:
    by  ID, sort: egen byte condition_fail = max(X != 0 & wave == 5)
    levelsof ID, local(IDs)
    foreach id of local IDs {
        summ condition_fail if ID == `id', meanonly
        if `r(mean)' == 0 {
            tab Y Z if ID == `id'
        }
    }
    Note: as no example data was provided, this code is untested. It may contains typos. It may not even be suitable for the way your data is organized as it is based on my imagination of what such a data set might be like. (For example, it assumes ID is a numeric variable. It also assumes the data is in long layout.) So if the code does not work in your real data, post back with example data. And be sure to use the -dataex- command to do that. 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.

    More generally, when asking for help with code, it is usually wise to show example data. Imaginary code written for imaginary data often fails when applied to the real thing, with both of us thereby having wasted our time.

    Comment


    • #3
      Thanks Clyde, your second option is very useful.
      I appreciate your final suggestion re code. I did think of it, but my code was so convoluted I thought it would just bore anyone reading. Rest assured your time was not wasted.
      Regards
      Laurence

      Comment

      Working...
      X