Announcement

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

  • Replacing the values in a variable with ordered numbers

    Hello, I have a variable which contains random numbers in each value, also there are missing values between each of the values in the variable. I would like to replace each value in the variable with ordered numbers.

    For example if I have 'n' values in the variable, I would like the first appearing value to be '1' and the second appearing value to be '2' all the way up to 'n'. What code can I use to implement this?

  • #2
    I'm a bit confused, and you don't say what you want to do when the value is missing, but the following will do what I think you are asking:
    Code:
    replace varname=_n
    or, if you don't want to replace the missing values:
    Code:
    replace varname=_n if varna me<.
    obviously, you should replace "varna me" in the above with whatever the actual name of the variable is

    Comment


    • #3
      Hi there, thanks for your reply, I don't want to do anything to the missing values.

      The problem with the code you provided is that its gives values that are equal to _n, I have entries that are separated by missing values but I want them to be ordered numerically for example 1,2,3. But the code you gave makes these values for example 1, 44, 98. I want them to be ordered even though they are separated by missing values.

      Comment


      • #4
        Perhaps this:
        Code:
        replace varname = sum(!missing(varname))

        Comment


        • #5
          This works! Thanks Guys!

          Comment

          Working...
          X