Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Creating variables with values taken from other observations


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Creating variables with values taken from other observations
Date   Tue, 10 Aug 2010 20:10:54 +0100

Another way to tackle it: 

gen FGender = . 

qui forval i = 1/`=_N' { 
	su NGender if Rid == Fid[`i'], meanonly 
	replace FGender = r(min) in `i'
} 

See also -countmatch- from SSC, especially the discussion embedded in
its help file. 

This is clumsy, but the principle is worth knowing. I am taking
-NGender- to be numeric, which you can ensure using -encode- even if
-Gender- is string. 

With strings there is another work-around. 

gen FGender = "" 
gen long obs = _n 

qui forval i = 1/`=_N' { 
	su obs if Rid == Fid[`i'], meanonly 
	replace FGender = Gender[r(min)] in `i'
}

See also 

SJ-6-4  dm0025  . . . . . . . . . .  Stata tip 36: Which observations?
Erratum
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        Q4/06   SJ 6(4):596                              (no commands)
        correction of example code for Stata tip 36

SJ-6-3  dm0025  . . . . . . . . . . . . . .  Stata tip 36: Which
observations?
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        Q3/06   SJ 6(3):430--432                                 (no
commands)
        tip for identifying which observations satisfy some
        specified condition

Nick 
n.j.cox@durham.ac.uk 

Mark McCann

I've worked out a way to do this using excel, but I'd much prefer to
keep everything in Stata instead of jumping back and forward every time
I generate a variable along these lines.

My data has three variables 

Respondent ID  -  Friend ID - Respondent Characteristic

The best friend ids all correspond to another respondent's ID. 

I want to create a variable - best friend's characteristic.

For each observation I want to take that observation's friend ID, find
the observation that has this friend id value as its value for
respondent ID, read this observation's characteristic value and create a
new variable taking the characteristic's value for the original
observation.

Question again in table format because I know that description wasn't
very clear


I have data like this

Rid	Fid	Gender

1	2	Female
2	5	Male
5	7	Missing
.	.	.
.	.	.


How do I create this?


Rid	Fid	Gender		Fgender

1	2	Female		Male
2	5	Male		Missing
5	7	Missing	etc.
.	.	.		.
.	.	.		.

Is there a way to do this in Stata? I used gender as a reader-friendly
example however I will be using predominantly numeric coding.


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index