Announcement

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

  • Turning three variables into one based on year. Code included

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double hhid int intyear double(dst2012 dst2010 dst2008)
    1010140020171 2008 .71 .71 4.124
    1010140020171 2010 .71 .71 4.124
    1010140020171 2012 .71 .71 4.124
    1010140020284 2008 .71 .71 4.124
    1010140020284 2010 .71 .71 4.124
    end

    Hi there,

    I working to build a panel dataset and I've run into a bit of trouble figuring out how to create a single distance variable from three separate distance variables based on year. I need a variable that will pull in the correct distance based on the year variable. E.g., in the first observation, I'd like the hhid for 2008 to correspond to a single variable that would pull in the dst2008 first observation, while the next hhid observation would correspond to dst2010. Any help would be greatly appreciated.

    Kind regards,
    Last edited by Tyler Spencer; 05 Mar 2022, 22:01.

  • #2
    It is not clear to me whether you want to retain the three dst* variables and add one more that has the year-intyear correspondence:
    Code:
    reshape long dst, i(hhid intyear) j(year)
    by hhid intyear: egen wanted = max(cond (year == intyear, dst, .))
    reshape wide
    or if you want to just end up with a single dst variable extracted from the one corresponding to the value of intyear, losing the original dst* variables:
    Code:
    reshape long dst, i(hhid intyear) j(year)
    keep if year == intyear
    drop year

    Comment

    Working...
    X