Statalist


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

st: RE: RE: RE: RE: RE: RE: copy values


From   "Victor, Jennifer Nicoll" <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: RE: RE: RE: RE: RE: copy values
Date   Thu, 11 Dec 2008 15:41:50 -0500

Great.  Thanks for making that clear.

___________________________________________
Jennifer Nicoll Victor
Assistant Professor
Department of Political Science
University of Pittsburgh
4600 Wesley W. Posvar Hall
(412) 624-7204
E-mail:  [email protected]
Homepage:  http://www.pitt.edu/~jnvictor


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Thursday, December 11, 2008 3:28 PM
To: [email protected]
Subject: st: RE: RE: RE: RE: RE: copy values

Not so. There is absolutely no need to loop. 

I am not completely sure of the best -sort- order but some variant on 
this 

bysort time state (district) : ///
replace `var' = `var'[_n-1] if missing(`var') 

will be fine. 

The point is explicit in the FAQ. The general idea is that 

bysort <avars> (<bvars>) : 

gives you the complete control you need. Stata is looping internally, as
it always does, but you need not do that yourself. 

Nick 
[email protected] 

Victor, Jennifer Nicoll

Good idea.  However, I think your first idea is preferable because the
second idea forces me to loop over time (I cannot sort by state without
corrupting the time element).  It seems your first suggestions maintains
simplicity which still ensuring a check.  

I used:

replace `var' = `var'[_n-1] if `var' >= . & state==state[_n-1]

with a foreach command.

Nick Cox

That's good to hear, but such code risks copying across states, which I
guess you do not want. 

Adding 

... if state == state[_n-1] 

is one way to ensure that, given sensible sort order. Doing things -by
state- is an even better way. 

Nick 
[email protected] 

Victor, Jennifer Nicoll

Thanks.  The following command worked perfectly.  Sorry I didn't look
for it in the FAQs.

replace blackstate = blackstate[_n-1] if blackstate >= .

Nick Cox

This is an FAQ. 

FAQ     . . . . . . . . . . . . . . . . . . . . . . . Replacing missing
values
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        2/03    How can I replace missing values with previous or
                following nonmissing values?
                http://www.stata.com/support/faqs/data/missing.html

-- apart from the twist about -drop-ping values which is standard stuff.

Victor, Jennifer Nicoll

I have a time series panel that has missing values.  I want to copy,
duplicate, replace, or otherwise fill-in values in one variable with
other values in that variable.

For example, suppose I have variables time, state, district,
blackpopdist (black population in that district) and blackpopstate
(black population in that state.  My unit of analysis is district, so
states are listed as district=0.  I have blackpopdist for each
district>0, but missing for district==0.  I have blackpopstate for each
district==0, but missing otherwise.  I want to ultimately delete the
observations where district =0. Before I do so, I want my blackpopstate
variable to take on constant values for that state, for how ever many
districts are in it.  Therefore, for each year-state, I want to
duplicate the first value in blackpopstate to fillin the other missing
value for the rest of the districts in that year-state.  Then drop the
district=0 observation.

A sample starting dataset:
Time	state	dist	blackpopdist	blackpopstate
92	AL	0	.		1234
92	AL	1	234		.
92	AL	2	456		.
92	CA	0	.		4567
92	CA	1	2345		.
92	CA	2	12		.
93	AL	0	.		1235
93	AL 	1	235		.

A sample complete dataset:
Time	state	dist	blackpopdist	blackpopstate
92	AL	1	234		1234
92	AL	2	456		1234
92	CA	1	2345		4567
92	CA	2	12		4567
93	AL 	1	235		1235


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

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index