Announcement

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

  • how to replace/recode variables that are a multiple of some number

    Hi I have a variable "level" that ranges from 1 to 125
    I would like to recode it into a variable "pitch", such that values 1, 6, 11, 16, 21 ... etc are coded as "1" for pitch and values 2, 7, 12, 17, 22... etc ared coded as "2" for pitch
    I could do that manually with recode e.g.
    HTML Code:
    recode level 1=1 6=1 11=1 etc, gen(obj)
    but it would take a long time

    Is there a quicker way to do this?

  • #2
    Wendy, you may generate "pitch" based on the modulus of "level" with respect to 5.

    Code:
    gen pitch = mod(level, 5)
    If you need to recode 5, 10, 15, ..., 125 to 5, then add another line of code:

    Code:
    replace pitch = 5 if pitch == 0

    Comment


    • #3
      Thank you so much!
      You don't know how many hours I've wasted playing around with different commands to figure this out!

      Comment


      • #4
        Functions range from those you know you want -- if you want a square root or logarithm, the usual issue is only exactly what is Stata's syntax? -- to those you don't know you need.

        A personal survey of often useful functions appeared in https://www.stata-journal.com/articl...article=dm0058 (pdf freely downloadable) while an incomplete riff on mod() appeared in https://www.stata-journal.com/articl...article=pr0031 (ditto). (I should have mentioned rotations.)

        Comment

        Working...
        X