Announcement

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

  • Comparison of values across variables

    Dear All

    I would like to check whether the values contained in the variable id, correspond to those in id1. In case any value in id is not included in id1 I need have a new variable that spots such a difference.
    here an example
    input int(id id1)
    2831 2965
    2831 3509
    2832 2838
    2832 3230
    2833 3042
    2833 3187
    2834 2888
    2834 3189
    2835 3265
    2836 2850
    2836 3156
    2837 2847
    2837 3034
    2838 2842
    2838 2857
    2839 2904
    2839 3611
    2840 3090
    2840 3308
    2841 2845
    2842 2871
    2843 2844
    2843 2892
    2844 3535
    2845 3143
    2846 2841
    2846 3431
    2847 3062
    2848 2881
    2849 3307
    2849 3603
    2850 3400
    2851 2905
    2851 3244
    2852 2854
    2852 2869
    2853 2877
    2853 3416
    2854 3659
    2855 2854
    2855 3230
    2856 2854
    2856 3125
    2857 2845
    2857 3503
    2858 3655
    2859 2936
    2860 3154
    2861 2885
    2861 3619
    end
    [/CODE]

    I tried this:
    Code:
    gen check=0
    levelsof id, local (ids) 
    levelsof id1 , local (id1s)
    foreach id in `ids'{
        foreach id1 in `id1s'{
        replace check=1 if "`ids'"=="`id1s'" 
        }
    }
    but returns all 0, which is not the case.
    Anyone can help?
    Thanks
    Federica

  • #2
    That seems a lot of loops to go through, not very efficient.

    I'd suggest saving a file with unique values in id1, and then merge back to this fine matching by id. That should be faster. Here is some reference code:

    Code:
    preserve
    keep id1
    rename id1 id
    bysort id: keep if _n == 1
    gen has_match_in_id1 = 1
    tempfile id1_list
    save `id1_list', replace
    restore
    
    merge m:1 id using `id1_list'
    drop if _merge == 2
    drop _merge

    Comment


    • #3
      Code:
      g notsame = id!=id1
      tab id if notsame

      Comment

      Working...
      X