Announcement

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

  • limits of _n

    Hi all,

    I have a dataset with 22,534,283 records. I would like to create a simple unique id so I used the following: g id=_n.

    When I examine the contents of this variable, I find that there are duplicates. See screenshot attached from results window and a paste is below.

    the other odd issue is that id's format is float rather than long. Not a big deal (maybe) but it struck me as odd.

    Any ideas how to resolve this issue?

    Thanks, in advance,

    Ben Hoen
    Berkeley Lab

    =================================================

    . drop id

    . g id=_n

    . duplicates report id

    Duplicates in terms of id

    --------------------------------------
    copies | observations surplus
    ----------+---------------------------
    1 | 18216483 0
    2 | 2 1
    3 | 4317798 2878532
    --------------------------------------
    Attached Files

  • #2
    Perhaps:

    Code:
    g double id = _n
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      That works! as does
      Code:
      g long id = _n
      .

      Thanks!

      Comment


      • #4
        Eric makes the main point. The default default (repetition intended) variable or storage type for numeric values is float. You can set up your own default, but Stata doesn't look at expressions with numeric results to optimise the variable type.

        Default storage types are explained alongside generate at help generate.

        The problem may be that beyond beginner stage people assume that they understand generate. (Happens to most of us, I guess: I know that I didn't know about a relatively recent addition to describe -- which may mean added within the last decade -- until I re-read the help.) More optimistically, the default doesn't bite very often.

        Comment


        • #5
          Thanks Nick.

          Ben

          Comment

          Working...
          X