Statalist The Stata Listserver


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

Re: st: combining two variables


From   "Austin Nichols" <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: combining two variables
Date   Wed, 7 Mar 2007 11:55:57 -0500

Eleanor Friedman et al.--
I don't think any of the approaches below are robust
to unexpected twists in the data, something we
should all be careful of as a rule. Just to be
sure the data is as expected, I would:
clear
input  var1   var2
      .      2
      .      8
      .      0
      7      .
      3      .
      .      .
      3      4
end
gen newvar1=max(var1,var2)
gen newvar2=min(var1,var2)
assert newvar1==newvar2
l var? newvar? if newvar1!=newvar2

Paswel Phiri Marenya <ppm3@cornell.edu> wrote:
What to me is easier is something like:
gen var3 = var2
then replace var3=var1 in 4/5 and so on...although with a long data set it
may be tedious perhaps.
Andrew H. Sidman <asidman@ic.sunysb.edu> wrote:
While there is probably a faster way, I gen
temporary variables and recode the missing:
gen var1a = var1
gen var2a = var2
recode var1a var2a (. = 0)
gen newvar = var1a + var2a
drop var1a var2a
Philipp Rehm <philipp.rehm@gmx.de> wrote:
gen newvar=var1
replace newvar=var2 if newvar==.
ricardo sierra <ricardo.sierra@gmail.com> wrote:
egen newvar=rowtotal(var1 var2)
replace newvar=. if var1==. & var2==.

nora@uchicago.edu wrote:
Now I have a question about combining the data from two
variables (in the same data set) into one variable. I want to
combine the variables in such a way that the data from one
variable can replace the missing values from the other. I have
created an example of what I am looking for below:
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index