Announcement

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

  • Sort rows of variables subset

    I currently have the following data:

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id BlockOrder1 BlockOrder2 BlockOrder3 BlockOrder4 BlockOrder5 Keuze1 Keuze2 Keuze3 Keuze4 Keuze5)
    1 2 3 15 9 4 1 2 2 2 1
    2 14 1 2 6 9 1 2 2 1 2
    3 15 16 4 1 14 1 2 2 1 2
    4 11 10 9 16 6 1 1 2 1 2
    5 14 7 13 2 12 2 2 1 2 1
    end


    The data is of discrete choice type, with BlockOrder indicating which choice task number was randomly assigned to respondent i and Keuze the final choice (either 1 or 2) of respondent i. In the example I added 5 order/choice variables but the full dataset has 16.

    For each respondent id or row, I want to generate y1-16, indicating which choice (i.e. Keuze*) was made but sorted according to the randomized order of choice tasks each respondent was presented with.

    For example, Keuze1 for respondent 1 now returns the choice made in choice task 2, since respondent 1 was first presented with this one (then choice task 3, 15, 9, 4 etc. until the 16 were finished). Choice task 1 was presented 6th, i.e. at Keuze6. Y1 needs to indicate choice task 1, y2 choice task 2 etc.

    Any suggestions on how to do this? Thank you in advance.
    Last edited by Annamarie de Ruijter; 18 Jul 2023, 08:32.

  • #2

    I've created a new data example in which the BlockOrder variables have the same range (1, ..., 5) as your number of responses, which gave me something to work with. Does the following do what you want with the example data I have here?

    Code:
    clear
    input byte (id BlockOrder1 BlockOrder2 BlockOrder3 BlockOrder4 BlockOrder5 Keuze1 Keuze2 Keuze3 Keuze4 Keuze5)
    1 2 3 5 4 1 1 2 2 2 1
    2 4 3 1 2 5 1 2 2 1 2
    3 5 3 1 4 2 1 2 2 1 2
    4 1 5 3 2 4 1 1 2 1 2
    5 5 4 3 2 1 2 2 1 2 1
    end
    //
    list
    // -quiet- makes it easier to "eyeball" the two data layouts
    quiet {
        reshape long Keuze BlockOrder, i(id) j(seq)
        drop seq
        gen y = Keuze
        drop Keuze
        reshape wide y, i(id) j(BlockOrder)
    }
    list
    
        +--------------------------------------------------------------------------------------------------------+
         | id   BlockO~1   BlockO~2   BlockO~3   BlockO~4   BlockO~5   Keuze1   Keuze2   Keuze3   Keuze4   Keuze5 |
         |--------------------------------------------------------------------------------------------------------|
      1. |  1          2          3          5          4          1        1        2        2        2        1 |
      2. |  2          4          3          1          2          5        1        2        2        1        2 |
      3. |  3          5          3          1          4          2        1        2        2        1        2 |
      4. |  4          1          5          3          2          4        1        1        2        1        2 |
      5. |  5          5          4          3          2          1        2        2        1        2        1 |
         +--------------------------------------------------------------------------------------------------------+
    
    . list
    
        +-----------------------------+
         | id   y1   y2   y3   y4   y5 |
         |-----------------------------|
      1. |  1    1    1    2    2    2 |
      2. |  2    2    1    2    1    2 |
      3. |  3    2    2    2    1    1 |
      4. |  4    1    1    2    2    1 |
      5. |  5    1    2    1    2    2 |
         +-----------------------------+

    Comment


    • #3
      It does, thank you for helping me out!

      Comment

      Working...
      X