Announcement

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

  • Identifying Year of First Move

    Hello Friends,

    I have a dataset with the following variables: "year", "pair_id", "house_id", "moved_once". "year" is the year, "pair_id" is a pair-specific identifier, "house_id" is a house-specific identifier, and "moved_once" is a dummy indicating if a pair has moved at least once at some point in the data (their "house_id" changed at least once). "moved_once" = 1 for all observations for pairs who moved at least once, and 0 for pairs who haven not moved. Below is an illustration of the data:
    Click image for larger version

Name:	Screen Shot 2023-11-12 at 18.59.39.png
Views:	1
Size:	164.0 KB
ID:	1733615



    I need to add a variable "year_first_move" indicating the year in which a pair first moved ("house_id" first changed). "year_first_move" should equal this year-of-first-change but for all observations of a given "pair_id", and naturally only for "pair_id"s where "moved_once" = 1. "year_first_move" can be blank for "pair_id"s where "moved_once" = 0. An illustration is below:
    Click image for larger version

Name:	Screen Shot 2023-11-12 at 19.07.01.png
Views:	1
Size:	252.7 KB
ID:	1733616



    I tried to create "year_first_move" with the following code:

    Code:
    gen year_first_move = .
    bysort pair_id (year): forvalues i = 2(1)_N {
        local year_first_move_l = 1
        replace `year_first_move_l' = year[`i'] if house_id[`i'] != house_id[`i' - 1]
        if `year_first_move_l' > 1 {
            replace year_first_move = `year_first_move_l'
            continue, break
        }
    }
    The inner workings of the loop are still rough (e.g., I haven't figured out how to replace all values of "year_first_move" with the year-of-first-change), but my urgent problem is that Stata does not even allow by and forvalues together. Is there any way I can work around this issue in creating my desired variable? Any help would be greatly appreciated.

    Thank you,

    Darman
Working...
X