Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Replacing missing with valid values


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Replacing missing with valid values
Date   Tue, 5 Jun 2007 11:28:38 -0400

Carter Rees--
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.

clear
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
end
reshape long mf ff, i(id) j(rank)
gen mok=rank if mf<.
bys id (mok): g cv=mf[1]
by id: g used=rank[1]
by id: g fsex="m"
gen fok=rank if ff<.
bys id (fok): replace cv=ff[1] if rank[1]<used & ff[1]<.
by id: replace used=rank[1] if rank[1]<used & ff[1]<.
by id: replace fsex="f" if rank[1]<used & ff[1]<.
by id: replace cv=(ff[1]+cv)/2 if rank[1]==used & ff[1]<.
by id: replace fsex="mf" if rank[1]==used & ff[1]<.
drop mok fok
reshape wide mf ff, i(id) j(rank)
aorder
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
together.

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 protected]> 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:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index