Announcement

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

  • Sorting data within case id

    Hello All,

    I wish to sort 'choice' by 'random' within each 'caseid', fixing 'id', 'caseid', and 'choiceset'.
    I'd greatly appreciate it if someone could help me with this.

    Thank you.


    id caseid choiceset choice random
    1 1 1 3 2
    1 1 1 3 2
    1 1 1 3 2
    1 2 2 2 5
    1 2 2 2 5
    1 2 2 2 5
    1 3 3 3 7
    1 3 3 3 7
    1 3 3 3 7
    1 4 4 2 10
    1 4 4 2 10
    1 4 4 2 10
    1 5 5 3 9
    1 5 5 3 9
    1 5 5 3 9
    1 6 6 3 1
    1 6 6 3 1
    1 6 6 3 1
    1 7 7 1 6
    1 7 7 1 6
    1 7 7 1 6
    1 8 8 1 3
    1 8 8 1 3
    1 8 8 1 3
    1 9 9 2 8
    1 9 9 2 8
    1 9 9 2 8
    1 10 10 2 4
    1 10 10 2 4
    1 10 10 2 4
    2 11 1 3 8
    2 11 1 3 8
    2 11 1 3 8
    2 12 2 2 1
    2 12 2 2 1
    2 12 2 2 1
    2 13 3 3 9
    2 13 3 3 9
    2 13 3 3 9
    2 14 4 2 3
    2 14 4 2 3
    2 14 4 2 3
    2 15 5 3 5
    2 15 5 3 5
    2 15 5 3 5
    2 16 6 1 10
    2 16 6 1 10
    2 16 6 1 10
    2 17 7 1 4
    2 17 7 1 4
    2 17 7 1 4
    2 18 8 1 6
    2 18 8 1 6
    2 18 8 1 6
    2 19 9 3 2
    2 19 9 3 2
    2 19 9 3 2
    2 20 10 7
    2 20 10 7
    2 20 10 7

  • #2
    Read -help sort-. I don't know what you mean by "fixing" id, caseid and choiceset. Perhaps this is what you want.

    Code:
    sort id caseid choiceset choice random
    Then look at your data (or a reduced sample of it) to see if that's what you intended.

    Comment


    • #3
      Thank you for your response, Leonardo.
      I apologize for not being clear about my question.
      Left is the original dataset, and right is what I wish to accomplish.


      id caseid choiceset choice random id caseid choiceset choice random
      1 1 1 3 2 1 1 1 3 1
      1 1 1 3 2 1 1 1 3 1
      1 1 1 3 2 1 1 1 3 1
      1 2 2 2 5 1 2 2 3 2
      1 2 2 2 5 1 2 2 3 2
      1 2 2 2 5 1 2 2 3 2
      1 3 3 3 7 1 3 3 1 3
      1 3 3 3 7 1 3 3 1 3
      1 3 3 3 7 1 3 3 1 3
      1 4 4 2 10 1 4 4 2 4
      1 4 4 2 10 1 4 4 2 4
      1 4 4 2 10 1 4 4 2 4
      1 5 5 3 9 1 5 5 2 5
      1 5 5 3 9 1 5 5 2 5
      1 5 5 3 9 1 5 5 2 5
      1 6 6 3 1 1 6 6 1 6
      1 6 6 3 1 1 6 6 1 6
      1 6 6 3 1 1 6 6 1 6
      1 7 7 1 6 1 7 7 3 7
      1 7 7 1 6 1 7 7 3 7
      1 7 7 1 6 1 7 7 3 7
      1 8 8 1 3 1 8 8 2 8
      1 8 8 1 3 1 8 8 2 8
      1 8 8 1 3 1 8 8 2 8
      1 9 9 2 8 1 9 9 3 9
      1 9 9 2 8 1 9 9 3 9
      1 9 9 2 8 1 9 9 3 9
      1 10 10 2 4 1 10 10 2 10
      1 10 10 2 4 1 10 10 2 10
      1 10 10 2 4 1 10 10 2 10
      2 11 1 3 8 2 11 1 2 1
      2 11 1 3 8 2 11 1 2 1
      2 11 1 3 8 2 11 1 2 1
      2 12 2 2 1 2 12 2 3 2
      2 12 2 2 1 2 12 2 3 2
      2 12 2 2 1 2 12 2 3 2
      2 13 3 3 9 2 13 3 2 3
      2 13 3 3 9 2 13 3 2 3
      2 13 3 3 9 2 13 3 2 3
      2 14 4 2 3 2 14 4 1 4
      2 14 4 2 3 2 14 4 1 4
      2 14 4 2 3 2 14 4 1 4
      2 15 5 3 5 2 15 5 3 5
      2 15 5 3 5 2 15 5 3 5
      2 15 5 3 5 2 15 5 3 5
      2 16 6 1 10 2 16 6 1 6
      2 16 6 1 10 2 16 6 1 6
      2 16 6 1 10 2 16 6 1 6
      2 17 7 1 4 2 17 7 7
      2 17 7 1 4 2 17 7 7
      2 17 7 1 4 2 17 7 7
      2 18 8 1 6 2 18 8 3 8
      2 18 8 1 6 2 18 8 3 8
      2 18 8 1 6 2 18 8 3 8
      2 19 9 3 2 2 19 9 3 9
      2 19 9 3 2 2 19 9 3 9
      2 19 9 3 2 2 19 9 3 9
      2 20 10 7 2 20 10 1 10
      2 20 10 7 2 20 10 1 10
      2 20 10 7 2 20 10 1 10

      Comment


      • #4
        OK. What you want to do is not sorting, at least not in the usual sense of the term. In fact, what you want to do is scramble your data. You want to break the existing associations between id caseid and choiceset on the one hand, and choice and random on the other. In the results you desire, id, caseid, and choiceset remain in their original order, but these same values are now associated with different values of choice and random. The values of choice and random are sorted on random within id. (choice never varies within id and random, at least not in the example data, so it isn't clear whether there is any intent to further sort on choice within random. The code shown below does do that additional sort, even though in the example data it has no effect.)
        Code:
        clear*
        input id caseid choiceset choice random
        1     1     1     3     2
        1     1     1     3     2
        1     1     1     3     2
        1     2     2     2     5
        1     2     2     2     5
        1     2     2     2     5
        1     3     3     3     7
        1     3     3     3     7
        1     3     3     3     7
        1     4     4     2     10
        1     4     4     2     10
        1     4     4     2     10
        1     5     5     3     9
        1     5     5     3     9
        1     5     5     3     9
        1     6     6     3     1
        1     6     6     3     1
        1     6     6     3     1
        1     7     7     1     6
        1     7     7     1     6
        1     7     7     1     6
        1     8     8     1     3
        1     8     8     1     3
        1     8     8     1     3
        1     9     9     2     8
        1     9     9     2     8
        1     9     9     2     8
        1     10     10     2     4
        1     10     10     2     4
        1     10     10     2     4
        2     11     1     3     8
        2     11     1     3     8
        2     11     1     3     8
        2     12     2     2     1
        2     12     2     2     1
        2     12     2     2     1
        2     13     3     3     9
        2     13     3     3     9
        2     13     3     3     9
        2     14     4     2     3
        2     14     4     2     3
        2     14     4     2     3
        2     15     5     3     5
        2     15     5     3     5
        2     15     5     3     5
        2     16     6     1     10
        2     16     6     1     10
        2     16     6     1     10
        2     17     7     1     4
        2     17     7     1     4
        2     17     7     1     4
        2     18     8     1     6
        2     18     8     1     6
        2     18     8     1     6
        2     19     9     3     2
        2     19     9     3     2
        2     19     9     3     2
        2     20     10     .    7
        2     20     10     .    7
        2     20     10     .    7
        end
        
        gen `c(obs_t)' obs_no = _n
        
        frame put obs_no id choice random, into(scramble)
        frame scramble {
            sort id random choice
            replace obs_no = _n
        }
        frlink 1:1 obs_no, frame(scramble)
        drop choice random
        frget choice random, from(scramble)
        drop scramble obs_no
        frame drop scramble
        In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 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.

        Comment


        • #5
          Hello Clyde,

          It worked like a charm! I've been struggling with this for almost two weeks... I cannot thank you enough!
          I will be sure to use the -dataex- command in the future!

          Comment

          Working...
          X