Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Using 2 vars to make 1 in presence of missing

From   SamL <[email protected]>
To   Stata Listserve <[email protected]>
Subject   st: Using 2 vars to make 1 in presence of missing
Date   Sun, 17 Oct 2004 21:05:43 -0700 (PDT)

Maybe my head has been around the problem so long, looking at the manual,
other code, and such, that I just cannot see it.  I have two variables, a
and b, and 1.5 datasets, A and B.  All cases in B are in A, but some cases
in A are not in B.  I want to merge the two datasets, matching on the id.

Variable a is missing for some cases in A, but the same variable has valid
values for some of those cases in B (as b).

I have tried several approaches.  What I want is:

do if (a ne .)
compute c=a
else if (b ne .)
compute c=b
compute c=.
end if

I know its old spss syntax, but basically I want to use the value in
variable a unless it is missing, in which case use the value in variable

I've tried recoding missing to a negative valid value and using gen as

gen c=max(a,b)

but that also sometimes ends up using a "b" value when a is missing but b
is not really valid for the case.  (long story, details not relevant, I'll
suppress unless asked).

I tried egen but it doesn't seem willing to let me say something like:

calculate c=a, unless case not in A, then calculate c=b .

I've tried various syntactical approaches and none seem to work; I won't
bore you with a long set of tweaked but failed code.

*                                     *                              *

Maybe I have, as I said, just gotten completely confused--there are about
half a dozen such situations in these datasets.  I've spent the weekend
cleaning the data and these are the final issues that remain, and maybe
I'm just missing it--well, it is kind of clear I am missing it.

Any assistance is greatly appreciated.


*   For searches and help try:

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