Announcement

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

  • Creating a variable that counts conditions being satisfied in previous periods

    Hi All,

    Relatively straightforward issue that seems to be taking me a little too long to solve:

    In the attached, I want the variable 'rexp' (routine experience) to show a value of +1 if the individual was in a routine occupation at the start and end of the previous period of transition. Each row represents a period of transition (there are 5 periods of transition, represented by the variable age).

    Note that in the screenshot each unique individual appears for 5 periods of transition, but this isn't the case across the data; some are present for all 5 some aren't.

    What I want is 'rexp' to be a variable that ranges from 0-4 based on how many previous periods the individual was in a routine occupation at the start and end of the previous period (i.e. when 'occgroup0'=4 and 'occgroup1'=4).

    Any quick solutions?

    See attached for screenshot (and my failed attempt at coding it)

    Many thanks,
    Ricky.


    Attached Files

  • #2
    I'm not sure I understand what you want because the values of rexp that you show are not at all consistent with what you say you want. For example, in observation 3 we have both occgroup0 and occgroup1 equaling 4, yet rexp remains 0 in observation 4 and beyond. In observation 18, rexp = 1 even though N10004R has never had either occgroup variable equal to 4.

    So I'll ignore what you show and give some code that, I think, will produce what you described.

    Code:
    xtset ncsid age
    by ncsid (age), sort: gen regular_periods_so_far = sum(occgroup0 == 4 & occgroup1 == 4)
    gen rexp = L1.regular_periods_so_far
    Important Note: NEVER post example data as a screen shot. The code above is untested, and may contain errors. I have only "debugged" it by imagining what the code would do with real data. I would much prefer to have used your example data to test my code, and correct errors if I have it wrong. But there is no way to import data from screenshot into Stata. The helpful way to show example data is by using the -dataex- command. Install it by running -ssc install dataex-. Then read the simple instructions by running -help dataex-. Going forward, always use -dataex- to post example data. It enables those who want to help you to create a completely faithful replica of your example in Stata with a simple copy/paste operation.

    If this code does not do what you want, post back including example data to work with using -dataex-. The example should include correct hand-calculated values of the desired variable rexp. And provide as clear an explanation as possible, consistent with your example data, of how you arrived at those hand-calculated values. Also in your explanation, please state how you want to handle situations where one of the occgroup* variables has a missing value.

    Comment


    • #3
      Clyde,

      Thank you for getting back to me.

      Apologies for not using dataex, and apologies for the lack of clarity above.

      With a few minor adjustments to the code you wrote it works!

      Thank you very much, and thanks for the advice on posting in the future.

      Best wishes,
      Ricky.

      Comment

      Working...
      X