Announcement

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

  • Appending vs one-to-one merging

    Hello all!

    I know it is a very basic question, however, I wanted to ask, do appending and on-to-one merging give the same results?

  • #2
    No. There are several differences. The most prominent difference is that -merge-ing puts the two data sets "side by side" where as -append-ing stacks the two data sets "vertically."

    But that is not the only difference. When you have two data sets, A, and B,
    Code:
    use A, clear
    merge 1:1 key_variables using B
    and
    Code:
    use B, clear
    merge 1:1 key_variables using A
    produce different results. By contrast
    Code:
    use A, clear
    append using B
    produces the same results as
    Code:
    use B, clear
    append using A
    (except for the sort order of the data).

    The reason for the asymmetry of -merge-ing is that when the data sets have variables other than the merge key variables, in common, the values in the first data set are retained, and those in the second ("using") data set are discarded. Now, you can override that with use of the -update- or -update replace- options of -merge-, but that just enables you to retain the values from the second data set and discard those from the first. When you -merge- two data sets, for variables in common, you can only retain the values from one of the two data sets, the others are discarded.

    By contrast, -append- is "lossless." The complete data from both data sets will be retained (provided, of course, that you don't have one of those incompatibility situations where a variable is string in one of the data sets and the same variable is numeric in the other--in which case either the -merge- fails or the values from one of the data sets will be lost.)

    If you are contemplating using one of these commands, the general guidance on which is appropriate is this: if you are trying to add new variables (columns) to an existing data set, you will generally want to use -merge-, but if you are trying to add new observations (rows) of the existing variables to the data set, you will generally want to use -append-.
    Last edited by Clyde Schechter; 20 Sep 2024, 10:34.

    Comment


    • #3
      No.

      Appending data adds rows to the data, and will increase the number of observations.

      One-to-One merging data will add new columns to the existing data in the dataset (and potentially new rows if there are no matches)

      If you have the following two datasets A and B:

      A
      ID Time Var1
      1 1 50
      1 2 75
      2 1 60
      B
      ID Time Var1 Var2
      1 1 'True'
      2 1 'False'
      3 1 'True'


      Appending will give you:
      ID Time Var1 Var2
      1 1 50 .
      1 2 75 .
      2 1 60 .
      1 1 . 'True'
      2 1 . 'False'
      3 1 . 'True'
      A 1:1 merge on ID and Time will give you the following:
      ID Time Var1 Var2 _merge
      1 1 50 'True' 3
      1 2 75 . 1
      2 1 60 'False' 3
      3 1 . 'True' 2

      In the case of the merge command, the new variable _merge will tell you whether the observation had a match and which dataset it was from.

      Comment


      • #4
        Thank you so much! This helps a lot!

        Comment

        Working...
        X