Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Unreasonable error "Obs. nos. out of range"


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Unreasonable error "Obs. nos. out of range"
Date   Mon, 17 Jun 2013 18:31:58 +0100

As said, [0] and [_N+1] were examples and [-1] [_N+2] and ... are
other examples.

I think this is one of those situations in which first time you are
bitten you think Stata should behave differently, but after a while
you see why Stata behaves as it does.

Nick
njcoxstata@gmail.com


On 17 June 2013 18:03, Stefan Bernhard <stefanbernhard88@gmail.com> wrote:
> 1. bak was defined during the first loop on observation 1 and thus was
> still 0 after that :/
>
> myvar[0] and myvar[_N+1] could be exempt from the error listing and so
> one would know if anything between those one refers to is not there...
>
> regards, stefan bernhard,
>
> 2013/6/17 Nick Cox <njcoxstata@gmail.com>:
>> You have not answered all my questions about your code.
>>
>> I don't know what your programming criteria are here.
>>
>> I note only as one common example that references to [_n-1] and [_n+1]
>> are commonplace and deliberate.
>> I don't want to see error messages that myvar[0] and myvar[_N+1] don't
>> exist. I know that already.
>> Nick
>> njcoxstata@gmail.com
>>
>>
>> On 17 June 2013 17:36, Stefan Bernhard <stefanbernhard88@gmail.com> wrote:
>>> Thx Nick, so the thing ist that it is actually possible to refer to a
>>> non-existant observation, which caused the confusion.
>>>
>>> I then found the error after I knew that the observations had to be
>>> diasspearing.
>>>
>>> But I think the way this is handled is somewhat awkward. It should
>>> already tell me that there is no 2nd observation when I try to refer
>>> to it with the if clause :/
>>>
>>> best regards, stefan bernhard,
>>>
>>>
>>>
>>> 2013/6/17 Nick Cox <njcoxstata@gmail.com>:
>>>> Difficult to comment given this little information, but
>>>>
>>>> 0. It is possible to have variables defined but no observations.
>>>>
>>>> 1. Possibly more to the point, the -replace- will certainly fail if _N
>>>> < 2 but it's not necessarily an error otherwise to refer to -cats[2]-
>>>> when it doesn't exist. If -cats- exists as a variable then any
>>>> references to subscripts that don't correspond to observation numbers
>>>> are interpreted as missing.
>>>>
>>>> 2. However, your code does imply that -cats[2]- is being treated as
>>>> zero. Could you confirm that -local bak- is not defined before the
>>>> code you cite?
>>>>
>>>> Either way, -list-ing the data would make your situation clearer.
>>>>
>>>> This example bears on #1.
>>>>
>>>> clear
>>>> set obs 1
>>>> gen cats = 1
>>>> if cats[2] == 0 {
>>>>       di "problem 1"
>>>> }
>>>> else di "problem 2"
>>>>
>>>> Nick
>>>> njcoxstata@gmail.com
>>>>
>>>>
>>>> On 17 June 2013 15:21, Stefan Bernhard <stefanbernhard88@gmail.com> wrote:
>>>>> dear statalisters,
>>>>>
>>>>> i have a piece of looping code over different variables and all
>>>>> observations, and an excerpt of the trace shows this:
>>>>>
>>>>> = if cats[2] == 0 {
>>>>>   local bak = 0
>>>>>   }
>>>>> - noi di as text "bak is `bak'"
>>>>> = noi di as text "bak is 0"
>>>>> bak is 0
>>>>> - replace `var' = 1 in `i'
>>>>> = replace cats = 1 in 2
>>>>> Obs. nos. out of range
>>>>>
>>>>>
>>>>> This makes no sense at all to me.
>>>>>
>>>>> In the first line, it successfully uses the value of cats of
>>>>> observations number 2 to define the local bak as 0.
>>>>>
>>>>> Few lines later, it acts as if there was no more observations number 2
>>>>> and cannot replace the number of cats with 1 in observation number 2.
>>>>>
>>>>> Why does it say Obs. nos. out of range ?
>>>>>
>>>>> regards, stefan bernhard,
>>>>> *
>>>>> *   For searches and help try:
>>>>> *   http://www.stata.com/help.cgi?search
>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>> *
>>>> *   For searches and help try:
>>>> *   http://www.stata.com/help.cgi?search
>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> *   http://www.ats.ucla.edu/stat/stata/
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index