Announcement

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

  • How can i create a new "time variable" (t=-4, t=-3....t=4) based in the value of another variable?

    I have a data panel and i'm working with transitions. I have data that goes from 2017-2021 and a variable called event_14 that identifies when an individual makes a transition and in which year (1 is transition and the rest are missing values). What i want to do is to generate a variable called t_time_14 that makes the year in which the individual made the transition into 0 and after that 1, 2, 3 and same before the transition (-1,-2..). I have tried a few ways to making that but i cant seem to get what i want. can someone please help me? im sorta new in stata. thanks!

    when i run this i get

    gen t_time_14 =year

    replace t_time_14=0 if event_14==1
    replace t_time_14=1 if event_14==. & event_14[_n-1]==1
    replace t_time_14=2 if event_14==. & event_14[_n-2]==1
    replace t_time_14=3 if event_14==. & event_14[_n-3]==1
    replace t_time_14=4 if event_14==. & event_14[_n-4]==1
    replace t_time_14=-1 if event_14==. & event_14[_n+1]==1
    replace t_time_14=-2 if event_14==. & event_14[_n+2]==1
    replace t_time_14=-3 if event_14==. & event_14[_n+3]==1
    replace t_time_14=-4 if event_14==. & event_14[_n+4]==1

    Click image for larger version

Name:	Screenshot 2023-05-26 205840.png
Views:	1
Size:	115.3 KB
ID:	1715138


    and i also tried something else

    gen t_time_1 =.
    replace t_time_1=0 if event_1==1

    replace t_time_1=1 if t_time_1[_n+1]==. & t_time_1[_n-1]==0
    replace t_time_1=2 if t_time_1[_n+2]==. & t_time_1[_n-1]==1 & t_time_1[_n-2]==0
    replace t_time_1=3 if t_time_1[_n+3]==. & t_time_1[_n-1]==2 & t_time_1[_n-2]==1 & t_time_1[_n-3]==0
    replace t_time_1=4 if t_time_1[_n+4]==. & t_time_1[_n-1]==3 & t_time_1[_n-2]==2 & t_time_1[_n-3]==1 & t_time_1[_n-4]==0
    replace t_time_1=-1 if t_time_1[_n-1]==. & t_time_1[_n+1]==0
    replace t_time_1=-2 if t_time_1[_n-2]==. & t_time_1[_n+1]==-1 & t_time_1[_n+2]==0
    replace t_time_1=-3 if t_time_1[_n-3]==. & t_time_1[_n+1]==-2 & t_time_1[_n+2]==-1 & t_time_1[_n+3]==0
    replace t_time_1=-4 if t_time_1[_n-3]==. & t_time_1[_n+1]==-3 & t_time_1[_n+2]==-2 & t_time_1[_n+3]==-1 & t_time_1[_n+4]==0
    replace t_time_1=-1 if t_time_1[_n+1]==0

    Click image for larger version

Name:	Screenshot 2023-05-26 210104.png
Views:	1
Size:	70.9 KB
ID:	1715139
Working...
X