[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: Replacing missing with valid values
It's not exactly clear what your example data is supposed to show--the
average of mf1 and ff1 for id==2 is 3.5, not 2. Are these not the
values you are intending to average? Are the suffixes on variables mf
and ff supposed to be rank (what if two male friends are tied on the
ranking variable? does it never happen in the data?). Presumably, you
want to keep person id's, not just rank used in the average, anyway.
All of what you want to do is probably easier with -reshape- e.g.
input id mf1 mf2 mf3 mf4 mf5 ff1 ff2 ff3 ff4 ff5 newvar
1 . . 3 1 4 . . . . . 3
2 2 4 3 4 5 5 2 1 1 2 2
3 3 . . . . 4 . . . . 3.5
4 . 1 1 3 3 1 4 3 3 1 1
5 . . . . 4 . . . . 2 3
6 5 2 4 . . 1 . . . . 3
reshape long mf ff, i(id) j(rank)
gen mok=rank if mf<.
bys id (mok): g cv=mf
by id: g used=rank
by id: g fsex="m"
gen fok=rank if ff<.
bys id (fok): replace cv=ff if rank<used & ff<.
by id: replace used=rank if rank<used & ff<.
by id: replace fsex="f" if rank<used & ff<.
by id: replace cv=(ff+cv)/2 if rank==used & ff<.
by id: replace fsex="mf" if rank==used & ff<.
drop mok fok
reshape wide mf ff, i(id) j(rank)
li, noo clean
The "used" variable now captures the rank used to create "cv" and the
"fsex" variable contains the sex of the friend used to make "cv" and
contains "mf" in the case that male and female friends are averaged
The approach is pretty general, so it should be easy to extend to the
other variables you left out of the example.
On 6/5/07, Carter Rees <email@example.com> wrote:
The value of the created variable for id 1 would be equal to that of mf3
(3). Id = 2 would be equal to the average of mf1 and ff1 (2). I am fairly
confident that I this is something I can program myself using a combination
of -replace- and if statements.
* For searches and help try: