Announcement

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

  • Help merging two datasets

    Dear all, I am using Stata 16, on mac. Here are snapshots of my two datasets. I have collected data of annual GDP for all 50 states for the years 2016, 2017, and 2018. for the first dataset. And for the second dataset I have collected annual population data for all 50 states for the years 2016, 2017, and 2018. I wanted some help merging these two datasets together.
    Click image for larger version

Name:	Screen Shot 2019-11-22 at 11.52.19 PM.png
Views:	1
Size:	28.7 KB
ID:	1526050


    Click image for larger version

Name:	Screen Shot 2019-11-22 at 11.50.32 PM.png
Views:	1
Size:	25.7 KB
ID:	1526049


    Thank you in advance for your help


    Jason Browen

  • #2
    See -help append-.

    Comment


    • #3
      see the FAQ for why snapshots like this are not useful and for what to do instead

      I read you query differently than Mike does and suggest you want to first, sort each file by state and then by year; then merge (1:1) on state and on year; see
      Code:
      help merge
      please read and act on the FAQ

      Comment


      • #4
        Jason Browen

        I agree with Rich's interpretation of your question and his solution.

        I too suggest you take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ. It's particularly unhelpful, as Rich suggests, and I suggested in an answer to a previous topic, to post pictures of data.

        The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

        In particular, the fact that your state names are displayed in blue suggests that state is a numeric variable with value labels, and we cannot tell from your pictures whether it is likely that, for example, California has the same numeric value in both datasets. If the encoding is not the same in both datasets, your merge will not be correct, because it will match the variable values, not their value labels.

        I will also add that I think it is no longer necessary, since Stata version 11, to sort the two datasets before using merge to combine them. The output of help merge in Stata 16 includes the sorted option, which implies that Stata will do the any needed sort as part of the merge.

        The following option is available with merge but is not shown in the dialog box:

        sorted specifies that the master and using datasets are already sorted by varlist. If the datasets are already sorted, then merge runs a little more quickly; the difference is hardly detectable, so this option is of interest only where speed is of the utmost importance.

        Comment


        • #5
          Sorry about that here are the commands that I used.

          Click image for larger version

Name:	Screen Shot 2019-11-23 at 12.01.16 PM.png
Views:	1
Size:	72.6 KB
ID:	1526112

          Comment


          • #6
            Yes, my suggestion to look at -append- is clearly wrong; I misread your posting, no excuse. It is a -merge- problem, with the same units, but different variables.

            Comment

            Working...
            X