Hi everyone,

I have a dataset where v1 is the household id that gives out one id per household.
I have other variables called v2 and v3 which are just consisted of numbers.
I want to generate a new variable 'v5' which is v3-v2.
However, the problem is that there are only one value of v2 and v3 in the same household id, so the value of v3 and v2 are not in the same column.

So in concise, I am wondering how I could generate v5
1) when v2 and v3 are not in the same column
2) using v2 and v3 that are bounded by the same household id

For example, the data looks like this:
v1 v2 v3 v4 v5
1 10 . . 10
1 . 20 .
2 13 . . 7
2 . 20 .
3 11 . . 6
3 . 17 .
4 22 . . -11
4 . . 2
4 . 11 .

Here are some things that I tried but did not succeed:

1) replace the missing value of v2 with the values from v3
2) tried this code (bysort v1: replace v4=v3-v2)

I tried to look up some threads on this but couldn't find much succesfully. (If anyone could generously help me with tips on how to search on this matter, that would be great as well)

Thank you in advance!

P.S. I use STATA 14.0 for Mac