Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How check if value in one variable is equal to any value in other variables

    I am trying to gen a variable or variables that say VAR1 has elements in commons with other 2 variables (VAR2 and VAR3), VAR2 has element in common with only one variable (VAR4), VAR 3 has elements in common with only VAR1 and VAR4 has elements in common with only one variable VAR2. I have to do this by place.

    Is there any way to do this in Stata?
    Thanks.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str51 place float(VAR1            VAR2                    VAR3                    VAR4)
    "SAN LEANDRO"      1996 . . .
    "SAN LEANDRO"      1997 . . .
    "SAN LEANDRO"      1998 . . .
    "SAN LEANDRO"      1999 . . .
    "SAN LEANDRO"           . . . .                                 1994
    "SAN LEANDRO"            . . . .                                1995
    "SAN LEANDRO"             . . . .                               1996
    "SAN LEANDRO""             . . . .                              1997
    "SAN LEANDRO"" . . . .                   1998
    "SAN LEANDRO""  . . . .                  1999
    "SAN LEANDRO""   . . . .                  2000
    "SAN LEANDRO""       . . . .              2001     
    "SAN LEANDRO""           . . . .                                                          2000  
    "SAN LEANDRO""            . . . .                                                         2001
    "SAN LEANDRO""             . . . .                                                        2002
    "SAN LEANDRO""              . . . .                                                       2003 
    end

  • #2
    I'm not sure of a good way to do this. You might just set up loops that loop over all the values in each variable and compare them to values in other variables.

    Here's a way to do it. By the way, your posted code has too many . . . to work and "" in some places - I don't know how you got this but it meant I had to waste time cleaning the junk before I could help you.

    Code:
    clear
    input str51 place float(VAR1            VAR2                    VAR3                    VAR4)
    "SAN LEANDRO"      1996 . . .
    "SAN LEANDRO"      1997 . . .
    "SAN LEANDRO"      1998 . . .
    "SAN LEANDRO"      1999 . . .
    "SAN LEANDRO"       .  .                                     1994 .
    "SAN LEANDRO"            . .                                 1995 .
    "SAN LEANDRO"             . .                                1996 .
    "SAN LEANDRO"             . .                              1997 .
    "SAN LEANDRO" .                    1998 . .
    "SAN LEANDRO"  .                   1999 . .
    "SAN LEANDRO"   .                   2000 . .
    "SAN LEANDRO"       .              2001  . .   
    "SAN LEANDRO"           . . .                                                           2000  
    "SAN LEANDRO"            . . .                                                         2001
    "SAN LEANDRO"             . .  .                                                        2002
    "SAN LEANDRO"              . . .                                                       2003 
    end
    save tempA,replace
    forvalues i=1/4 {
    use tempA,clear
    list
        g year=VAR`i'
    keep place VAR`i' year
    list
    drop if VAR`i'==.
    list
    save tempVAR`i',replace
    }
    clear
    set obs 20
    g place = "SAN LEANDRO"
    g year=1995 + _n
    
    forvalues i=1/4 {
    merge 1:1 place year using tempVAR`i'
    drop _merge
    }
    
    g match12=(VAR1 - VAR2)==0
    You can move the 1's to the other values of place using bysort place: egen mat=mean(match12)

    Comment


    • #3
      Thank you, it worked.
      I have now another issue, I am doing a loop to compare the variables among them but this loop is comparing each variable with itself and I just need it to compare each variable with the other ones, but not itself(I have many variables to compare) . My code is the following one:

      foreach i in A199500002 A199500015 A199500024 A199500040 A199500046
      bysort Zplace: g match`i'=(`i'- `i')==0
      }

      I don't know how can I solve this.
      Last edited by Fayza Azz; 05 Feb 2020, 09:18.

      Comment

      Working...
      X