Announcement

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

  • List unique id by year

    For a longitudinal research - i'm trying to get a list of observations which do not appear in specific, according to their unique ID.

    In other words - to know whether a particular observation appeared in one year and then did not appear in the following year.
    The result that I want to get is a list of these observations ID

    E.g in the file attached - I want to find what the ID of the 19 "new" observations that appear in 1994 but not in 1993.

    I would appreciate your help,
    Thanks!
    Gal

  • #2
    If you have a relatively small number of IDs and you're interested only in 1993 vs 1994:

    Code:
    levelsof id if year == 1994, local(1994ids)
    levelsof id if year == 1993, local(1993ids)
    
    local new_ids: list 1994ids - 1993ids
    display `"`new_ids'"'
    If you have a large number of relevant IDs, then that might be too cumbersome. Still assuming only 1993 vs 1994 matters:
    Code:
    preserve
    keep if year == 1993
    keep id
    duplicates drop
    tempfile 1993ids
    save `1993ids'
    restore
    keep if year == 1994
    keep id
    duplicates drop
    merge 1:1 id using `1993ids', keep(master)
    list
    If you are actually interested in identifying any observation that appears in any year but not in the previous:

    Code:
    xtset id year
    by id (year): gen byte apparition= missing(L.year)
    list id year if apparition == 1
    Note: The above will mark the first year in which any id appears as an apparition. So, if you don't want to count those, you can just add -if _n > 1- to the end of the -by id (year): gen...- command.
    Last edited by Clyde Schechter; 31 Mar 2015, 15:32.

    Comment

    Working...
    X