Announcement

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

  • Sorting Panel Data to clean errors

    I am using NLSY1979 panel data.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(caseid_1979 year weight) float weightchange
       30 1990 170 -826
      170 1990 178 -818
      637 2008 225 -315
     1207 2010 165 -426
     2290 2006 250 -200
     4120 2010 170 -295
     4263 2018 188 -214
     4429 1990 188 -808
     7673 2016 135 -300
     8151 1990 136 -860
     8220 1990 139 -857
     8331 2014 140 -200
    10480 1990 170 -826
    11984 1990 160 -836
    end

    I want to sort my variable weightchange in ascending order, but I want to still see every weight observation for that ID. This way I can determine if the extreme weight values are mistakes or if they are reasonable weights in comparison to the ID's weights in every year. Using the code
    Code:
    *sort weightchange caseid_1979
    sorts my data in data editor browse as:



    I want all the observations for each caseID to show in order from the lowest weight change to highest. So it will show the ID for every year with the weightchange of -860 rather than just the year with the dramatic weight change.
    Attached Files

  • #2
    Isn't it
    Code:
    sort caseid_1979 weightchange

    Comment


    • #3
      Duplicate.

      Comment


      • #4
        Code:
        sort caseid_1979 weightchange
        displays the data like this
        Click image for larger version

Name:	Screen Shot 2022-03-17 at 12.24.07 PM.png
Views:	1
Size:	420.3 KB
ID:	1654906 .
        Which is the format I want, but I was wanting the whole caseID observations for the ID with the lowest weight change( so the case ID that has a weight change of -860) listed first and so on...
        Last edited by Sadie Belechak; 17 Mar 2022, 11:29.

        Comment


        • #5
          So do you mean this?
          Code:
          by caseid_1979, sort: egen lowest_weightchange = min(weightchange)
          sort lowest_weightchange caseid_1979 year
          Note: I could not really test this with your example data, because in your -dataex- example, each caseid_1979 had only a single observation.

          Comment


          • #6
            Code:
            by caseid_1979, sort: egen wc_min = min(weightchange)
            sort wc_min caseid_1979 year
            list, sepby(wc_min caseid_1979)

            Comment


            • #7
              This code worked. Thank you so much.

              Code:
              by caseid_1979, sort: egen minweightchange=min(weightchange)
              sort minweightchange caseid_1979 year

              Comment

              Working...
              X