Statalist The Stata Listserver


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

st: RE: combining two variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: combining two variables
Date   Fri, 3 Feb 2006 00:45:23 -0000

You should show your code and the results produced, 
as suggested in the Statalist FAQ. That way, you would learn, ideally, 
not only how to do it but also where you went wrong. 

To me, this sounds like 

foreach e in school size score { 
	gen c_`e' = max(a_`e', b_`e') 
} 

However, you should test your own assertion. You are 
asserting that no two non-missing values of the a
and b variables are ever different. We should build
that into the loop. 

foreach e in school size score { 
	assert a_`e' == b_`e' if !missing(a_`e', b_`e') 
	gen c_`e' = max(a_`e', b_`e') 
} 

Nick 
[email protected] 

Daphna Bassok
 
> i have a data structure question, and i could use some advice.
> 
> suppose i have two variables say a_school and b_school.  the 
> values of 
> these two variables are either the same, or one of the two might 
> be  missing. i would like to somehow merge these into a 
> single variable- 
> say c_school.
> 
> if i only had to do this once, it would be quite simple.
> 
> gen c_school=b_school
> replace  c_school=a_school if c_school==.
> 
> done.
> 
> however, i have to do this repeatedly.  as in... i have many of these 
> pairs. a_size, b_size... a_score, b_score...etc.
> 
> i have been trying to figure out a way to do this with the 
> foreach command.
> 
> but it doesn't seem to be working out.

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