Announcement

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

  • random selection between observation

    Hi everyone,
    In the below dataset, I want to randomly delete one of the observations that has the same wage for a given year. For example, I would be happy if Stata randomly drops business a or b for year 93. Do you have any idea?
    thanks a lot.

    year wage business
    93 100 a
    93 100 b
    94 150 c
    95 200 d
    95 250 e
    96 300 f


  • #2
    Code:
    * read your example data
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte year int wage str1 business
    93 100 "a"
    93 100 "b"
    94 150 "c"
    95 200 "d"
    95 250 "e"
    96 300 "f"
    end
    
    * do the task
    generate long seq = _n
    set seed 42
    generate float rseq = runiform()
    sort year wage rseq
    by year wage (rseq), sort: generate todrop = _n>1
    sort seq
    drop if todrop
    drop seq rseq todrop
    list, clean noobs
    Code:
    . list, clean noobs
    
        year   wage   business  
          93    100          b  
          94    150          c  
          95    200          d  
          95    250          e  
          96    300          f
    An alternative approach.
    Code:
    * do the task
    generate long seq = _n
    set seed 42
    generate float rseq = runiform()
    sort year wage rseq
    duplicates drop year wage, force
    sort seq
    drop seq rseq
    list, clean noobs
    Code:
    . list, clean noobs
    
        year   wage   business  
          93    100          b  
          94    150          c  
          95    200          d  
          95    250          e  
          96    300          f
    Last edited by William Lisowski; 11 Jan 2023, 13:09.

    Comment


    • #3
      Code:
      set seed 12012023
      sample 1, count by(year wage)

      Comment


      • #4
        Wow, I reinvented the wheel two different ways - hexgonal and pentagonal. The round wheel from Romalpa Akzo wins.

        Comment

        Working...
        X