Announcement

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

  • Merging dataset with different dates

    Hi, I want to add data from my csv file to my main data file in STATA. The csv file goes from 1988-2017 and my main data file goes from 2004-2022. I only want values from 2004-2017 in my csv to merge into my main data file in STATA. How would I go about doing this?

  • #2
    The merge command has a -keep- option, so you can specify

    Code:
    merge 1:1 id year using ..., keep(match)
    In this case, the intersection between 1988-2017 and 2004-2022 is exactly 2004-2017.

    See

    Code:
    help merge
    Last edited by Andrew Musau; 15 Dec 2022, 05:22.

    Comment


    • #3
      Do I need to import my csv into stata or can it merge from the csv?

      Comment


      • #4
        Never mind about post #3- I have sorted this.

        I am encountering this issue of trying to match the dates- the variable "date" is the same in both files

        Code:
         merge 1:1 date using "/Users/raulathwall/OneDrive/Dis STATA/Fama French factors.dta"
        variable date does not uniquely identify observations in the master data
        r(459);
        I am trying to merge this onto my master file

        Comment


        • #5
          As long as date uniquely identifies observations in the using dataset

          Code:
          merge m:1
          should work.

          Comment


          • #6
            Code:
            date    cost    cheap    donation    asset
            1/1/2004    10    70    0    0
            1/2/2004    15    75    0    6
            1/3/2004    10    65    0    0
            1/4/2004    11    76    0    0
            1/5/2004    0    44    0    0
            1/6/2004    15    74    0    15
            1/7/2004    10    67    5    15
            1/8/2004    16    64    0    14
            1/9/2004    15    58    3    11
            1/10/2004    14    79    0    0
            This is what the master dataset looks like, and it still gives me the same error

            With the dataset I want to merge the dates are in the same format e.g. 1/1/2004, but all the days do not coincide together as this is financial data so I do not have weekend data so I want these to be missing values when merged in

            Comment


            • #7
              Check why you have duplicates, or show the results of

              Code:
              bys date: gen tag=_N>1
              dataex if tag
              across both datasets.

              Comment

              Working...
              X