Announcement

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

  • Constructing lagged sums of dependent variable.

    Dear All,

    I have panel data on poisonings with 58 monthly observations (waves) per individual. Each wave there is a dummy for whether the person poisoned that month. For each wave I want to construct two variables- (1) whether a person has poisoned before or not and, (2) how many times a person has poisoned before. Because of the large number of waves I am reluctant to use the L. function to capture lags. Is there a more sensible way to do this? I suspect it shouldn't be hard but I am stuck.

    Many thanks.
    Sumedha.

  • #2
    Hi Sumedha,
    this should work for your scope

    Code:
    bys id (wave): gen n_poisoned = sum(poisoned)
    bys id (wave): gen number_poisoned = n_poisoned[_n-1]
    gen has_poisoned = (number_poisoned>=1 & number_poisoned<.)
    Best,
    Raffaele

    Comment


    • #3
      Suppose p is your panel variable and w is your wave variable and x is your variable for poisoned that month. The following should start you to what you want.
      Code:
      bysort p (w): generate times_poisoned_before = sum(x)
      bysort p (w): generate has_poisoned_before = min(times_poisoned_before,1)
      Sample data for just one well-chosen individual would have helped me write this answer. You've posted sample data on earlier topics, it would have helped on this one as well.

      Added in edit: This crossed with #2. It is not clear to me what you mean by "before" - Raffaele takes it to mean "in waves previous to the current one" while I did not. Again, sample data with an explanation of the values you hope for would have made your meaning clear.

      Added further: I think the code in post #1 will generate a missing value for each id in wave 1. Replacing the second command with the following will yield a 0 instead of a missing by subtracting the current value from the running sum.
      Code:
      bys id (wave): gen number_poisoned = n_poisoned - poisoned
      Last edited by William Lisowski; 10 May 2018, 09:13.

      Comment


      • #4
        Thank you so much! Works perfectly!

        Comment

        Working...
        X