Announcement

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

  • Do Not Rank If Condition

    Hello,

    In the sample attached I have ranked var: tfr_lr in var: rank_tfr_lr
    I would like var: tfr_lr not to be ranked if the var: code is a 'j'. Then it should be blank.
    var 5 column is there to show what I would like var: rank_tfr_lr to look like ideally.

    Thank you.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 time byte(tfr_lr rank_tfr_lr) str1 code byte var5
    "17:20" 81 3 "f" 3
    "17:20" 72 7 "f" 7
    "17:20" 75 5 "f" 5
    "17:20" 62 8 "f" 8
    "17:20" 81 3 "f" 3
    "17:20" 51 9 "j" .
    "17:20" 89 1 "f" 1
    "17:20" 87 2 "f" 2
    "17:20" 74 6 "f" 6
    end

  • #2
    Hans:
    the condition you impose bears on -rank- from -egen- function calculation:
    Code:
    . egen wanted=rank( tfr_lr) if code !="j"
    (1 missing value generated)
    
    . list
    
         +--------------------------------------------------+
         |  time   tfr_lr   rank_t~r   code   var5   wanted |
         |--------------------------------------------------|
      1. | 17:20       81          3      f      3      5.5 |
      2. | 17:20       72          7      f      7        2 |
      3. | 17:20       75          5      f      5        4 |
      4. | 17:20       62          8      f      8        1 |
      5. | 17:20       81          3      f      3      5.5 |
         |--------------------------------------------------|
      6. | 17:20       51          9      j      .        . |
      7. | 17:20       89          1      f      1        8 |
      8. | 17:20       87          2      f      2        7 |
      9. | 17:20       74          6      f      6        3 |
         +--------------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Thanks very much, Carlo. Much appreciated.

      Comment

      Working...
      X