Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: match values of several variables

From   Maarten buis <>
Subject   Re: st: match values of several variables
Date   Fri, 23 Apr 2010 02:03:10 -0700 (PDT)

--- On Fri, 23/4/10, Cris Dogaru wrote:
> I have a longitudinal data in which they asked, at
> different data collection points, the year of birth for
> the subject's mother; the variables are named based on the
> year of data collection (i.e., birthyear_98, birthyear_99,
> etc). One would expect the values of these varables to
> match (if it's the same person), but they don't. I'd like
> to find an easy way to check if several variables have the
> same value or not, irrespective of the missing values. 

In the double loop below I move over every combination of 
mother's birthyears. If you add another wave, you would add
a variable mbyr4, and you would change the 3 in a 4 in the
two -forvalues- commands. For each of these combinations I
looked at whether one or both mbyrs are missing, and if that
is the case I assign temp a 1, otherwise I assing temp a 1
if both mbyrs are the same and a 0 if they are different.
I then multiply it witht the variable chk, so chk will only
be 1 if all reported mbyrs are equal to one another, excluding
missing values.

*------------- begin example -------------
input mbyr1 mbyr2 mbyr3
      1950  1950  1950
      1947  1948  1947
      1945  .     1945
      1945  1946  .
      1945  .     1946
gen byte chk = 1
gen byte temp = 1
forvalues i = 1/3 {
    forvalues j = `=`i'+1' / 3 {
        replace temp = cond(missing(mbyr`i', mbyr`j'), ///
                            1, mbyr`i' == mbyr`j')
        replace chk = chk*temp
drop temp
*--------------- end example --------------------
(For more on examples I sent to the Statalist see: 

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen


*   For searches and help try:

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