Statalist The Stata Listserver

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

Re: st: combining two variables

From   "Austin Nichols" <>
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:
input  var1   var2
      .      2
      .      8
      .      0
      7      .
      3      .
      .      .
      3      4
gen newvar1=max(var1,var2)
gen newvar2=min(var1,var2)
assert newvar1==newvar2
l var? newvar? if newvar1!=newvar2

Paswel Phiri Marenya <> 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 <> 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 <> wrote:
gen newvar=var1
replace newvar=var2 if newvar==.
ricardo sierra <> wrote:
egen newvar=rowtotal(var1 var2)
replace newvar=. if var1==. & var2==. 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:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index