Statalist The Stata Listserver


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

FW: st: Replacing missing with valid values


From   "Carter Rees" <[email protected]>
To   <[email protected]>
Subject   FW: st: Replacing missing with valid values
Date   Tue, 5 Jun 2007 16:23:29 -0400

Austin,

My apologies for missing the FAQ; duly noted.  Thanks for the help.

Carter 

-----Original Message-----
From: Austin Nichols [mailto:[email protected]] 
Sent: Tuesday, June 05, 2007 4:08 PM
To: [email protected]
Subject: Re: st: Replacing missing with valid values

Carter--
Note the FAQ admonition to make replies public unless there is a
compelling reason not to.  You have found a bug in my code; you could
find the source of it by putting a -list- command in between other
commands to see what they are doing to the data.

Here is a better version of my old code that assigns fsex before cv
and limits the assignment of "mf" to fsex to those cases not already
assigned an "f" by the previous code.  There's a better way to do this
with 2 calls to -reshape- so both mf and ff become f, with another
variable sex taking values "m" and "f" or 1 and 0--see if you can sort
that out as an exercise.

clear
input id mf1 mf2 mf3 mf4 mf5 ff1 ff2 ff3 ff4 ff5
1 . . 3 1 4 . . . . .
2 2 4 3 4 5 5 2 1 1 2
3 3 . . . . 4 . . . .
4 . 1 1 3 3 1 4 3 3 1
5 . . . . 4 . . . . 2
6 5 2 4 . . 1 . . . .
7 . . . . . . 2 . . .
8 1 . . . . . . . . .
9 . . . . . . . 3 . .
10 . . . . . 1 . . . .
end
reshape long mf ff, i(id) j(rank)
gen mok=rank if mf<.
bys id (mok): g cv=mf[1]
by id: g fsex="m" if mf[1]<.
by id: g used=rank[1] if mf[1]<.
gen fok=rank if ff<.
bys id (fok): replace cv=ff[1] if rank[1]<used & ff[1]<.
by id: replace fsex="f" if rank[1]<used & ff[1]<.
by id: replace used=rank[1] if rank[1]<used & ff[1]<.
li if id==4 | id==10, noo sepby(id)
by id: replace fsex="mf" if rank[1]==used & ff[1]<. & fsex!="f"
by id: replace cv=(ff[1]+cv)/2 if rank[1]==used & ff[1]<. & fsex!="f"
li if id==4 | id==10, noo sepby(id)
drop mok fok
reshape wide mf ff, i(id) j(rank)
aorder
li, noo clean

The "rest of the group" average can be calculated in the long form of
the data or the wide form, but it's probably easier in the long form,
and easiest in the twice-reshaped "extra-long" data.

On 6/5/07, Carter Rees <[email protected]> wrote:
> Austin,
>
> Thank you for the help.  The code seems to be working fine except for
cases
> in which females are the only friends who are reporting valid values.  I
> have added a couple of test cases after id = 6.    The actual value of cv
is
> correct along with the used variable but fsex returns a value of mf in
cases
> where it should only be f.
>
> As a final step, I would like to create a "rest of the group" average
based
> upon all remaining friends not used for the generic best friend values.
> Based upon what you have done here it seems possible but I am not clearly
> seeing the solution.

*
*   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