Announcement

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

  • Looping a command in a panel dataset

    Dear all,
    sorry for the apparently silly question.

    I am trying to write a syntax that loops up to when the condition specified it is not anymore present.

    Basically, the command is:
    Code:
    drop if status!=2 & status[_n-1]==2 & caso==caso[_n-1]
    I want to loop this command in all the waves, but the following command does not work:
    Code:
    forval i in 1 (1) 18 {
     drop if status!=2 & status[_n-1]==2 & caso==caso[_n-1] if wave == `i'
     }
    *
    Do you have any advise? Here the fictious data I am using:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(caso status wave)
    1 1  1
    1 1  2
    1 2  3
    1 .  4
    1 2  5
    1 1  6
    1 .  7
    1 .  8
    1 2  9
    1 2 10
    1 . 11
    1 1 12
    1 2 13
    1 2 14
    1 1 15
    1 . 16
    1 1 17
    1 1 18
    2 .  1
    2 2  2
    2 1  3
    2 1  4
    2 2  5
    2 .  6
    2 .  7
    2 2  8
    2 2  9
    2 3 10
    2 2 11
    2 . 12
    2 1 13
    2 . 14
    2 . 15
    2 2 16
    2 1 17
    2 3 18
    end
    Thanks a lot, best, G

  • #2
    You will want to use by here

    Code:
    bys wave (caso): gen tag= status!=2 & status[_n-1]==2 & caso==caso[_n-1]
    drop if tag
    Last edited by Andrew Musau; 21 Mar 2019, 05:52.

    Comment


    • #3
      Dear Andrew,
      your command does not make any change to the data actually, how should I do?

      Comment


      • #4
        Can you explain what you want to do?

        drop if status!=2 & status[_n-1]==2 & caso==caso[_n-1]
        This says that if "status" changes from 2 and "caso" remains the same, then I should drop that observation. What variable defines how observations are sorted in a given wave? In my code, I assumed that this variable is "caso".

        Comment


        • #5
          I agree with Andrew that there is much missing from the statement of the problem. My interpretation is that the data is longitudinal, "caso" is some sort of identifier of the survey unit, and each "caso" is observed in waves 1-18. The objective is ... not clear, in particular because nothing is said about how missing values of status are to be treated. If it weren't for the missing values, it seems to be to drop an observation where status!=2 after each observation where status==2. But perhaps the description in post #1 assumes that if you have the sequence 2 1 1 ... the first 1 will be dropped, and then the second 1 will be compared to the 2 and it also will be dropped.

          What would be helpful would be explain for the data in post #1 which waves of each caso are to be dropped, and why.

          Comment

          Working...
          X