Hi all -

A friend of mine has a long dataset that looks like the following. Essentially, she wants to replace the missing variables based on the numlist 1/7, but only replace the missing variables and not change the values that already exist. We reshaped from long to wide and back again, and that did the trick, but I was wondering if there was another way. I was thinking it could be done using a conditional statement, but the code that I have written doesn't maintain the values that already exist. Which values go where doesn't matter, as long as wallet #1, for example, reads 6 7 and then any combination of the remaining values 1 2 3 4 5. Likewise for wallet #2.

This is the code I was playing around with:

by wallet, sort: replace month = cond(missing(month) & month != wallet[_n], _n, .)

This is the data:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(wallet month)
1 6
1 7
1 .
1 .
1 .
1 .
1 .
2 .
2 .
2 .
2 .
2 3
2 4
2 5
end