Announcement

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

  • How to merge two datasets and How to use keep

    Hello everyone!

    I'm a very beginner using STATA so please I'd like to get some help with merge command,

    My purpose is to add variables to the master dataset and keep only what is matched related to a particular observation, so I've been told I must use merge command (I'm using merge in STATA 17). My master dataset has more observations than my using dataset, but they both have in common two variables householdnumber and household_number_member. I was suggested to use all common variables when merging, not just one in common.

    However, in the master dataset the householdnumber variable has repeated observations. In other words, appears multiple times the same household number referring to each member of the household, who will be identified by household_number_member variable. Thus, household_number_member is identifed as 1= marriage immigrant, 2= national spouse, 3= children, etc. On the other hand, the using dataset doesn't have repeated observations in the variable householdnumber, because this dataset only uses in the household_number_member variable the observation 1 (which means, 1= marriage immigrant) that's why it has less observations.

    By using merge, I'm only interested in keeping all related to 1 (which means 1=marriage immigrant) and adding variables that appear in the using dataset.

    To make myself clear, and to get proper help. I'm using dataex command as follows (the following code corresponds to my master dataset)

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long householdnumber byte household_number_member
    10000028 1
    10000028 2
    10000031 1
    10000031 2
    10000062 1
    10000068 1
    10000068 2
    10000124 1
    10000124 2
    10000140 1
    10000140 2
    10000186 1
    10000187 1
    10000187 2
    10000213 1
    10000232 1
    10000232 3
    10000270 1
    10000270 2
    10000292 1
    10000293 1
    10000293 2
    10000315 1
    10000315 2
    10000328 1
    10000328 2
    10000344 1
    10000393 1
    10000404 1
    10000404 2
    10000415 1
    10000415 2
    10000419 1
    10000419 2
    10000422 1
    10000422 2
    10000455 1
    10000455 2
    10000492 1
    10000492 2
    10000497 1
    10000497 2
    10000533 1
    10000533 2
    10000549 1
    10000556 1
    10000556 2
    10000556 3
    10000556 4
    10000556 21
    10000592 1
    10000592 2
    10000592 3
    10000592 21
    10000601 1
    10000601 21
    10000608 1
    10000608 2
    10000615 1
    10000658 1
    10000658 2
    10000700 1
    10000700 2
    10000700 3
    10000712 1
    10000712 2
    10000712 3
    10000713 1
    10000713 2
    10000764 1
    10000771 1
    10000771 2
    10000774 1
    10000774 2
    10000787 1
    10000855 1
    10000855 2
    10000894 1
    10000894 2
    10000894 3
    10000902 1
    10000902 2
    10000952 1
    10000952 2
    10000952 21
    10000970 1
    10000970 2
    10001003 1
    10001003 2
    10001004 1
    10001004 2
    10001069 1
    10001069 2
    10001142 1
    10001193 1
    10001193 2
    10001193 3
    10001193 4
    10001200 1
    10001200 2
    end


    The following code corresponds to my using dataset:

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long householdnumber byte household_number_member
    10000028 1
    10000031 1
    10000062 1
    10000068 1
    10000124 1
    10000140 1
    10000186 1
    10000187 1
    10000213 1
    10000232 1
    10000270 1
    10000292 1
    10000293 1
    10000315 1
    10000328 1
    10000344 1
    10000393 1
    10000404 1
    10000415 1
    10000419 1
    10000422 1
    10000455 1
    10000492 1
    10000497 1
    10000533 1
    10000549 1
    10000556 1
    10000592 1
    10000601 1
    10000608 1
    10000658 1
    10000700 1
    10000712 1
    10000713 1
    10000764 1
    10000771 1
    10000774 1
    10000787 1
    10000855 1
    10000894 1
    10000902 1
    10000952 1
    10000970 1
    10001003 1
    10001004 1
    10001069 1
    10001142 1
    10001193 1
    10001200 1
    10001214 1
    10001292 1
    10001332 1
    10001334 1
    10001353 1
    10001425 1
    10001426 1
    10001434 1
    10001445 1
    10001482 1
    10001523 1
    10001609 1
    10001690 1
    10001720 1
    10001775 1
    10001782 1
    10001827 1
    10001865 1
    10001883 1
    10001924 1
    10001959 1
    10001969 1
    10002006 1
    10002084 1
    10002150 1
    10002174 1
    10002193 1
    10002227 1
    10002311 1
    10002325 1
    10002403 1
    10002409 1
    10002437 1
    10002498 1
    10002511 1
    10002568 1
    10002580 1
    10002592 1
    10002595 1
    10002600 1
    10002620 1
    10002714 1
    10002730 1
    10002732 1
    10002749 1
    10002816 1
    10002840 1
    10002870 1
    10002941 1
    10002970 1
    10003085 1
    end

    I've been using the command as follows, but STATA issued an error message saying: factor-variable and time-series operators not allowed

    merge 1:1 householdnumber household_number_member "filename", keep if household_member_number==1


    Any help would be very much appreciated.

  • #2
    Your description of what you want to end up with isn't entirely clear. The following code will leave you with a data set containing all observations from the master data set and, for those with household_member_number == 1 that also have a match in the using data set, their information from using.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long householdnumber byte household_number_member
    10000028 1
    10000028 2
    10000031 1
    10000031 2
    10000062 1
    10000068 1
    10000068 2
    10000124 1
    10000124 2
    10000140 1
    10000140 2
    10000186 1
    10000187 1
    10000187 2
    10000213 1
    10000232 1
    10000232 3
    10000270 1
    10000270 2
    10000292 1
    10000293 1
    10000293 2
    10000315 1
    10000315 2
    10000328 1
    10000328 2
    10000344 1
    10000393 1
    10000404 1
    10000404 2
    10000415 1
    10000415 2
    10000419 1
    10000419 2
    10000422 1
    10000422 2
    10000455 1
    10000455 2
    10000492 1
    10000492 2
    10000497 1
    10000497 2
    10000533 1
    10000533 2
    10000549 1
    10000556 1
    10000556 2
    10000556 3
    10000556 4
    10000556 21
    10000592 1
    10000592 2
    10000592 3
    10000592 21
    10000601 1
    10000601 21
    10000608 1
    10000608 2
    10000615 1
    10000658 1
    10000658 2
    10000700 1
    10000700 2
    10000700 3
    10000712 1
    10000712 2
    10000712 3
    10000713 1
    10000713 2
    10000764 1
    10000771 1
    10000771 2
    10000774 1
    10000774 2
    10000787 1
    10000855 1
    10000855 2
    10000894 1
    10000894 2
    10000894 3
    10000902 1
    10000902 2
    10000952 1
    10000952 2
    10000952 21
    10000970 1
    10000970 2
    10001003 1
    10001003 2
    10001004 1
    10001004 2
    10001069 1
    10001069 2
    10001142 1
    10001193 1
    10001193 2
    10001193 3
    10001193 4
    10001200 1
    10001200 2
    end
    tempfile master
    save `master'
    
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long householdnumber byte household_number_member
    10000028 1
    10000031 1
    10000062 1
    10000068 1
    10000124 1
    10000140 1
    10000186 1
    10000187 1
    10000213 1
    10000232 1
    10000270 1
    10000292 1
    10000293 1
    10000315 1
    10000328 1
    10000344 1
    10000393 1
    10000404 1
    10000415 1
    10000419 1
    10000422 1
    10000455 1
    10000492 1
    10000497 1
    10000533 1
    10000549 1
    10000556 1
    10000592 1
    10000601 1
    10000608 1
    10000658 1
    10000700 1
    10000712 1
    10000713 1
    10000764 1
    10000771 1
    10000774 1
    10000787 1
    10000855 1
    10000894 1
    10000902 1
    10000952 1
    10000970 1
    10001003 1
    10001004 1
    10001069 1
    10001142 1
    10001193 1
    10001200 1
    10001214 1
    10001292 1
    10001332 1
    10001334 1
    10001353 1
    10001425 1
    10001426 1
    10001434 1
    10001445 1
    10001482 1
    10001523 1
    10001609 1
    10001690 1
    10001720 1
    10001775 1
    10001782 1
    10001827 1
    10001865 1
    10001883 1
    10001924 1
    10001959 1
    10001969 1
    10002006 1
    10002084 1
    10002150 1
    10002174 1
    10002193 1
    10002227 1
    10002311 1
    10002325 1
    10002403 1
    10002409 1
    10002437 1
    10002498 1
    10002511 1
    10002568 1
    10002580 1
    10002592 1
    10002595 1
    10002600 1
    10002620 1
    10002714 1
    10002730 1
    10002732 1
    10002749 1
    10002816 1
    10002840 1
    10002870 1
    10002941 1
    10002970 1
    10003085 1
    end
    tempfile using
    save `using'
    
    use `master', clear
    merge 1:1 householdnumber household_number_member using `using', keep(match master)
    However, perhaps you don't want to keep those from the master data set who have no matching observation in the using data set (which entails also losing those in the master data set with household_member_number != 1). In that case, replace -keep(match master)- with -keep(match)-. Then you will be left only with those where household_member_number == 1 and a match in the using data set.

    Note that the keyword using must appear in the -merge- command before the name of the using data set, even if the using data set itself is called using.

    Note that the -merge- command does not allow -if- qualifiers. In your case, since the using data set has household_number_member == 1 in all of its observations, no such specification is needed anyway-- only observations in the master data with household_number_member == 1 will find a match. If it were not the case that household_number_member == 1 in all observations of the using data set, you could impose that restriction in a separate command after the -merge-, as -keep if household_number_member == 1-.

    Thank you for using -dataex- on your very first post!

    Added: And if you were in a situation where the using data set didn't mention household_number_member (which would, often as not, be true as it is common to eliminate variables that only take on a single value. Then you would code -merge m:1 household_number using `using', keep(match master)-, or perhaps with -keep(match)- if you only wanted matching observations.
    Last edited by Clyde Schechter; 17 Oct 2021, 15:11.

    Comment


    • #3
      Sorry to trouble everyone but I have a problem. I have one dataset with 50 cases (states) and another with senators' votes. They are too large to use dataex to upload them. I want to combine them into a single data set with 100 cases. I tried this command with the outcome as follows:

      merge m:1 state using D:\Data\DACArollcallnov2025.dta
      variable state does not uniquely identify observations in the using data

      What did I do wrong? Thanks,
      Ric Uslaner

      Comment


      • #4
        first, even a small sample of each of the data sets would be helpful

        second, my guess is that you wanted 1:m rather than m:1 but without sample data, or at least a better description, this can be no more than a guess

        Comment


        • #5
          Here are some data. From file 1:

          save "D:\Data\DACArollcallnov2025.dta"
          file D:\Data\DACArollcallnov2025.dta saved

          . clist dacavote state senator in 1/5

          dacavote state senator
          1. 1 TN Alexander
          2. 1 NH Ayotte
          3. 1 WI Baldwin
          4. 1 MT Baucus
          5. 1 AK Begich

          From file 2:

          . clist state pid2 christiannationalism in 1/10

          state pid2 christ~m
          1. Alabama 94 2.65753
          2. Alaska 22.8 3.16422
          3. Arizona 67.4783 3.36301
          4. Arkansas 63.5217 2.57143
          5. California 410 3.34286
          6. Colorado 90.7391 3
          7. Connecticut 96.087 3
          8. Delaware 16.8696 2.87097
          9. Florida 233.826 2.94898
          10. Georgia 133.826 2.70455

          Hope this helps.

          Comment


          • #6
            I agree with Rich Goldstein that you almost surely need 1:m, not m:1 here because in file2 each state appears to be mentioned once, whereas in D:\Data\DACArollcallnov2025.dta, each state will appear twice (once for each of the state's two senators).

            But you have another problem. The variable state, in its current form, cannot serve as a merge key for these data sets because it is differently specified. That is, -merge will not understand that TN is Tennessee, etc. So all of the observations in both data sets will fail to match. You will need to either add a full state name variable to D:\Data\DACArollcallnov2025.dta, or add a two-letter code variable to file 2, and rename the variables so they match properly before you can do this -merge-.

            Comment


            • #7
              Thanks. Ultimately the merge didn't work because there are multiple senators from some states due to replacement. I thus had to copy the new state variables into the roll call data base and that worked.

              Comment


              • #8
                Sorry to trouble you again. I have entered the state names as suggested. I have two data files I need to merge so I am requesting help on how to merge them.

                The files are:
                D:\Data\statedataaggjune2024revisednew.dta
                and D:\Data\congressionalunity.dta

                . dataex state pid2 christiannationalism

                ----------------------- copy starting from the next line -----------------------
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input long state double pid2 float christiannationalism
                51                 94 2.6575344
                52               22.8  3.164216
                53  67.47826086956522  3.363014
                54  63.52173913043478 2.5714285
                55                410  3.342857
                56  90.73913043478261         3
                57  96.08695652173913         3
                58 16.869565217391305 2.8709676
                59 233.82608695652175 2.9489796
                60 133.82608695652175 2.7045455
                61               21.2 3.0714285
                62 27.652173913043477       3.3
                63  246.8695652173913 2.9382715
                64 138.56521739130434  2.978873
                65  83.34782608695652  3.076923
                66 61.608695652173914         3
                67  59.73913043478261  2.866953
                68   66.6086956521739  3.090909
                69  44.21739130434783         .
                70                 82  3.195122
                71 213.82608695652175  3.185185
                72  244.2173913043478  3.119718
                73 116.69565217391305     3.125
                74  24.47826086956522  2.632653
                75 138.04347826086956 3.1764705
                76  24.47826086956522 2.7956204
                77  36.26086956521739 2.6785715
                78  20.52173913043478       2.9
                79  37.91304347826087     3.625
                80 190.82608695652175  3.147959
                81 28.130434782608695  3.191781
                82  348.3478260869565  3.205128
                83                124         3
                84 20.869565217391305  2.888889
                85  261.9130434782609  3.051948
                86 44.391304347826086 2.4642856
                87   66.3913043478261  3.333333
                88 219.30434782608697  3.076923
                89  35.52173913043478  3.352113
                90  81.26086956521739 3.0143886
                91 12.347826086956522  2.952381
                92 121.78260869565217  2.980392
                93  333.1304347826087  2.764706
                94  34.43478260869565  3.048193
                95  21.08695652173913  3.291667
                96  167.8695652173913      3.25
                97 155.04347826086956  2.948718
                98  33.52173913043478 3.2439024
                99 140.17391304347825   3.15625
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                end
                label values state state
                label def state 51 "Alabama", modify
                label def state 52 "Alaska", modify
                label def state 53 "Arizona", modify
                label def state 54 "Arkansas", modify
                label def state 55 "California", modify
                label def state 56 "Colorado", modify
                label def state 57 "Connecticut", modify
                label def state 58 "Delaware", modify
                label def state 59 "Florida", modify
                label def state 60 "Georgia", modify
                label def state 61 "Hawaii", modify
                label def state 62 "Idaho", modify
                label def state 63 "Illinois", modify
                label def state 64 "Indiana", modify
                label def state 65 "Iowa", modify
                label def state 66 "Kansas", modify
                label def state 67 "Kentucky", modify
                label def state 68 "Louisiana", modify
                label def state 69 "Maine", modify
                label def state 70 "Maryland", modify
                label def state 71 "Massachusetts", modify
                label def state 72 "Michigan", modify
                label def state 73 "Minnesota", modify
                label def state 74 "Mississippi", modify
                label def state 75 "Missouri", modify
                label def state 76 "Montana", modify
                label def state 77 "Nebraska", modify
                label def state 78 "Nevada", modify
                label def state 79 "New Hampshire", modify
                label def state 80 "New Jersey", modify
                label def state 81 "New Mexico", modify
                label def state 82 "New York", modify
                label def state 83 "North Carolina", modify
                label def state 84 "North Dakota", modify
                label def state 85 "Ohio", modify
                label def state 86 "Oklahoma", modify
                label def state 87 "Oregon", modify
                label def state 88 "Pennsylvania", modify
                label def state 89 "Rhode Island", modify
                label def state 90 "South Carolina", modify
                label def state 91 "South Dakota", modify
                label def state 92 "Tennessee", modify
                label def state 93 "Texas", modify
                label def state 94 "Utah", modify
                label def state 95 "Vermont", modify
                label def state 96 "Virginia", modify
                label def state 97 "Washington", modify
                label def state 98 "West Virginia", modify
                label def state 99 "Wisconsin", modify
                ------------------ copy up to and including the previous line ------------------

                Listed 100 out of 117 observations
                Use the count() option to list more

                . dataex state pid2 christiannationalism,count(117)

                ----------------------- copy starting from the next line -----------------------
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input long state double pid2 float christiannationalism
                51                 94 2.6575344
                52               22.8  3.164216
                53  67.47826086956522  3.363014
                54  63.52173913043478 2.5714285
                55                410  3.342857
                56  90.73913043478261         3
                57  96.08695652173913         3
                58 16.869565217391305 2.8709676
                59 233.82608695652175 2.9489796
                60 133.82608695652175 2.7045455
                61               21.2 3.0714285
                62 27.652173913043477       3.3
                63  246.8695652173913 2.9382715
                64 138.56521739130434  2.978873
                65  83.34782608695652  3.076923
                66 61.608695652173914         3
                67  59.73913043478261  2.866953
                68   66.6086956521739  3.090909
                69  44.21739130434783         .
                70                 82  3.195122
                71 213.82608695652175  3.185185
                72  244.2173913043478  3.119718
                73 116.69565217391305     3.125
                74  24.47826086956522  2.632653
                75 138.04347826086956 3.1764705
                76  24.47826086956522 2.7956204
                77  36.26086956521739 2.6785715
                78  20.52173913043478       2.9
                79  37.91304347826087     3.625
                80 190.82608695652175  3.147959
                81 28.130434782608695  3.191781
                82  348.3478260869565  3.205128
                83                124         3
                84 20.869565217391305  2.888889
                85  261.9130434782609  3.051948
                86 44.391304347826086 2.4642856
                87   66.3913043478261  3.333333
                88 219.30434782608697  3.076923
                89  35.52173913043478  3.352113
                90  81.26086956521739 3.0143886
                91 12.347826086956522  2.952381
                92 121.78260869565217  2.980392
                93  333.1304347826087  2.764706
                94  34.43478260869565  3.048193
                95  21.08695652173913  3.291667
                96  167.8695652173913      3.25
                97 155.04347826086956  2.948718
                98  33.52173913043478 3.2439024
                99 140.17391304347825   3.15625
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                 .                  .         .
                end
                label values state state
                label def state 51 "Alabama", modify
                label def state 52 "Alaska", modify
                label def state 53 "Arizona", modify
                label def state 54 "Arkansas", modify
                label def state 55 "California", modify
                label def state 56 "Colorado", modify
                label def state 57 "Connecticut", modify
                label def state 58 "Delaware", modify
                label def state 59 "Florida", modify
                label def state 60 "Georgia", modify
                label def state 61 "Hawaii", modify
                label def state 62 "Idaho", modify
                label def state 63 "Illinois", modify
                label def state 64 "Indiana", modify
                label def state 65 "Iowa", modify
                label def state 66 "Kansas", modify
                label def state 67 "Kentucky", modify
                label def state 68 "Louisiana", modify
                label def state 69 "Maine", modify
                label def state 70 "Maryland", modify
                label def state 71 "Massachusetts", modify
                label def state 72 "Michigan", modify
                label def state 73 "Minnesota", modify
                label def state 74 "Mississippi", modify
                label def state 75 "Missouri", modify
                label def state 76 "Montana", modify
                label def state 77 "Nebraska", modify
                label def state 78 "Nevada", modify
                label def state 79 "New Hampshire", modify
                label def state 80 "New Jersey", modify
                label def state 81 "New Mexico", modify
                label def state 82 "New York", modify
                label def state 83 "North Carolina", modify
                label def state 84 "North Dakota", modify
                label def state 85 "Ohio", modify
                label def state 86 "Oklahoma", modify
                label def state 87 "Oregon", modify
                label def state 88 "Pennsylvania", modify
                label def state 89 "Rhode Island", modify
                label def state 90 "South Carolina", modify
                label def state 91 "South Dakota", modify
                label def state 92 "Tennessee", modify
                label def state 93 "Texas", modify
                label def state 94 "Utah", modify
                label def state 95 "Vermont", modify
                label def state 96 "Virginia", modify
                label def state 97 "Washington", modify
                label def state 98 "West Virginia", modify
                label def state 99 "Wisconsin", modify
                ------------------ copy up to and including the previous line ------------------

                Listed 117 out of 117 observations


                and

                . dataex state congress-unity_score

                ----------------------- copy starting from the next line -----------------------
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input long state int congress float unity_score
                 1 82   95.2381
                 1 82  94.79166
                 3 82  69.30693
                 3 82  90.81633
                 2 82  92.52336
                 2 82  92.45283
                 4 82  76.47059
                 4 82   83.5443
                 5 82  91.30434
                 5 82  67.77778
                 6 82  89.65517
                 6 82     81.25
                 7 82  90.82569
                 7 82  55.14019
                 8 82  80.37383
                 8 82  76.36364
                 9 82   89.3617
                 9 82  76.53061
                13 82  76.08696
                13 82        92
                10 82  94.49541
                10 82  91.50944
                11 82  95.55556
                11 82  58.33333
                12 82  87.83784
                12 82  86.02151
                14 82  90.90909
                14 82  87.73585
                15 82  91.58878
                15 82  88.46154
                16 82 66.037735
                16 82 75.229355
                19 82  67.59259
                19 82  72.81554
                18 82  60.43956
                18 82  91.48936
                17 82  84.33735
                17 82  86.91589
                20 82  84.40367
                20 82  72.22222
                21 82        80
                21 82 75.471695
                23 82  86.79245
                23 82   87.7551
                22 82  75.92593
                22 82 70.833336
                24 82  80.30303
                24 82  77.65958
                31 82        62
                31 82  69.15888
                32 82  64.10256
                32 82  70.37037
                25 82  73.13433
                25 82  88.23529
                27 82  45.67901
                27 82  90.72165
                28 82  69.44444
                28 82  77.77778
                29 82   84.9315
                29 82  84.81013
                26 82 65.333336
                26 82  66.27907
                30 82 73.333336
                30 82  79.24529
                33 82     93.75
                33 82  91.11111
                34 82        95
                34 82        87
                35 82  74.76636
                35 82  54.20561
                36 82  91.08911
                36 82  59.32203
                37 82  86.40777
                37 82  82.47423
                38 82  74.25742
                38 82  79.09091
                39 82  81.65137
                39 82  85.85859
                40 82  87.69231
                40 82  82.97872
                41 82  88.78505
                41 82  90.29126
                42 82  92.52336
                42 82  87.73585
                44 82  38.80597
                45 82  55.66038
                43 82  73.25581
                43 82  62.85714
                46 82  82.65306
                46 82  89.24731
                48 82  79.56989
                48 82  91.75258
                47 82  86.53846
                47 82  83.83839
                49 82  75.72816
                 . 82  78.02198
                end
                label values state state
                label def state 1 "Alabama", modify
                label def state 2 "Arizona", modify
                label def state 3 "Arkansas", modify
                label def state 4 "California", modify
                label def state 5 "Colorado", modify
                label def state 6 "Connecticut", modify
                label def state 7 "Delaware", modify
                label def state 8 "Florida", modify
                label def state 9 "Georgia", modify
                label def state 10 "Idaho", modify
                label def state 11 "Illinois", modify
                label def state 12 "Indiana", modify
                label def state 13 "Iowa", modify
                label def state 14 "Kansas", modify
                label def state 15 "Kentucky", modify
                label def state 16 "Louisiana", modify
                label def state 17 "Maine", modify
                label def state 18 "Maryland", modify
                label def state 19 "Massachusetts", modify
                label def state 20 "Michigan", modify
                label def state 21 "Minnesota", modify
                label def state 22 "Mississippi", modify
                label def state 23 "Missouri", modify
                label def state 24 "Montana", modify
                label def state 25 "Nebraska", modify
                label def state 26 "Nevada", modify
                label def state 27 "New Hampshire", modify
                label def state 28 "New Jersey", modify
                label def state 29 "New Mexico", modify
                label def state 30 "New York", modify
                label def state 31 "North Carolina", modify
                label def state 32 "North Dakota", modify
                label def state 33 "Ohio", modify
                label def state 34 "Oklahoma", modify
                label def state 35 "Oregon", modify
                label def state 36 "Pennsylvania", modify
                label def state 37 "Rhode Island", modify
                label def state 38 "South Carolina", modify
                label def state 39 "South Dakota", modify
                label def state 40 "Tennessee", modify
                label def state 41 "Texas", modify
                label def state 42 "Utah", modify
                label def state 43 "Vermont", modify
                label def state 44 "Viginia", modify
                label def state 45 "Virginia", modify
                label def state 46 "Washington", modify
                label def state 47 "West Virginia", modify
                label def state 48 "Wisconsin", modify
                label def state 49 "Wyoming", modify
                ------------------ copy up to and including the previous line ------------------

                Listed 96 out of 96 observations

                Any help would be appreciated as to how to merge the two files. Thanks

                Ric Uslaner

                Comment


                • #9
                  Manifestly the assignment of value labels isn't consistent. So, I would decode each numeric variable and merge on the resulting string variables.

                  But, but, but: before you do that
                  merge do clean up the results. Without trying I spotted Viginia and there may be other such phantoms.

                  Comment


                  • #10
                    Sorry again but I am running into the same problem after doing what Nick says.

                    For D:\Data\statedatanov2025revgss.dta


                    . dataex state dacavote dacaparty pid2 christiannationalism

                    ----------------------- copy starting from the next line -----------------------
                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input long state float(dacavote dacaparty) double pid2 float christiannationalism
                     1  0  0                 94 2.6575344
                     2  1  0 22.799999237060547  3.164216
                     3  1  0  67.47826385498047  3.363014
                     4  0  0   63.5217399597168 2.5714285
                     5  1  1                410  3.342857
                     6  1  1  90.73912811279297         3
                     7 .5  1  96.08695983886719         3
                     8  1  1 16.869565963745117 2.8709676
                     9  1 .5 233.82608032226563 2.9489796
                    10  0  0 133.82608032226563 2.7045455
                    11  1  1 21.200000762939453 3.0714285
                    12 .5 .5  27.65217399597168       3.3
                    13  1  1 246.86956787109375 2.9382715
                    14 .5 .5 138.56521606445313  2.978873
                    15 .5 .5  83.34782409667969  3.076923
                    16  0  0  61.60869598388672         3
                    17  0  0 59.739131927490234  2.866953
                    18 .5 .5  66.60869598388672  3.090909
                    19  1 .5  44.21739196777344         .
                    20  1  1                 82  3.195122
                    21  1  1 213.82608032226563  3.185185
                    22  1  1 244.21739196777344  3.119718
                    23  1  1  116.6956558227539     3.125
                    24  0  0 24.478260040283203  2.632653
                    25 .5 .5 138.04347229003906 3.1764705
                    26  1  1 24.478260040283203 2.7956204
                    27  0  0 36.260868072509766 2.6785715
                    28  .  . 20.521739959716797       2.9
                    29  1 .5  37.91304397583008     3.625
                    30  1  1 190.82608032226563  3.147959
                    31  1  1 28.130434036254883  3.191781
                    32  1  1 348.34783935546875  3.205128
                    33 .5 .5                124         3
                    34  0  0 20.869565963745117  2.888889
                    35 .5 .5  261.9130554199219  3.051948
                    36  0  0  44.39130401611328 2.4642856
                    37  1  1  66.39130401611328  3.333333
                    38  0  0 219.30435180664063  3.076923
                    39  1  1   35.5217399597168  3.352113
                    40  0  0  81.26087188720703 3.0143886
                    41 .5 .5  12.34782600402832  2.952381
                    42  1  0 121.78260803222656  2.980392
                    43  0  0 333.13043212890625  2.764706
                    44 .5  0 34.434783935546875  3.048193
                    45  1  1 21.086956024169922  3.291667
                    46  1  1 167.86956787109375      3.25
                    47  1  1 155.04347229003906  2.948718
                    48  1  1   33.5217399597168 3.2439024
                    49 .5 .5 140.17391967773438   3.15625
                     .  .  .                  .         .
                    end
                    label values state statenew
                    label def statenew 1 "Alabama", modify
                    label def statenew 2 "Alaska", modify
                    label def statenew 3 "Arizona", modify
                    label def statenew 4 "Arkansas", modify
                    label def statenew 5 "California", modify
                    label def statenew 6 "Colorado", modify
                    label def statenew 7 "Connecticut", modify
                    label def statenew 8 "Delaware", modify
                    label def statenew 9 "Florida", modify
                    label def statenew 10 "Georgia", modify
                    label def statenew 11 "Hawaii", modify
                    label def statenew 12 "Idaho", modify
                    label def statenew 13 "Illinois", modify
                    label def statenew 14 "Indiana", modify
                    label def statenew 15 "Iowa", modify
                    label def statenew 16 "Kansas", modify
                    label def statenew 17 "Kentucky", modify
                    label def statenew 18 "Louisiana", modify
                    label def statenew 19 "Maine", modify
                    label def statenew 20 "Maryland", modify
                    label def statenew 21 "Massachusetts", modify
                    label def statenew 22 "Michigan", modify
                    label def statenew 23 "Minnesota", modify
                    label def statenew 24 "Mississippi", modify
                    label def statenew 25 "Missouri", modify
                    label def statenew 26 "Montana", modify
                    label def statenew 27 "Nebraska", modify
                    label def statenew 28 "Nevada", modify
                    label def statenew 29 "New Hampshire", modify
                    label def statenew 30 "New Jersey", modify
                    label def statenew 31 "New Mexico", modify
                    label def statenew 32 "New York", modify
                    label def statenew 33 "North Carolina", modify
                    label def statenew 34 "North Dakota", modify
                    label def statenew 35 "Ohio", modify
                    label def statenew 36 "Oklahoma", modify
                    label def statenew 37 "Oregon", modify
                    label def statenew 38 "Pennsylvania", modify
                    label def statenew 39 "Rhode Island", modify
                    label def statenew 40 "South Carolina", modify
                    label def statenew 41 "South Dakota", modify
                    label def statenew 42 "Tennessee", modify
                    label def statenew 43 "Texas", modify
                    label def statenew 44 "Utah", modify
                    label def statenew 45 "Vermont", modify
                    label def statenew 46 "Virginia", modify
                    label def statenew 47 "Washington", modify
                    label def statenew 48 "West Virginia", modify
                    label def statenew 49 "Wisconsin", modify
                    ------------------ copy up to and including the previous line ------------------

                    Listed 50 out of 50 observations

                    for D:\Data\congressionalunityNov2025revised.dta

                    . dataex state unity_score unity_ptile senator west-south

                    ----------------------- copy starting from the next line -----------------------
                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input long state float(unity_score unity_ptile) double senator float(west northeast midwest farwest south)
                     1  94.79166        96 6770 . . . . 1
                     1   95.2381       100 3357 . . . . 1
                     2  92.52336        94 3213 1 . . . 0
                     2  92.45283        92 5187 1 . . . 0
                     3  69.30693        22 5116 . . . . 1
                     3  90.81633        88 2565 . . . . 1
                     4  76.47059 34.782608 5383 1 . . . 0
                     4   83.5443  45.65217 4074 1 . . . 0
                     5  67.77778        18 3745 1 . . . 0
                     5  91.30434   82.6087 4823 1 . . . 0
                     6  89.65517        84 5267 . 1 . . 0
                     6     81.25        54  484 . 1 . . 0
                     7  90.82569  73.91304 7733 . 1 . . 0
                     7  55.14019         4 2536 . 1 . . 0
                     8  76.36364        38 3439 . . . . 1
                     8  80.37383        52 6654 . . . . 1
                     9   89.3617        82 6275 . . . . 1
                     9  76.53061        40 2679 . . . . 1
                    10  94.49541  97.82609 2083 . . . 1 0
                    10  91.50944  86.95652 7596 . . . 1 0
                    11  95.55556       100 1931 . . 1 . 0
                    11  58.33333         8 2001 . . 1 . 0
                    12  87.83784  65.21739 3719 . . 1 . 0
                    12  86.02151  54.34783 1114 . . 1 . 0
                    13  76.08696        36 2733 . . 1 . 0
                    13        92  91.30434 3328 . . 1 . 0
                    14  90.90909  76.08696 1143 . . 1 . 0
                    14  87.73585  60.86956 6413 . . 1 . 0
                    15  88.46154        78 7327 . . . . 1
                    15  91.58878        90 1357 . . . . 1
                    16 75.229355        30 4426 . . . . 1
                    16 66.037735        14 2159 . . . . 1
                    17  86.91589  56.52174 6705 . 1 . . 0
                    17  84.33735  47.82609  754 . 1 . . 0
                    18  91.48936  84.78261 1005 . 1 . . 0
                    18  60.43956        10 5447 . 1 . . 0
                    19  67.59259  15.21739 4398 . 1 . . 0
                    19  72.81554  21.73913 6319 . 1 . . 0
                    20  72.22222        26 4895 . . 1 . 0
                    20  84.40367        50 2341 . . 1 . 0
                    21        80        50 3581 . . 1 . 0
                    21 75.471695 32.608696 7193 . . 1 . 0
                    22 70.833336        24 2108 . . . . 1
                    22  75.92593        34 6867 . . . . 1
                    23   87.7551  63.04348 3920 . . 1 . 0
                    23  86.79245        72 3292 . . 1 . 0
                    24  80.30303  41.30435 2119 . . . 1 0
                    24  77.65958        42 5056 . . . 1 0
                    25  73.13433 23.913044 6468 . . 1 . 0
                    25  88.23529 67.391304 1002 . . 1 . 0
                    26  66.27907        16 5101 1 . . . 0
                    26 65.333336 13.043478 4573 1 . . . 0
                    27  90.72165  71.73913  761 . 1 . . 0
                    27  45.67901  2.173913 7214 . 1 . . 0
                    28  69.44444 17.391304 6687 . 1 . . 0
                    28  77.77778  36.95652 3287 . 1 . . 0
                    29  84.81013        64  120 1 . . . 0
                    29   84.9315        66 1258 1 . . . 0
                    30 73.333336  28.26087 3650 . 1 . . 0
                    30  79.24529        48 4285 . 1 . . 0
                    31  69.15888        20 3413 . . . . 1
                    31        62        12 6725 . . . . 1
                    32  70.37037  19.56522 7914 . . . 1 0
                    32  64.10256 10.869565 4185 . . . 1 0
                    33     93.75  95.65218  758 . . 1 . 0
                    33  91.11111  80.43478 7041 . . 1 . 0
                    34        87        74 4886 . . . . 1
                    34        95        98 3966 . . . . 1
                    35  74.76636  30.43478 1533 1 . . . 0
                    35  54.20561  4.347826 4979 1 . . . 0
                    36  91.08911  78.26087 4633 . 1 . . 0
                    36  59.32203  6.521739 2044 . 1 . . 0
                    37  86.40777        68 5607 . 1 . . 0
                    37  82.47423        56 2893 . 1 . . 0
                    38  79.09091        46 3791 . . . . 1
                    38  74.25742        28 4690 . . . . 1
                    39  81.65137  43.47826 5026 . . . 1 0
                    39  85.85859  52.17391 1191 . . . 1 0
                    40  87.69231        76 3892 . . . . 1
                    40  82.97872        60 5229 . . . . 1
                    41  88.78505        80 3766 . . . . 1
                    41  90.29126        86 1472 . . . . 1
                    42  87.73585  60.86956 7531 . . . 1 0
                    42  92.52336  93.47826  474 . . . 1 0
                    43  73.25581 26.086956 2419 . 1 . . 0
                    43  62.85714  8.695652   41 . 1 . . 0
                    44  55.66038         6 6116 . . . . 1
                    44  38.80597         2 1023 . . . . 1
                    45  82.65306        58 4547 1 . . . 0
                    45  89.24731 69.565216 1050 1 . . . 0
                    46  83.83839        62 5325 . 1 . . 0
                    46  86.53846        70 3990 . 1 . . 0
                    47  79.56989  39.13044 7707 . . 1 . 0
                    47  91.75258  89.13043 5106 . . 1 . 0
                    48  78.02198        44 5464 . . . 1 0
                    48  75.72816        32 3591 . . . 1 0
                     .         .         .    . . . . . .
                    end
                    label values state statex
                    label def statex 1 "Alabama", modify
                    label def statex 2 "Arizona", modify
                    label def statex 3 "Arkansas", modify
                    label def statex 4 "California", modify
                    label def statex 5 "Colorado", modify
                    label def statex 6 "Connecticut", modify
                    label def statex 7 "Delaware", modify
                    label def statex 8 "Florida", modify
                    label def statex 9 "Georgia", modify
                    label def statex 10 "Idaho", modify
                    label def statex 11 "Illinois", modify
                    label def statex 12 "Indiana", modify
                    label def statex 13 "Iowa", modify
                    label def statex 14 "Kansas", modify
                    label def statex 15 "Kentucky", modify
                    label def statex 16 "Louisiana", modify
                    label def statex 17 "Maine", modify
                    label def statex 18 "Maryland", modify
                    label def statex 19 "Massachusetts", modify
                    label def statex 20 "Michigan", modify
                    label def statex 21 "Minnesota", modify
                    label def statex 22 "Mississippi", modify
                    label def statex 23 "Missouri", modify
                    label def statex 24 "Montana", modify
                    label def statex 25 "Nebraska", modify
                    label def statex 26 "Nevada", modify
                    label def statex 27 "New Hampshire", modify
                    label def statex 28 "New Jersey", modify
                    label def statex 29 "New Mexico", modify
                    label def statex 30 "New York", modify
                    label def statex 31 "North Carolina", modify
                    label def statex 32 "North Dakota", modify
                    label def statex 33 "Ohio", modify
                    label def statex 34 "Oklahoma", modify
                    label def statex 35 "Oregon", modify
                    label def statex 36 "Pennsylvania", modify
                    label def statex 37 "Rhode Island", modify
                    label def statex 38 "South Carolina", modify
                    label def statex 39 "South Dakota", modify
                    label def statex 40 "Tennessee", modify
                    label def statex 41 "Texas", modify
                    label def statex 42 "Utah", modify
                    label def statex 43 "Vermont", modify
                    label def statex 44 "Virginia", modify
                    label def statex 45 "Washington", modify
                    label def statex 46 "West Virginia", modify
                    label def statex 47 "Wisconsin", modify
                    label def statex 48 "Wyoming", modify
                    label values senator senator
                    label def senator 41 "AIKEN, George David", modify
                    label def senator 120 "ANDERSON, Clinton Presba", modify
                    label def senator 474 "BENNETT, Wallace Foster", modify
                    label def senator 484 "BENTON, William", modify
                    label def senator 754 "BREWSTER, Ralph Owen", modify
                    label def senator 758 "BRICKER, John William", modify
                    label def senator 761 "BRIDGES, Henry Styles (Styles)", modify
                    label def senator 1002 "BUTLER, Hugh Alfred", modify
                    label def senator 1005 "BUTLER, John Marshall", modify
                    label def senator 1023 "BYRD, Harry Flood", modify
                    label def senator 1050 "CAIN, Harry Pulliam", modify
                    label def senator 1114 "CAPEHART, Homer Earl", modify
                    label def senator 1143 "CARLSON, Frank", modify
                    label def senator 1191 "CASE, Francis Higbee", modify
                    label def senator 1258 "CHAVEZ, Dennis", modify
                    label def senator 1357 "CLEMENTS, Earle C.", modify
                    label def senator 1472 "CONNALLY, Thomas Terry (Tom)", modify
                    label def senator 1533 "CORDON, Guy", modify
                    label def senator 1931 "DIRKSEN, Everett McKinley", modify
                    label def senator 2001 "DOUGLAS, Paul Howard", modify
                    label def senator 2044 "DUFF, James Henderson", modify
                    label def senator 2083 "DWORSHAK, Henry Clarence", modify
                    label def senator 2108 "EASTLAND, James Oliver", modify
                    label def senator 2119 "ECTON, Zales Nelson", modify
                    label def senator 2159 "ELLENDER, Allen Joseph", modify
                    label def senator 2341 "FERGUSON, Homer Samuel", modify
                    label def senator 2419 "FLANDERS, Ralph Edward", modify
                    label def senator 2536 "FREAR, Joseph Allen, Jr.", modify
                    label def senator 2565 "FULBRIGHT, James William", modify
                    label def senator 2679 "GEORGE, Walter Franklin", modify
                    label def senator 2733 "GILLETTE, Guy Mark", modify
                    label def senator 2893 "GREEN, Theodore Francis", modify
                    label def senator 3213 "HAYDEN, Carl Trumbull", modify
                    label def senator 3287 "HENDRICKSON, Robert Clymer", modify
                    label def senator 3292 "HENNINGS, Thomas Carey, Jr.", modify
                    label def senator 3328 "HICKENLOOPER, Bourke Blakemore", modify
                    label def senator 3357 "HILL, Joseph Lister", modify
                    label def senator 3413 "HOEY, Clyde Roark", modify
                    label def senator 3439 "HOLLAND, Spessard Lindsey", modify
                    label def senator 3581 "HUMPHREY, Hubert Horatio, Jr.", modify
                    label def senator 3591 "HUNT, Lester Callaway", modify
                    label def senator 3650 "IVES, Irving McNeil", modify
                    label def senator 3719 "JENNER, William Ezra", modify
                    label def senator 3745 "JOHNSON, Edwin Carl", modify
                    label def senator 3766 "JOHNSON, Lyndon Baines", modify
                    label def senator 3791 "JOHNSTON, Olin DeWitt Talmadge", modify
                    label def senator 3892 "KEFAUVER, Carey Estes", modify
                    label def senator 3920 "KEM, James Preston", modify
                    label def senator 3966 "KERR, Robert Samuel", modify
                    label def senator 3990 "KILGORE, Harley Martin", modify
                    label def senator 4074 "KNOWLAND, William Fife", modify
                    label def senator 4185 "LANGER, William", modify
                    label def senator 4285 "LEHMAN, Herbert Henry", modify
                    label def senator 4398 "LODGE, Henry Cabot, Jr.", modify
                    label def senator 4426 "LONG, Russell Billiu", modify
                    label def senator 4547 "MAGNUSON, Warren Grant", modify
                    label def senator 4573 "MALONE, George Wilson", modify
                    label def senator 4633 "MARTIN, Edward", modify
                    label def senator 4690 "MAYBANK, Burnet Rhett", modify
                    label def senator 4823 "MILLIKIN, Eugene Donald", modify
                    label def senator 4886 "MONRONEY, Almer Stillwell Mike", modify
                    label def senator 4895 "MOODY, Arthur Edson Blair", modify
                    label def senator 4979 "MORSE, Wayne Lyman", modify
                    label def senator 5026 "MUNDT, Karl Earl", modify
                    label def senator 5056 "MURRAY, James Edward", modify
                    label def senator 5101 "McCARRAN, Patrick Anthony (Pat)", modify
                    label def senator 5106 "McCARTHY, Joseph Raymond", modify
                    label def senator 5116 "McCLELLAN, John Little", modify
                    label def senator 5187 "McFARLAND, Ernest William", modify
                    label def senator 5229 "McKELLAR, Kenneth Douglas", modify
                    label def senator 5267 "McMAHON, Brien", modify
                    label def senator 5325 "NEELY, Matthew Mansfield", modify
                    label def senator 5383 "NIXON, Richard Milhous", modify
                    label def senator 5447 "O'CONOR, Herbert Romulus", modify
                    label def senator 5464 "O'MAHONEY, Joseph Christopher", modify
                    label def senator 5607 "PASTORE, John Orlando", modify
                    label def senator 6116 "ROBERTSON, Absalom Willis", modify
                    label def senator 6275 "RUSSELL, Richard Brevard, Jr.", modify
                    label def senator 6319 "SALTONSTALL, Leverett", modify
                    label def senator 6413 "SCHOEPPEL, Andrew Frank", modify
                    label def senator 6468 "SEATON, Frederick Andrew", modify
                    label def senator 6654 "SMATHERS, George Armistead", modify
                    label def senator 6687 "SMITH, Howard Alexander", modify
                    label def senator 6705 "SMITH, Margaret Chase", modify
                    label def senator 6725 "SMITH, Willis", modify
                    label def senator 6770 "SPARKMAN, John Jackson", modify
                    label def senator 6867 "STENNIS, John Cornelius", modify
                    label def senator 7041 "TAFT, Robert Alphonso", modify
                    label def senator 7193 "THYE, Edward John", modify
                    label def senator 7214 "TOBEY, Charles William", modify
                    label def senator 7327 "UNDERWOOD, Thomas Rust", modify
                    label def senator 7531 "WATKINS, Arthur Vivian", modify
                    label def senator 7596 "WELKER, Herman", modify
                    label def senator 7707 "WILEY, Alexander", modify
                    label def senator 7733 "WILLIAMS, John James", modify
                    label def senator 7914 "YOUNG, Milton Ruben", modify
                    ------------------ copy up to and including the previous line ------------------

                    Listed 97 out of 97 observations

                    .
                    I type and get the error message:

                    . merge m:1 state using D:\Data\statedatanov2025newgss.dta
                    key variable state is long in master but str14 in using data
                    Each key variable -- the variables on which observations are matched -- must be of the same
                    generic type in the master and using datasets. Same generic type means both numeric or both
                    string.
                    r(106);

                    but state is numeric in both data sets. Any help would be appreciated

                    Comment


                    • #11
                      It seems that you didn’t do what I suggested. All keys should be string variables. As the error message tells you, you cannot merge if one key is string and one is numeric. Stata is never wrong about this, in my experience.

                      Perhaps you should hire a research assistant who is proficient in Stata.

                      Comment


                      • #12
                        The following code incorporates what Nick Cox advised you to do. It runs without error messages and it produces a -merged- data set. The only thing that might be surprising about the resulting -merge- is that Alaska and Hawaii appear only in the first data set, and Wyoming appears only in the second one--so these observations have no matches.

                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input long state float(dacavote dacaparty) double pid2 float christiannationalism
                         1  0  0                 94 2.6575344
                         2  1  0 22.799999237060547  3.164216
                         3  1  0  67.47826385498047  3.363014
                         4  0  0   63.5217399597168 2.5714285
                         5  1  1                410  3.342857
                         6  1  1  90.73912811279297         3
                         7 .5  1  96.08695983886719         3
                         8  1  1 16.869565963745117 2.8709676
                         9  1 .5 233.82608032226563 2.9489796
                        10  0  0 133.82608032226563 2.7045455
                        11  1  1 21.200000762939453 3.0714285
                        12 .5 .5  27.65217399597168       3.3
                        13  1  1 246.86956787109375 2.9382715
                        14 .5 .5 138.56521606445313  2.978873
                        15 .5 .5  83.34782409667969  3.076923
                        16  0  0  61.60869598388672         3
                        17  0  0 59.739131927490234  2.866953
                        18 .5 .5  66.60869598388672  3.090909
                        19  1 .5  44.21739196777344         .
                        20  1  1                 82  3.195122
                        21  1  1 213.82608032226563  3.185185
                        22  1  1 244.21739196777344  3.119718
                        23  1  1  116.6956558227539     3.125
                        24  0  0 24.478260040283203  2.632653
                        25 .5 .5 138.04347229003906 3.1764705
                        26  1  1 24.478260040283203 2.7956204
                        27  0  0 36.260868072509766 2.6785715
                        28  .  . 20.521739959716797       2.9
                        29  1 .5  37.91304397583008     3.625
                        30  1  1 190.82608032226563  3.147959
                        31  1  1 28.130434036254883  3.191781
                        32  1  1 348.34783935546875  3.205128
                        33 .5 .5                124         3
                        34  0  0 20.869565963745117  2.888889
                        35 .5 .5  261.9130554199219  3.051948
                        36  0  0  44.39130401611328 2.4642856
                        37  1  1  66.39130401611328  3.333333
                        38  0  0 219.30435180664063  3.076923
                        39  1  1   35.5217399597168  3.352113
                        40  0  0  81.26087188720703 3.0143886
                        41 .5 .5  12.34782600402832  2.952381
                        42  1  0 121.78260803222656  2.980392
                        43  0  0 333.13043212890625  2.764706
                        44 .5  0 34.434783935546875  3.048193
                        45  1  1 21.086956024169922  3.291667
                        46  1  1 167.86956787109375      3.25
                        47  1  1 155.04347229003906  2.948718
                        48  1  1   33.5217399597168 3.2439024
                        49 .5 .5 140.17391967773438   3.15625
                         .  .  .                  .         .
                        end
                        label values state statenew
                        label def statenew 1 "Alabama", modify
                        label def statenew 2 "Alaska", modify
                        label def statenew 3 "Arizona", modify
                        label def statenew 4 "Arkansas", modify
                        label def statenew 5 "California", modify
                        label def statenew 6 "Colorado", modify
                        label def statenew 7 "Connecticut", modify
                        label def statenew 8 "Delaware", modify
                        label def statenew 9 "Florida", modify
                        label def statenew 10 "Georgia", modify
                        label def statenew 11 "Hawaii", modify
                        label def statenew 12 "Idaho", modify
                        label def statenew 13 "Illinois", modify
                        label def statenew 14 "Indiana", modify
                        label def statenew 15 "Iowa", modify
                        label def statenew 16 "Kansas", modify
                        label def statenew 17 "Kentucky", modify
                        label def statenew 18 "Louisiana", modify
                        label def statenew 19 "Maine", modify
                        label def statenew 20 "Maryland", modify
                        label def statenew 21 "Massachusetts", modify
                        label def statenew 22 "Michigan", modify
                        label def statenew 23 "Minnesota", modify
                        label def statenew 24 "Mississippi", modify
                        label def statenew 25 "Missouri", modify
                        label def statenew 26 "Montana", modify
                        label def statenew 27 "Nebraska", modify
                        label def statenew 28 "Nevada", modify
                        label def statenew 29 "New Hampshire", modify
                        label def statenew 30 "New Jersey", modify
                        label def statenew 31 "New Mexico", modify
                        label def statenew 32 "New York", modify
                        label def statenew 33 "North Carolina", modify
                        label def statenew 34 "North Dakota", modify
                        label def statenew 35 "Ohio", modify
                        label def statenew 36 "Oklahoma", modify
                        label def statenew 37 "Oregon", modify
                        label def statenew 38 "Pennsylvania", modify
                        label def statenew 39 "Rhode Island", modify
                        label def statenew 40 "South Carolina", modify
                        label def statenew 41 "South Dakota", modify
                        label def statenew 42 "Tennessee", modify
                        label def statenew 43 "Texas", modify
                        label def statenew 44 "Utah", modify
                        label def statenew 45 "Vermont", modify
                        label def statenew 46 "Virginia", modify
                        label def statenew 47 "Washington", modify
                        label def statenew 48 "West Virginia", modify
                        label def statenew 49 "Wisconsin", modify
                        tempfile dataset1
                        save `dataset1'
                        
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input long state float(unity_score unity_ptile) double senator float(west northeast midwest farwest south)
                         1  94.79166        96 6770 . . . . 1
                         1   95.2381       100 3357 . . . . 1
                         2  92.52336        94 3213 1 . . . 0
                         2  92.45283        92 5187 1 . . . 0
                         3  69.30693        22 5116 . . . . 1
                         3  90.81633        88 2565 . . . . 1
                         4  76.47059 34.782608 5383 1 . . . 0
                         4   83.5443  45.65217 4074 1 . . . 0
                         5  67.77778        18 3745 1 . . . 0
                         5  91.30434   82.6087 4823 1 . . . 0
                         6  89.65517        84 5267 . 1 . . 0
                         6     81.25        54  484 . 1 . . 0
                         7  90.82569  73.91304 7733 . 1 . . 0
                         7  55.14019         4 2536 . 1 . . 0
                         8  76.36364        38 3439 . . . . 1
                         8  80.37383        52 6654 . . . . 1
                         9   89.3617        82 6275 . . . . 1
                         9  76.53061        40 2679 . . . . 1
                        10  94.49541  97.82609 2083 . . . 1 0
                        10  91.50944  86.95652 7596 . . . 1 0
                        11  95.55556       100 1931 . . 1 . 0
                        11  58.33333         8 2001 . . 1 . 0
                        12  87.83784  65.21739 3719 . . 1 . 0
                        12  86.02151  54.34783 1114 . . 1 . 0
                        13  76.08696        36 2733 . . 1 . 0
                        13        92  91.30434 3328 . . 1 . 0
                        14  90.90909  76.08696 1143 . . 1 . 0
                        14  87.73585  60.86956 6413 . . 1 . 0
                        15  88.46154        78 7327 . . . . 1
                        15  91.58878        90 1357 . . . . 1
                        16 75.229355        30 4426 . . . . 1
                        16 66.037735        14 2159 . . . . 1
                        17  86.91589  56.52174 6705 . 1 . . 0
                        17  84.33735  47.82609  754 . 1 . . 0
                        18  91.48936  84.78261 1005 . 1 . . 0
                        18  60.43956        10 5447 . 1 . . 0
                        19  67.59259  15.21739 4398 . 1 . . 0
                        19  72.81554  21.73913 6319 . 1 . . 0
                        20  72.22222        26 4895 . . 1 . 0
                        20  84.40367        50 2341 . . 1 . 0
                        21        80        50 3581 . . 1 . 0
                        21 75.471695 32.608696 7193 . . 1 . 0
                        22 70.833336        24 2108 . . . . 1
                        22  75.92593        34 6867 . . . . 1
                        23   87.7551  63.04348 3920 . . 1 . 0
                        23  86.79245        72 3292 . . 1 . 0
                        24  80.30303  41.30435 2119 . . . 1 0
                        24  77.65958        42 5056 . . . 1 0
                        25  73.13433 23.913044 6468 . . 1 . 0
                        25  88.23529 67.391304 1002 . . 1 . 0
                        26  66.27907        16 5101 1 . . . 0
                        26 65.333336 13.043478 4573 1 . . . 0
                        27  90.72165  71.73913  761 . 1 . . 0
                        27  45.67901  2.173913 7214 . 1 . . 0
                        28  69.44444 17.391304 6687 . 1 . . 0
                        28  77.77778  36.95652 3287 . 1 . . 0
                        29  84.81013        64  120 1 . . . 0
                        29   84.9315        66 1258 1 . . . 0
                        30 73.333336  28.26087 3650 . 1 . . 0
                        30  79.24529        48 4285 . 1 . . 0
                        31  69.15888        20 3413 . . . . 1
                        31        62        12 6725 . . . . 1
                        32  70.37037  19.56522 7914 . . . 1 0
                        32  64.10256 10.869565 4185 . . . 1 0
                        33     93.75  95.65218  758 . . 1 . 0
                        33  91.11111  80.43478 7041 . . 1 . 0
                        34        87        74 4886 . . . . 1
                        34        95        98 3966 . . . . 1
                        35  74.76636  30.43478 1533 1 . . . 0
                        35  54.20561  4.347826 4979 1 . . . 0
                        36  91.08911  78.26087 4633 . 1 . . 0
                        36  59.32203  6.521739 2044 . 1 . . 0
                        37  86.40777        68 5607 . 1 . . 0
                        37  82.47423        56 2893 . 1 . . 0
                        38  79.09091        46 3791 . . . . 1
                        38  74.25742        28 4690 . . . . 1
                        39  81.65137  43.47826 5026 . . . 1 0
                        39  85.85859  52.17391 1191 . . . 1 0
                        40  87.69231        76 3892 . . . . 1
                        40  82.97872        60 5229 . . . . 1
                        41  88.78505        80 3766 . . . . 1
                        41  90.29126        86 1472 . . . . 1
                        42  87.73585  60.86956 7531 . . . 1 0
                        42  92.52336  93.47826  474 . . . 1 0
                        43  73.25581 26.086956 2419 . 1 . . 0
                        43  62.85714  8.695652   41 . 1 . . 0
                        44  55.66038         6 6116 . . . . 1
                        44  38.80597         2 1023 . . . . 1
                        45  82.65306        58 4547 1 . . . 0
                        45  89.24731 69.565216 1050 1 . . . 0
                        46  83.83839        62 5325 . 1 . . 0
                        46  86.53846        70 3990 . 1 . . 0
                        47  79.56989  39.13044 7707 . . 1 . 0
                        47  91.75258  89.13043 5106 . . 1 . 0
                        48  78.02198        44 5464 . . . 1 0
                        48  75.72816        32 3591 . . . 1 0
                         .         .         .    . . . . . .
                        end
                        label values state statex
                        label def statex 1 "Alabama", modify
                        label def statex 2 "Arizona", modify
                        label def statex 3 "Arkansas", modify
                        label def statex 4 "California", modify
                        label def statex 5 "Colorado", modify
                        label def statex 6 "Connecticut", modify
                        label def statex 7 "Delaware", modify
                        label def statex 8 "Florida", modify
                        label def statex 9 "Georgia", modify
                        label def statex 10 "Idaho", modify
                        label def statex 11 "Illinois", modify
                        label def statex 12 "Indiana", modify
                        label def statex 13 "Iowa", modify
                        label def statex 14 "Kansas", modify
                        label def statex 15 "Kentucky", modify
                        label def statex 16 "Louisiana", modify
                        label def statex 17 "Maine", modify
                        label def statex 18 "Maryland", modify
                        label def statex 19 "Massachusetts", modify
                        label def statex 20 "Michigan", modify
                        label def statex 21 "Minnesota", modify
                        label def statex 22 "Mississippi", modify
                        label def statex 23 "Missouri", modify
                        label def statex 24 "Montana", modify
                        label def statex 25 "Nebraska", modify
                        label def statex 26 "Nevada", modify
                        label def statex 27 "New Hampshire", modify
                        label def statex 28 "New Jersey", modify
                        label def statex 29 "New Mexico", modify
                        label def statex 30 "New York", modify
                        label def statex 31 "North Carolina", modify
                        label def statex 32 "North Dakota", modify
                        label def statex 33 "Ohio", modify
                        label def statex 34 "Oklahoma", modify
                        label def statex 35 "Oregon", modify
                        label def statex 36 "Pennsylvania", modify
                        label def statex 37 "Rhode Island", modify
                        label def statex 38 "South Carolina", modify
                        label def statex 39 "South Dakota", modify
                        label def statex 40 "Tennessee", modify
                        label def statex 41 "Texas", modify
                        label def statex 42 "Utah", modify
                        label def statex 43 "Vermont", modify
                        label def statex 44 "Virginia", modify
                        label def statex 45 "Washington", modify
                        label def statex 46 "West Virginia", modify
                        label def statex 47 "Wisconsin", modify
                        label def statex 48 "Wyoming", modify
                        label values senator senator
                        label def senator 41 "AIKEN, George David", modify
                        label def senator 120 "ANDERSON, Clinton Presba", modify
                        label def senator 474 "BENNETT, Wallace Foster", modify
                        label def senator 484 "BENTON, William", modify
                        label def senator 754 "BREWSTER, Ralph Owen", modify
                        label def senator 758 "BRICKER, John William", modify
                        label def senator 761 "BRIDGES, Henry Styles (Styles)", modify
                        label def senator 1002 "BUTLER, Hugh Alfred", modify
                        label def senator 1005 "BUTLER, John Marshall", modify
                        label def senator 1023 "BYRD, Harry Flood", modify
                        label def senator 1050 "CAIN, Harry Pulliam", modify
                        label def senator 1114 "CAPEHART, Homer Earl", modify
                        label def senator 1143 "CARLSON, Frank", modify
                        label def senator 1191 "CASE, Francis Higbee", modify
                        label def senator 1258 "CHAVEZ, Dennis", modify
                        label def senator 1357 "CLEMENTS, Earle C.", modify
                        label def senator 1472 "CONNALLY, Thomas Terry (Tom)", modify
                        label def senator 1533 "CORDON, Guy", modify
                        label def senator 1931 "DIRKSEN, Everett McKinley", modify
                        label def senator 2001 "DOUGLAS, Paul Howard", modify
                        label def senator 2044 "DUFF, James Henderson", modify
                        label def senator 2083 "DWORSHAK, Henry Clarence", modify
                        label def senator 2108 "EASTLAND, James Oliver", modify
                        label def senator 2119 "ECTON, Zales Nelson", modify
                        label def senator 2159 "ELLENDER, Allen Joseph", modify
                        label def senator 2341 "FERGUSON, Homer Samuel", modify
                        label def senator 2419 "FLANDERS, Ralph Edward", modify
                        label def senator 2536 "FREAR, Joseph Allen, Jr.", modify
                        label def senator 2565 "FULBRIGHT, James William", modify
                        label def senator 2679 "GEORGE, Walter Franklin", modify
                        label def senator 2733 "GILLETTE, Guy Mark", modify
                        label def senator 2893 "GREEN, Theodore Francis", modify
                        label def senator 3213 "HAYDEN, Carl Trumbull", modify
                        label def senator 3287 "HENDRICKSON, Robert Clymer", modify
                        label def senator 3292 "HENNINGS, Thomas Carey, Jr.", modify
                        label def senator 3328 "HICKENLOOPER, Bourke Blakemore", modify
                        label def senator 3357 "HILL, Joseph Lister", modify
                        label def senator 3413 "HOEY, Clyde Roark", modify
                        label def senator 3439 "HOLLAND, Spessard Lindsey", modify
                        label def senator 3581 "HUMPHREY, Hubert Horatio, Jr.", modify
                        label def senator 3591 "HUNT, Lester Callaway", modify
                        label def senator 3650 "IVES, Irving McNeil", modify
                        label def senator 3719 "JENNER, William Ezra", modify
                        label def senator 3745 "JOHNSON, Edwin Carl", modify
                        label def senator 3766 "JOHNSON, Lyndon Baines", modify
                        label def senator 3791 "JOHNSTON, Olin DeWitt Talmadge", modify
                        label def senator 3892 "KEFAUVER, Carey Estes", modify
                        label def senator 3920 "KEM, James Preston", modify
                        label def senator 3966 "KERR, Robert Samuel", modify
                        label def senator 3990 "KILGORE, Harley Martin", modify
                        label def senator 4074 "KNOWLAND, William Fife", modify
                        label def senator 4185 "LANGER, William", modify
                        label def senator 4285 "LEHMAN, Herbert Henry", modify
                        label def senator 4398 "LODGE, Henry Cabot, Jr.", modify
                        label def senator 4426 "LONG, Russell Billiu", modify
                        label def senator 4547 "MAGNUSON, Warren Grant", modify
                        label def senator 4573 "MALONE, George Wilson", modify
                        label def senator 4633 "MARTIN, Edward", modify
                        label def senator 4690 "MAYBANK, Burnet Rhett", modify
                        label def senator 4823 "MILLIKIN, Eugene Donald", modify
                        label def senator 4886 "MONRONEY, Almer Stillwell Mike", modify
                        label def senator 4895 "MOODY, Arthur Edson Blair", modify
                        label def senator 4979 "MORSE, Wayne Lyman", modify
                        label def senator 5026 "MUNDT, Karl Earl", modify
                        label def senator 5056 "MURRAY, James Edward", modify
                        label def senator 5101 "McCARRAN, Patrick Anthony (Pat)", modify
                        label def senator 5106 "McCARTHY, Joseph Raymond", modify
                        label def senator 5116 "McCLELLAN, John Little", modify
                        label def senator 5187 "McFARLAND, Ernest William", modify
                        label def senator 5229 "McKELLAR, Kenneth Douglas", modify
                        label def senator 5267 "McMAHON, Brien", modify
                        label def senator 5325 "NEELY, Matthew Mansfield", modify
                        label def senator 5383 "NIXON, Richard Milhous", modify
                        label def senator 5447 "O'CONOR, Herbert Romulus", modify
                        label def senator 5464 "O'MAHONEY, Joseph Christopher", modify
                        label def senator 5607 "PASTORE, John Orlando", modify
                        label def senator 6116 "ROBERTSON, Absalom Willis", modify
                        label def senator 6275 "RUSSELL, Richard Brevard, Jr.", modify
                        label def senator 6319 "SALTONSTALL, Leverett", modify
                        label def senator 6413 "SCHOEPPEL, Andrew Frank", modify
                        label def senator 6468 "SEATON, Frederick Andrew", modify
                        label def senator 6654 "SMATHERS, George Armistead", modify
                        label def senator 6687 "SMITH, Howard Alexander", modify
                        label def senator 6705 "SMITH, Margaret Chase", modify
                        label def senator 6725 "SMITH, Willis", modify
                        label def senator 6770 "SPARKMAN, John Jackson", modify
                        label def senator 6867 "STENNIS, John Cornelius", modify
                        label def senator 7041 "TAFT, Robert Alphonso", modify
                        label def senator 7193 "THYE, Edward John", modify
                        label def senator 7214 "TOBEY, Charles William", modify
                        label def senator 7327 "UNDERWOOD, Thomas Rust", modify
                        label def senator 7531 "WATKINS, Arthur Vivian", modify
                        label def senator 7596 "WELKER, Herman", modify
                        label def senator 7707 "WILEY, Alexander", modify
                        label def senator 7733 "WILLIAMS, John James", modify
                        label def senator 7914 "YOUNG, Milton Ruben", modify
                        tempfile dataset2
                        save `dataset2'
                        
                        use `dataset2'
                        decode state, gen(str_state)
                        drop state
                        tempfile holding
                        save `holding'
                        
                        use `dataset1'
                        decode state, gen(str_state)
                        drop state
                        merge 1:m str_state using `holding'
                        
                        list str_state _merge if _merge != 3

                        Comment


                        • #13
                          Thank you both. The manual writes that the merge variable can be either numeric or string. My variable state was numeric but the merge didn't work. Following your advice I changed it to a string and it worked.

                          Comment


                          • #14
                            Yes, the manual says that the merge variable can be either numeric or string, and that is correct. The problem you encountered was because, the numeric versions having been created with -encode- in data sets having different values of the state variable, the same number corresponded to different states in each data set. So mergeing on that variable leads to data salad. But if you had the exact same numeric encoding of the states in both data sets, e.g. FIPS or ISO or something like that, the merge would have gone smoothly with the numeric variables.

                            Comment


                            • #15
                              I now have another question. I have a dataset from the American National Election study. I am trying to link attitudes of respondents with their members of Congress. To do so requires matching respondents with their Congressional districts. Yet the data on members may have different coding than that of the respondents. So I need to find a way to make the two datasets have identical values for the congressional districts in each data set. The data set that I have presents the data for districts as "state district number." I need to split this into two variables, one with just the state and the other with the district number to make the two data sets compatible. I have tried a number of options for split but none has worked. If anyone can help me convert the single variable in the American National Election Study dataset to two variables, I would appreciate it. The dataset from the ANES is presented below from dataex, but one has to go to the end of the dataex to see state together with the district number, The example is as follows:

                              . dataex congdist if congdist < 20

                              ----------------------- copy starting from the next line -----------------------
                              Code:
                              * Example generated by -dataex-. For more info, type help dataex
                              clear
                              input long congdist
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              1
                              2
                              2
                              2
                              2
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              3
                              4
                              4
                              4
                              4
                              4
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              5
                              end
                              label values congdist congdist
                              label def congdist 1 "AL03", modify
                              label def congdist 2 "AL04", modify
                              label def congdist 3 "AL06", modify
                              label def congdist 4 "AL07", modify
                              label def congdist 5 "AR01", modify
                              ------------------ copy up to and including the previous line ------------------

                              Listed 100 out of 1980 observations
                              Use the count() option to list more

                              .
                              So AL03 needs to be "AL" and "03" as separate variables. Thanks for any help.

                              Ric Uslaner

                              Comment

                              Working...
                              X