Announcement

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

  • _n not referring to observation number

    Hello!

    I have run into an issue where _n does not refer to the observation number.

    The commands and results I have seen are listed below:

    . gen id = _n

    . format id %22.0f

    . list id in 16777213 / 16777219


    ---ob no----+-----id-------+
    16777213. | 16777213 |
    16777214. | 16777214 |
    16777215. | 16777215 |
    16777216. | 16777216 |
    16777217. | 16777216 |
    16777218. | 16777218 |
    16777219. | 16777220 |
    ---------------+---------------+

    Is there a particular reason why this would be happening? I have tried other methods too such as the one below but always get similar results.

    . gen id = 1

    . gen sumid = sum(id)

    . format id sumid %22.0f

    . list id sumid in 16777213 / 16777219

    ---ob no----+-id---sumid---+
    16777213. | 1 16777213 |
    16777214. | 1 16777214 |
    16777215. | 1 16777215 |
    16777216. | 1 16777216 |
    16777217. | 1 16777216 |
    16777218. | 1 16777218 |
    16777219. | 1 16777220 |
    ----------------+-----------------+

    My ultimate goal would be to generate a variable with a unique id for each of the observations. Would there be any way to work around this?

    Thank you for your time and help!
    Last edited by Taewan Roh; 16 Apr 2015, 13:31.

  • #2
    It's a precision issue.
    See this posting for some background (http://blog.stata.com/tag/precision/) and -help precision-, but consider this example where specifying 'double' format for the variable "id" works:

    ********************
    clear
    set obs `=10^8'

    **doesnt work:
    . gen id = _n
    . format id %22.0f
    . list id in 16777213 / 16777219


    **works:
    g double id2 = _n
    . format id2 %22.0f
    . list id2 in 16777213 / 16777219
    ********************


    Last edited by eric_a_booth; 16 Apr 2015, 13:47.
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Thank you so much! It works perfectly now.

      I was looking in all the wrong places...

      Comment

      Working...
      X