Announcement

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

  • Rescale a continuous variable given a specific range

    Dear Stata User,

    I have a continuos variable, set as below:

    Code:
    set obs 100
    gen rating = runiform()
    How to rescale a variable on a range from 2 to 27 with gaps of 3 and 22. Basically, what I want that “rating” takes values from 2 (lowest value of “rating”) to 27 (lowest value of “rating”) excluding rescaling values of 3 and 22.

    Please, advise me on this issue.

  • #2
    If it's continuous, as you say in both title and first line, then you'll have an infinitesimal chance of getting either of the two excluded integers.
    Code:
    clear *
    
    set seed `=strreverse("1442054")'
    quietly set obs 100
    
    generate double rating = runiform()
    summarize rating, meanonly
    quietly replace rating = (rating - r(min)) / (r(max) - r(min))
    quietly replace rating = 25 * rating + 2
    
    summarize rating
    
    assert !inlist(rating, 3, 22)
    By continuous, do you mean integers?
    Code:
    clear *
    
    set seed `=strreverse("1442054")'
    quietly set obs 100
    
    generate byte rating = runiformint(2, 27)
    
    quietly count if inlist(rating, 3, 22)
    while r(N) > 0 {
        quietly replace rating = runiformint(2, 27)
        quietly count if inlist(rating, 3, 22)
    }
    
    tabulate rating

    Comment

    Working...
    X