Statalist The Stata Listserver


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

Re: st: combining two variables


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: combining two variables
Date   Wed, 7 Mar 2007 15:19:49 -0500

Taking my concern about robustness to unforeseen data problems as read,
these commands are very easy to try out...

clear
set obs 8
forv i=1/7 {
g v`i'=uniform() if _n==`i'
}
li
g vmax=max(v1,v2,v3,v3,v4,v5,v6,v7)
egen vrowmax=rowmax(v1-v7)
li, noo clean

On 3/7/07, Austin Nichols <[email protected]> wrote:
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 <[email protected]> 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 <[email protected]> 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 <[email protected]> wrote:
> gen newvar=var1
> replace newvar=var2 if newvar==.

ricardo sierra <[email protected]> wrote:
> egen newvar=rowtotal(var1 var2)
> replace newvar=. if var1==. & var2==.

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