Announcement

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

  • Merging certain variables whilst matching ID number

    Dear Statalist,

    Hope you're well. I am trying to merge certain variables from my using dataset into my master. The identifying variable here is 'ncdsid'. What command shall I use, I presume this is a one-to-one? Can you also go several variables at once using one command?

    I have attached images to help.

    Many thanks in advance,
    Vidhushi Singhal
    Click image for larger version

Name:	Screenshot 2020-11-28 at 18.12.09.png
Views:	2
Size:	150.6 KB
ID:	1583813
    Click image for larger version

Name:	Screenshot 2020-11-28 at 18.12.51.png
Views:	1
Size:	392.7 KB
ID:	1583812
    Attached Files

  • #2
    This does appear to be a 1:1 merge: within the example data shown each value of ncdsid appears only once. If that pattern holds throughout the data, then it is, in fact, a 1:1 merge.

    Code:
    use first_data_set, clear
    merge 1:1 ncdsid using second_data_set
    Replace the italicized portions of the code with the actual names of the data sets. And read -help merge- to understand how the -merge- command works and how to interpret the -merge- variable that Stata will add to the final data set. There you will also learn of options you can use to modify the behavior of the -merge- command in some ways that are sometimes useful.

    In the event that there are values of ncdsid that appear more than once in one or both of the data sets, then the data would not be suitable for 1:1 merging. In that case, Stata will give you an error message and tell you which data set has the problem. Then you have to figure out why ncdsid does not actually serve as a unique identifier and then either fix that problem, or change the -merge- to 1:m or m:1. (Whatever happens, never use -merge m:m-. It just makes data salad.)

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    Comment


    • #3
      Thank you very much Clyde Schechter.

      Apologies, I was not aware of this and I will take note of the -dataex- command you have mentioned for future posts.

      Comment

      Working...
      X