Announcement

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

  • not sorted error in spite of xtset

    Dear Stata experts,

    I have data on bilateral trade for different sectors. Here is the sample of my data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id year HS2) long(reporternr partnernr) double mean_tariff float ln_m_tariff
    44685 1992 86 19 1     .          .
    17480 2005  9  8 1  3.07  1.1216775
    32398 2014 34 14 1  1.91   .6471033
    12518 2015 82  6 1   1.9   .6418539
    10069 1991 83  5 1 18.56   2.921009
     7476 1990 37  4 1     .          .
    32410 1994 46 14 1     .          .
    34819 1993  5 15 1  9.75  2.2772672
    12507 2011 70  6 1  1.77  .57097954
    42242 2016 89 18 1  1.18  .16551444
    17480 2009  9  8 1  3.67  1.3001916
    15067 1999 83  7 1   6.1  1.8082888
     5024 2007 35  3 1  3.45  1.2383742
    37283 1990 23 16 1     .          .
    10038 2017 51  5 1   .89 -.11653382
     7467 2014 28  4 1  1.91   .6471033
     7522 2014 84  4 1  1.91   .6471033
    22443 2001 72 10 1   4.8   1.568616
    17480 2013  9  8 1  1.84   .6097656
    12486 2008 49  6 1  3.15  1.1474024
     7459 2017 20  4 1   .89 -.11653382
    37313 2009 53 16 1  3.67  1.3001916
    29823 2003  7 13 1  3.74  1.3190856
     5047 2012 58  3 1  1.81  .59332687
    15061 1994 76  7 1     .          .
    17562 1998 92  8 1  6.49  1.8702625
    22379 2002  8 10 1  3.69  1.3056265
     5052 2003 63  3 1  3.74  1.3190856
    22397 1991 26 10 1 18.56   2.921009
    17481 1991 10  8 1 18.56   2.921009
    57124 2010 30 24 1   2.8  1.0296195
    32414 2013 50 14 1  1.84   .6097656
    42228 2007 74 18 1  3.45  1.2383742
    52241 2001 42 22 1   4.8   1.568616
    19974 2005 53  9 1  3.07  1.1216775
    54743 2015 95 23 1   1.9   .6418539
    37316 2005 56 16 1  3.07  1.1216775
    44649 2003 49 19 1  3.74  1.3190856
    52227 2009 28 22 1  3.67  1.3001916
    34863 2010 49 15 1   2.8  1.0296195
    29866 2011 50 13 1  1.77  .57097954
    49673 2000  8 21 1  5.83   1.763017
    57109 2006 15 24 1  3.38  1.2178757
    22387 1999 16 10 1   6.1  1.8082888
    22413 1994 42 10 1     .          .
    29822 2018  6 13 1   .86  -.1508229
    17559 2016 89  8 1  1.18  .16551444
    34903 2009 90 15 1  3.67  1.3001916
    17481 1995 10  8 1     .          .
    22390 2003 19 10 1  3.74  1.3190856
    end
    label values reporternr reporternr
    label def reporternr 3 "BEL", modify
    label def reporternr 4 "CAN", modify
    label def reporternr 5 "CHE", modify
    label def reporternr 6 "CHN", modify
    label def reporternr 7 "CZE", modify
    label def reporternr 8 "DEU", modify
    label def reporternr 9 "DNK", modify
    label def reporternr 10 "ESP", modify
    label def reporternr 13 "GBR", modify
    label def reporternr 14 "ITA", modify
    label def reporternr 15 "JPN", modify
    label def reporternr 16 "KOR", modify
    label def reporternr 18 "NOR", modify
    label def reporternr 19 "NZL", modify
    label def reporternr 21 "RUS", modify
    label def reporternr 22 "SWE", modify
    label def reporternr 23 "TUR", modify
    label def reporternr 24 "USA", modify
    label values partnernr partnernr
    label def partnernr 1 "AUS", modify
    I wanted to have year-on-year logarithmic difference of importer's tariff. In the beginning I used the code:

    Code:
    xtset id year
    gen D_ln_m_tariff=ln_m_tariff-L.ln_m_tariff
    I noticed it's not correct though. It should be for every importer (here partnernr). In this case it's just taking the lagged values within the whole panel. I used the code:

    Code:
    xtset id year
    bysort partnernr: gen D_ln_m_tariff=ln_m_tariff-L.ln_m_tariff
    I saw an error: not sorted

    I sorted partnernr and year. But wasn't successful.

    I generated id by grouping exporter, importer and sector.

  • #2
    I've just changed the dataset with tariffs, year and partner only which I previously merged with my master data. However, I would be happy to know more elegant solution

    Comment


    • #3
      Guest:
      as -AUS- is coded 1 in the whole dataset, my guess is that Stata finds nothing to sort by.
      Conversely, the following works:
      Code:
      . bysort id (year): gen D_ln_m_tariff=ln_m_tariff-L.ln_m_tariff
      (50 missing values generated)
      Last edited by sladmin; 28 Aug 2023, 08:31. Reason: anonymize original poster
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Code:
        xtset id year

        implies

        Code:
        sort id year
        and is in general inconsistent with any sort based on other variables. So, if you change the sort order thereafter anything dependent on the xtset definition becomes impossible. That includes use of time series operators.

        @Carlo Lazzaro's solution

        Code:
        bysort id (year): gen D_ln_m_tariff=ln_m_tariff-L.ln_m_tariff
        is entirely consistent with

        Code:
        xtset id year
        but could just be




        Code:
        gen D_ln_m_tariff=ln_m_tariff-L.ln_m_tariff
        That may help to explain the first error in #1. I doubt it explains how to do what you want, which I don't understand.


        Comment


        • #5
          Thank you Carlo and Nick for your answers. I mean I wanted to have log-on-year difference on tariff. Here is the dataset on tariffs only:

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str3 partner int year double mean_tariff
          "AUS" 1990     .
          "AUS" 1991 18.56
          "AUS" 1992     .
          "AUS" 1993  9.75
          "AUS" 1994     .
          "AUS" 1995     .
          "AUS" 1996  6.81
          "AUS" 1997  6.82
          "AUS" 1998  6.49
          "AUS" 1999   6.1
          "AUS" 2000  5.83
          "AUS" 2001   4.8
          "AUS" 2002  3.69
          "AUS" 2003  3.74
          "AUS" 2004  3.69
          "AUS" 2005  3.07
          "AUS" 2006  3.38
          "AUS" 2007  3.45
          "AUS" 2008  3.15
          "AUS" 2009  3.67
          "AUS" 2010   2.8
          "AUS" 2011  1.77
          "AUS" 2012  1.81
          "AUS" 2013  1.84
          "AUS" 2014  1.91
          "AUS" 2015   1.9
          "AUS" 2016  1.18
          "AUS" 2017   .89
          "AUS" 2018   .86
          "AUT" 1990   4.8
          "AUT" 1991   4.5
          "AUT" 1992  4.18
          "AUT" 1993  3.79
          "AUT" 1994  4.16
          "AUT" 1995  6.28
          "AUT" 1996   4.4
          "AUT" 1997  3.66
          "AUT" 1998  3.47
          "AUT" 1999  3.48
          "AUT" 2000  2.41
          "AUT" 2001  3.37
          "AUT" 2002  2.45
          "AUT" 2003  2.52
          "AUT" 2004  2.24
          "AUT" 2005  2.46
          "AUT" 2006   2.3
          "AUT" 2007  2.24
          "AUT" 2008  1.72
          "AUT" 2009  1.85
          "AUT" 2010  2.13
          "AUT" 2011  1.59
          "AUT" 2012  1.46
          "AUT" 2013  1.49
          "AUT" 2014  2.03
          "AUT" 2015  2.02
          "AUT" 2016  2.09
          "AUT" 2017  1.96
          "AUT" 2018  1.83
          "BEL" 1990   4.8
          "BEL" 1991   4.5
          "BEL" 1992  4.18
          "BEL" 1993  3.79
          "BEL" 1994  4.16
          "BEL" 1995  6.28
          "BEL" 1996   4.4
          "BEL" 1997  3.66
          "BEL" 1998  3.47
          "BEL" 1999  3.48
          "BEL" 2000  2.41
          "BEL" 2001  3.37
          "BEL" 2002  2.45
          "BEL" 2003  2.52
          "BEL" 2004  2.24
          "BEL" 2005  2.46
          "BEL" 2006   2.3
          "BEL" 2007  2.24
          "BEL" 2008  1.72
          "BEL" 2009  1.85
          "BEL" 2010  2.13
          "BEL" 2011  1.59
          "BEL" 2012  1.46
          "BEL" 2013  1.49
          "BEL" 2014  2.03
          "BEL" 2015  2.02
          "BEL" 2016  2.09
          "BEL" 2017  1.96
          "BEL" 2018  1.83
          "CAN" 1990     .
          "CAN" 1991     .
          "CAN" 1992     .
          "CAN" 1993  3.59
          "CAN" 1994     .
          "CAN" 1995  6.69
          "CAN" 1996  5.43
          "CAN" 1997  1.99
          "CAN" 1998  1.44
          "CAN" 1999  1.01
          "CAN" 2000  1.31
          "CAN" 2001   1.6
          "CAN" 2002  1.49
          end

          after merging it with my master data I realized that I wanted log-on-year differences on tariffs. The problem is that it would take differences e.g. between Austria and Belgium. I wanted to have year differences only within the same countries. I decided to compute it in the using data set. I used a code:

          Code:
          gen ln_m_tariff=ln(mean_tariff)
          encode partner, gen(partnernr)
          xtset partnernr year
          gen D_l_tar=ln_m_tariff-L.ln_m_tariff
          and then merged with my master data. I was wondering if it was possible to do it when datasets have been merged. If this question sounds annoying- pardon me. I'm just doing my best to work efficiently in Stata

          Comment


          • #6
            once you xtset, it won't lag outside of the country.

            Code:
            egen cid = group(partner)
            order cid, after(partner)
            g ltariff = ln(mean_tariff)
            xtset cid year
            g dltariff = ltariff - l.ltariff

            Comment


            • #7
              Thank you George! I wouldn't come up with the idea to group partner

              Comment


              • #8
                xtset no-like the strings

                Comment

                Working...
                X