Announcement

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

  • Listing a series of 10 max and 10 min values of a variable in stata

    Hi

    I want to extract a list of my panel IDs named as "FI" and months/years named as "Time_period" having the 10 maximum and 10 minimum values corresponding to three variables i.e. X1,X2 and X3 as listed below. I have tried to look at stata forum and FAQ but could resolve this.

    I have attached a sample data set. Dataex is giving problems while showing random data for my sample.
    Time_Period FI X1 X2 X3
    2006m12 1 -31.62 -17.2541 5.59478
    2007m1 1 -31.1079 -13.6183 4.89549
    2007m2 1 -35.0292 -21.3673 3.61578
    2007m3 1 -32.0316 -13.8069 3.35394
    2007m4 1 -32.1261 -14.8359 5.03884
    2007m5 1 -34.7342 -17.2733 2.82228
    2007m6 1 -32.1319 -15.8077 9.02871
    2007m7 1 -33.2637 -16.4245 3.7428
    2007m8 1 -32.4987 -14.8021 4.67124
    2007m9 1 -30.7949 -12.8132 10.2397
    2007m10 1 -33.5104 -15.5779 4.98208
    2007m11 1 -32.8088 -17.422 2.4125
    2007m12 1 -31.9746 -15.7475 0.264498
    2008m1 1 -32.3618 -14.7923 4.21812
    2008m2 1 -31.386 -10.8896 3.05475
    2008m3 1 -32.8108 -16.8539 5.65313
    2008m4 1 -31.1901 -9.4226 3.40321
    2008m5 1 -30.9669 -9.5927 2.4106
    2008m6 1 -28.1117 -10.4687 4.33386
    2008m7 1 -37.349 -9.2677 3.50355
    2008m8 1 -28.4493 -13.0546 5.14516
    2008m9 1 -29.3784 -14.7088 2.7661
    2008m10 1 -31.0301 -20.771 4.85348
    2008m11 1 -32.0214 -18.136 1.65883
    2008m12 1 -32.5977 -15.8387 1.38131
    2009m1 1 -26.2674 -17.4087 3.35616
    2009m2 1 -31.4534 -28.3432 1.64986
    2009m3 1 -33.5362 -30.7234 1.20864
    2009m4 1 -34.3029 -39.8339 2.18494
    2009m5 1 -34.1437 -34.3469 2.75695
    2009m6 1 -34.9015 -31.897 1.6435
    2009m7 1 -32.7348 -31.4532 0.395141
    2009m8 1 -32.8876 -36.4342 3.06275
    2009m9 1 -35.1103 -35.6647 1.71369
    2009m10 1 -34.4853 -35.1553 1.77603
    2009m11 1 -33.6103 -32.3969 1.89342
    2009m12 1 -32.6687 -31.1351 2.53184
    2010m1 1 -32.9219 -33.26 0.377065
    2010m2 1 -32.7273 -30.6241 1.48177
    2007m5 2 -36.399 -12.4001 2.45553
    2007m6 2 -36.7031 -10.9524 2.52438
    2007m7 2 -35.1544 -9.8063 1.34138
    2007m8 2 -36.7315 -13.9833 1.73393
    2007m9 2 -34.6924 -13.3162 2.03121
    2007m10 2 -36.5771 -15.4347 0.903296
    2007m11 2 -35.3132 -14.3729 1.92998
    2007m12 2 -31.9243 -13.8944 2.01513
    2008m1 2 -36.4854 -11.82 2.15618
    2008m2 2 -38.7875 -9.4712 1.37465
    2008m3 2 -35.2983 -7.2655 0.164659
    2008m4 2 -34.5109 -6.7124 0.760472
    2008m5 2 -35.3267 -7.6855 2.11197
    2008m6 2 -34.2074 -6.0948 0.357734
    2008m7 2 -34.3853 -6.1205 1.33408
    2008m8 2 -32.2983 -7.9216 1.99192
    2008m9 2 -32.7456 -9.0042 1.8572
    2008m10 2 -29.3405 -13.0682 1.28423
    2008m11 2 -25.9295 -18.1566 1.18597
    2008m12 2 -34.3702 -14.1004 1.84264
    2009m1 2 -32.9606 -12.9358 1.73874
    2009m2 2 -32.4144 -12.1557 1.88879
    2009m3 2 -36.1901 -10.4371 2.30612
    2009m4 2 -39.5233 -7.6807 1.78613
    2009m5 2 -35.1506 -7.9609 2.59944
    2009m6 2 -37.4168 -8.6087 1.67847
    2009m7 2 -37.4519 -7.5728 1.84188
    2009m8 2 -36.7478 -9.7603 1.88078
    2009m9 2 -39.3216 -6.7513 1.52824
    2009m10 2 -36.3919 -5.58 1.69917
    2009m11 2 -31.529 -12.6839 2.18918
    2009m12 2 -32.2721 -11.7222 1.77936
    2010m1 2 -40.899 -10.7459 2.06669
    2010m2 2 -37.0198 -14.6874 2.58071
    2010m3 2 -37.6377 -11.0552 2.00856
    2010m4 2 -38.3496 -6.0151 1.91164
    2010m5 2 -34.9085 -12.4874 1.89597
    2010m6 2 -33.7598 -14.873 1.47095
    2010m7 2 -38.6767 -10.7353 1.82498
    2010m8 2 -37.0398 -11.1229 2.24761
    2005m5 3 -33.3961 -4.9435 1.17461
    2005m6 3 -30.5544 -12.9705 2.65861
    2005m7 3 -33.3243 -8.1051 2.3987
    2005m8 3 -30.712 -9.0695 3.06638
    2005m9 3 -32.6332 -7.1768 2.18306
    2005m10 3 -32.4904 -8.352 2.23325
    2005m11 3 -34.2676 -6.2287 1.75638
    2005m12 3 -31.7104 -10.1193 1.94831
    2006m1 3 -30.5068 -14.6299 1.12014
    2006m2 3 -32.0584 -10.8112 1.99931
    2006m3 3 -29.9282 -13.8727 2.05349
    2006m4 3 -26.8388 -20.9504 2.49089
    2006m5 3 -29.9139 -9.2954 3.16111
    2006m6 3 -33.852 -18.4747 2.85708
    2006m7 3 -35.5596 -17.5367 2.16876
    2006m8 3 -32.8254 -20.0149 1.237
    2006m9 3 -32.4573 -12.8746 1.18647
    2006m10 3 -33.3377 -11.7411 2.27633
    2006m11 3 -31.2758 -11.4121 2.04178
    2006m12 3 -33.1126 -20.9257 1.86742
    2007m1 3 -34.7782 -14.5807 2.00617
    2007m2 3 -32.2737 -17.3267 1.26322
    2007m3 3 -32.826 -14.8892 1.19135
    2007m4 3 -34.5409 -13.3523 2.16866
    2007m5 3 -33.4108 -12.2583 2.23999
    2007m6 3 -33.6427 -10.6883 2.11074
    2007m7 3 -32.3879 -10.6242 2.10911
    2007m8 3 -33.6884 -13.5362 2.03304
    2007m9 3 -32.1469 -14.2952 2.4145
    2007m10 3 -33.6297 -15.0259 2.18978
    2007m11 3 -32.5434 -14.82 2.36588
    2007m12 3 -29.9317 -16.6507 1.8595
    2008m1 3 -33.3868 -11.5976 1.73856
    2008m2 3 -34.7829 -7.6451 1.82701
    2008m3 3 -32.1477 -7.9235 1.36305
    2008m4 3 -31.4283 -7.8764 0.424538
    2008m5 3 -32.1398 -8.2825 1.43384
    2008m6 3 -31.1971 -7.4982 1.67966
    2008m7 3 -31.4153 -7.4362 2.09772
    2008m8 3 -29.7896 -10.5289 1.24054
    2008m9 3 -30.145 -11.2543 1.75509
    2008m10 3 -27.7855 -17.5109 2.33656
    2008m11 3 -24.8172 -24.4392 2.45384
    2008m12 3 -30.8208 -14.7409 2.34108
    2009m1 3 -29.4666 -14.4634 2.36558
    2009m2 3 -28.905 -14.0348 1.82533
    2009m3 3 -31.8386 -9.8923 0.981194
    2009m4 3 -34.436 -5.0643 1.65122
    2009m5 3 -30.8944 -7.1197 0.814917
    2009m6 3 -32.5789 -6.5994 2.0814
    2009m7 3 -32.5967 -6.2433 2.08569
    2009m8 3 -32.0396 -8.7694 1.36878
    2009m9 3 -34.0069 -4.1943 1.92471
    2009m10 3 -31.6576 -5.02 1.41787
    2009m11 3 -27.8475 -14.0928 1.49899
    2009m12 3 -28.4354 -12.8362 1.71027
    2010m1 3 -35.3102 -6.9316 1.95136
    2010m2 3 -32.2556 -12.4605 2.04053
    2010m3 3 -32.7135 -8.6773 1.52413
    2010m4 3 -33.2616 -4.1516 2.1554
    2010m5 3 -30.5935 -11.9555 2.79395
    2014m4 4 -11.4182 -12.4488 4.12049
    2014m5 4 -11.5161 -12.5761 5.57937
    2014m6 4 -11.2398 -12.2653 4.87862
    2014m7 4 -11.3379 -12.378 4.16158
    2014m8 4 -11.0363 -12.031 4.61227
    2014m9 4 -11.1065 -12.3985 5.32779
    2014m10 4 -10.7403 -11.7295 4.53812
    2014m11 4 -10.7211 -11.929 3.60818
    2014m12 4 -9.5328 -10.9003 1.63048
    2015m1 4 -8.52541 -9.9607 2.63183
    2015m2 4 -11.4398 -3.9184 2.0124
    2015m3 4 -10.0863 -12.3341 2.6707
    2015m4 4 -16.3682 -18.584 2.53167
    2015m5 4 -10.2965 -12.5639 1.54793
    2015m6 4 -8.99634 -11.172 2.72849
    2015m7 4 -9.52738 -14.2586 1.59571
    2015m8 4 -10.671 -12.3933 1.44922
    2015m9 4 -8.50324 -10.0703 0.445449
    2015m10 4 -11.2874 -12.6297 3.33785
    2015m11 4 -11.8667 -13.2271 3.47578
    2015m12 4 -11.24 -12.345 2.6214
    2016m1 4 -10.9375 -11.617 1.78829
    2016m2 4 -10.4502 -11.0948 3.24688
    2016m3 4 -10.4882 -11.0943 1.69079
    2016m4 4 -11.0395 -11.7848 0.991117
    2016m5 4 -10.3019 -10.7041 4.3579
    2016m6 4 -9.29865 -15.4396 3.19182
    2016m7 4 -11.2813 -11.0149 2.08944
    2016m8 4 -10.8509 -10.2114 3.99318
    2016m9 4 -11.6247 -10.8009 2.93907
    2016m10 4 -10.733 -9.7781 1.66417
    2016m11 4 -11.247 -10.2747 1.69814
    2016m12 4 -10.2298 -9.0935 2.61739
    2017m1 4 -11.2033 -9.93 2.57637
    2017m2 4 -11.6587 -10.3313 3.00756
    2017m3 4 -11.4011 -9.944 4.32675
    2017m4 4 -12.5527 -5.2793 2.33856
    2017m5 4 -11.0864 -9.9169 2.24577
    2017m6 4 -10.5698 -9.2978 3.02978
    2017m7 4 -10.4599 -8.9525 2.65721
    2017m8 4 -8.75301 -7.7424 1.95377
    2017m9 4 -9.15071 -8.1362 2.40478
    2017m10 4 -8.51483 -7.4921 2.84579
    2017m11 4 -8.44625 -7.2867 2.07805
    2017m12 4 -8.03553 -6.5698 2.53319
    2018m1 4 -9.354 -7.447 2.29817
    2018m2 4 -9.61835 -7.2637 2.03962
    2018m3 4 -8.93064 -1.9172 2.07927
    2018m4 4 -8.16795 -4.7822 2.11018
    2018m5 4 -7.93117 -4.5514 0.908515
    2018m6 4 -6.2193 -2.8093 1.66997
    2018m7 4 -8.62767 -4.6205 1.7408
    2018m8 4 -11.4251 -7.4867 1.66439
    2018m9 4 -10.5638 -5.9207 2.6215
    2018m10 4 -11.0462 -6.652 1.98961
    2018m11 4 -10.7081 -5.8977 1.42967
    2018m12 4 -9.7917 -4.3473 2.19367
    2019m1 4 -10.7452 -5.1528 2.20975
    2019m2 4 -7.24964 -1.8362 2.09057
    2019m3 4 -11.2934 -5.579 1.71869
    2019m4 4 -10.6879 -5.7262 2.03399
    2019m5 4 0 4.55039
    2019m6 4 0 4.58949
    2019m7 4 0 3.14705
    2019m8 4 0 5.42092

    Best Regards

  • #2
    Sort and list

    Code:
    sort var
    l var in 1/10
    l var in -10/l

    Comment


    • #3
      I am fuzzy about exactly what is wanted here, and the absence of a good data example using dataex is inhibiting

      -- a quick check on your previous threads shows repeated mentions of dataex or requests to read https://www.statalist.org/forums/help#stata --

      but I flag the existence of extremes on SSC. e.g.

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . ssc install extremes 
      
      . extremes mpg make
      
        +--------------------------------+
        | obs:   mpg   make              |
        |--------------------------------|
        |  26.    12   Linc. Continental |
        |  27.    12   Linc. Mark V      |
        |  11.    14   Cad. Deville      |
        |  12.    14   Cad. Eldorado     |
        |  28.    14   Linc. Versailles  |
        +--------------------------------+
      
        +-----------------------------+
        |  68.    31   Toyota Corolla |
        |  43.    34   Plym. Champ    |
        |  57.    35   Datsun 210     |
        |  66.    35   Subaru         |
        |  71.    41   VW Diesel      |
        +-----------------------------+
      
      note: 6 values of 14
      
      . extremes mpg make, n(10)
      
        +--------------------------------+
        | obs:   mpg   make              |
        |--------------------------------|
        |  26.    12   Linc. Continental |
        |  27.    12   Linc. Mark V      |
        |  11.    14   Cad. Deville      |
        |  12.    14   Cad. Eldorado     |
        |  28.    14   Linc. Versailles  |
        |--------------------------------|
        |  30.    14   Merc. Cougar      |
        |  33.    14   Merc. XR-7        |
        |  64.    14   Peugeot 604       |
        |   5.    15   Buick Electra     |
        |  31.    15   Merc. Marquis     |
        +--------------------------------+
      
        +-----------------------------+
        |  42.    28   Plym. Arrow    |
        |  62.    28   Honda Civic    |
        |  14.    29   Chev. Chevette |
        |  20.    30   Dodge Colt     |
        |  63.    30   Mazda GLC      |
        |-----------------------------|
        |  68.    31   Toyota Corolla |
        |  43.    34   Plym. Champ    |
        |  57.    35   Datsun 210     |
        |  66.    35   Subaru         |
        |  71.    41   VW Diesel      |
        +-----------------------------+
      
      note: 3 values of 28
      Last edited by Nick Cox; 16 Oct 2019, 04:55.

      Comment


      • #4
        Thank you both !

        Indeed it was as simple as it is.

        Nick Cox I tried to use dataex (randomtag) but could not sort how to extract random data to show substantial panel IDs and time periods.

        Best Regards

        Comment


        • #5
          If one listing is desired, Andrew's recommendation can be compressed somewhat:

          Code:
          sysuse auto
          sort mpg
          list mpg if inrange(_n, 1, x) | inrange(_n, _N-x+1, _N)  // replace x with whatever number of extreme values you want.

          Comment


          • #6
            it doesn't bite with with the example in #5 -- which uses a trick that deserves to be better known -- but more general code has to be deal with

            * missing values --- which in this context are rarely of interest

            * other if (or occasionally in) conditions.

            Comment


            • #7
              #4 No call for anything subtle. From #1 you make clear that FI is the panel identifier so something like

              Code:
              dataex FI TimePeriod X1 X2 X3  if FI <= 3
              should work fine, including showing the time variable in a way that is easy to read in.

              Comment

              Working...
              X