Announcement

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

  • Adding more years to dataset

    Hi, I have a dataset containing approximately 640K observations (gravity data). Each data point contains trade with another nation and the data is between 1993-2015.
    I would like to add 2 new rows for each trade arrangement between countries - extending the years from 2015-2017.

    Does anyone have any solution for this?

  • #2
    Something like this:
    Code:
    isid country1 country2 year
    expand 3 if year == 2015
    by country1 country2 year, sort: replace year = year + _n - 1 if year == 2015
    The resulting observations for years 2016 and 2017 would then be, except for the year variable, copies of the 2015 observation for the same country pair. You would need additional code to somehow overwrite those with the information for 2016 and 2017.

    In the future, when asking for help with coding you should post example data. I have made certain assumptions about how your data is organized. If those assumptions are incorrect, then we have both wasted our time.

    When showing data examples, please be sure to use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 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.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Sorry about that! Will post that from now on!

      And a big thank you! it solved it!

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Something like this:
        Code:
        isid country1 country2 year
        expand 3 if year == 2015
        by country1 country2 year, sort: replace year = year + _n - 1 if year == 2015
        The resulting observations for years 2016 and 2017 would then be, except for the year variable, copies of the 2015 observation for the same country pair. You would need additional code to somehow overwrite those with the information for 2016 and 2017.

        In the future, when asking for help with coding you should post example data. I have made certain assumptions about how your data is organized. If those assumptions are incorrect, then we have both wasted our time.

        When showing data examples, please be sure to use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 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.



        When asking for help with code, always show example data. When showing example data, always use -dataex-.
        Thank you Schechter, it solve my problem also

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Something like this:
          Code:
          isid country1 country2 year
          expand 3 if year == 2015
          by country1 country2 year, sort: replace year = year + _n - 1 if year == 2015
          The resulting observations for years 2016 and 2017 would then be, except for the year variable, copies of the 2015 observation for the same country pair. You would need additional code to somehow overwrite those with the information for 2016 and 2017.
          thank you Clyde Schechter. the code helped me too.

          Comment

          Working...
          X