Announcement

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

  • Handling of out of range values

    Dear All,

    I've just found that the following code does not produce an error, while I anticipated it should:

    Code:
    clear
    generate x="abc"
    display x[1]
    display x[1000]
    display x[-1000]
    I expected to see here:
    Code:
    observation numbers out of range
    r(198);
    Similar to what we are getting after
    Code:
    replace x=1 in 1000
    Observed in Stata 17 (if this matters, but probably all versions of Stata).

    Is there any way to restrict this behavior to result in error for when attempts are made to read from outside of the data range?

    Thank you, Sergiy

  • #2
    It is not always an error to refer directly to an observation not in the dataset. A common example is something like


    Code:
    bysort foo : gen whatever = bar[_n-1]
    which for the first observation in each group refers to bar[0], which is not in the dataset, but Stata just evaluates this as missing.

    This is a an example of the error you mention, as there are 74 observations in the auto dataset.

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . replace mpg = 100 in 100
    observation numbers out of range
    r(198);
    I don't have a good story for the reasons why or why not, but the first is something I have done many times on purpose, where anything like the second is just a silly mistake.

    Comment

    Working...
    X