Announcement

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

  • Generate random value stata

    Dear collegues,

    I'm working on a consumer survey. In this survey, we asked to consumers to rank their three preferred labelling schemes among a pool of ten alternatives.

    So, for each consumers, we have his rank 1, rank 2 and rank 3. In the exemple provide below, the consumer 1 ranked the alternative 7 at rank1, the alternative 5 at rank 2 and the alternative 4 at rank 3.

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	3.1 KB
ID:	1696892


    My question is: Do you know how I can generate random variable (value between 1 and 10) without taking the values taken in the previous rows?

    For exemple, here, i want to generate a value for the rank 4 between 1 and 10 without taking the value 7, 5 and 2. For the rank 5, generate value between 1 and 10 without taking the value 7,5, 2 and the one generate for rank 4 etc.

    I hope I have made myself clear I remain available for any further informations,

    Regards

    Jean-François DEWALS

  • #2
    Do you need to do this over multiple observations? It may be easier to convert the dataset to long layout first so that you do the assignment only once and reshape back to wide if need be. But first present a data example using the dataex command (e.g., with 5 observations if you have multiple observations).

    Comment


    • #3
      Thanks for your response

      Yes, i have multiple observations in my dataset (1500 individuals).

      My data exemple for the first 5 individuals :

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int record float(rang1 rang2 rang3 rang4 rang5 rang6 rang7 rang8 rang9 rang10)
      400  7 5 4 . . . . . . .
      404  7 6 1 . . . . . . .
      408  7 5 1 . . . . . . .
      409  2 1 6 . . . . . . .
      411  9 2 5 . . . . . . .
      ....
      end
      Thank for your help, i remain available

      Jean-François

      Comment


      • #4


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int record float(rang1 rang2 rang3 rang4 rang5 rang6 rang7 rang8 rang9 rang10)
        400 7 5 4 . . . . . . .
        404 7 6 1 . . . . . . .
        408 7 5 1 . . . . . . .
        409 2 1 6 . . . . . . .
        411 9 2 5 . . . . . . .
        end
        
        reshape long rang, i(record) j(which)
        frame put record rang, into(new)
        frame new: rename rang which
        frame new: drop if missing(which)
        frlink 1:1 record which, frame(new)
        gen double rand= rnormal()
        drop if !missing(new)
        sort record rand
        rename (which rang) (rang which)
        by record: replace which=_n+3
        frame drop new
        drop rand new
        reshape wide rang, i(record) j(which)
        Res.:

        Code:
        
        
        . l
        
             +-----------------------------------------------------------------+
             | record   rang4   rang5   rang6   rang7   rang8   rang9   rang10 |
             |-----------------------------------------------------------------|
          1. |    400       2       6       9       1       8      10        3 |
          2. |    404       3       4      10       8       2       9        5 |
          3. |    408       2      10       8       6       9       4        3 |
          4. |    409       3       7       9       4       5      10        8 |
          5. |    411       8       6       1       3       7      10        4 |
             +-----------------------------------------------------------------+
        Last edited by Andrew Musau; 12 Jan 2023, 05:44.

        Comment


        • #5
          Thanks a lot, it works! And thanks for all your contributions on this forum ! Really helpfull for beginner like me =)

          Jean-François

          Comment

          Working...
          X