Announcement

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

  • Generate unique ID of 4 digits

    Hello,

    My data doesn't have a unique identifier and I want to generate a unique id of each row of 4 digits (0001, 0002, 0003..... 1114, etc), so I can later merge the ID with other elements such as State Name, (NY0001, NY0002, ... NY1114 etc). I am using _n but only generates a number of length 1.

    Thanks,

    Tania

  • #2
    Using something like

    Code:
    gen int id = _n
    Is a sensible way to assign a unique and incremental I'd number to your observations. The numeric id can also be useful for later subsetting without worrying about string operations.

    If you want to make text-based id with the padded zeroes, this link will give you a start in the right direction.

    It will look something like this (note: code is untested and may come with typos).

    Code:
    gen int id = _n
    Gen str10 sid = "ABC" + string(id, "%04.0f")

    Comment


    • #3
      Thanks Lenardo!

      Following your advice. This was my final code:

      gen STATE3 = substr(STATE, 1, 3)

      gen str3 id3 = string(_n,"%04.0f")

      gen unique_id = STATE3 + id3

      Comment

      Working...
      X