Kiefer
Kiefer

Reputation: 163

How to copy value from previous group into the next group in Stata

I think this might be simple but haven't found a way to do it in Stata so I'd really appreciate if someone could help me out. I'm trying to copy values from one group to the other, with the restriction that the values must be from the previous year. I think the example and the results I'd like to have may make it clearer:

I have data that looks like this:

year  group_id   educ  place
1990     1        6      a
1990     1        6      b
1992     2        2      c
1992     2        2      d
1994     3        11     e
1994     3        11     f
1990     4        10     g
1990     4        10     h
1992     5        5      i
1992     5        5      j
1994     6        7      k
1994     6        7      l

I have different groups, identified by "group_id" and different years ("year"), and I want, for example, for groups to get the educ value from the previous year. But I don't want 1990 to get the value from 1994. Is this possbile? My data would hopefully end up looking like this:

year  group_id   educ  place  prev_educ
1990     1        6      a        .
1990     1        6      b        .
1992     2        2      c        6
1992     2        2      d        6
1994     3        11     e        2
1994     3        11     f        2
1990     4        10     g        .
1990     4        10     h        .
1992     5        5      i        10
1992     5        5      j        10
1994     6        7      k        5
1994     6        7      l        5

I tried variations of:

gen prev_educ=.
bysort group_id: replace prev_educ=educ[_N -1] if group_id[_n]!=group_id[_n-1] 

which is clearly not what I want.

Upvotes: 0

Views: 465

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

This is a very dangerous data structure, as sorting without extreme care could destroy its integrity. It appears that only the order of year tells you that 1, 2, 3 belong together and 4, 5, 6 belong together.

This reproduces your example:

clear 
input year  group_id   educ  str1 place
1990     1        6      a
1990     1        6      b
1992     2        2      c
1992     2        2      d
1994     3        11     e
1994     3        11     f
1990     4        10     g
1990     4        10     h
1992     5        5      i
1992     5        5      j
1994     6        7      k
1994     6        7      l
end 
gen prev_educ = . 
replace prev_educ = educ[_n-1] if year > year[_n-1] 
replace prev_educ = prev_educ[_n-1] if group_id == group_id[_n-1] & missing(prev_educ) 

Upvotes: 3

Related Questions