Announcement

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

  • Comparing values within the same variables

    Hi all, I have a longitudinal dataset (N=560) over two waves with an identifier variable called "pid" (I cannot post the dataset here since it is microdata). Everyone has a unique ID and is in wave 1 and wave 2. After cleaning up the dataset, some observations were dropped, meaning that some individuals in both waves are now in only one wave. I want to drop these observations by comparing the ID values in "pid" and generating a new variable called "match" that takes on a value of 1 if the "pid" values are equal, '.' if not. I have the following code:

    sort pid
    gen match = 0

    // Loop through the observations and compare 'pid' values
    forvalues i=2/560 {
    if pid[`i'] == pid[`i'-1] {
    replace match[`i'] = 1
    }
    }

    Each time this returns an error "weights not allowed".

    Can anyone help?

  • #2
    Code:
    assert !missing(pid)
    bys id (pid): gen same= pid[1]==pid[_N]
    where "id" is the individual identifier and "pid" is the wave identifier. This creates an indicator where same=1 if an individual is observed in a single wave and zero otherwise. So

    Code:
    drop if same
    See https://www.stata.com/support/faqs/d...ions-in-group/
    Last edited by Andrew Musau; 02 May 2023, 10:28.

    Comment


    • #3
      This is just what I needed - thank you so much!

      Comment

      Working...
      X