Announcement

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

  • Error r(100) - Varlist Required

    Hello,
    I am fairly new to Stata and am trying to write a code to understand the sequence of flights.My code is
    set obs 509519
    local count = 1
    local slno = 1
    gen sequence = _n
    forval slno = 1(1)509517{
    replace `sequence' = `count' in `slno'
    while ((flightnum[`slno'] == flightnum[`slno' + 1]) && (tailnum[`slno'] == tailnum[`slno'+1])){
    replace `sequence' = `count' in `slno'
    replace `count' = `count' + 1
    replace `slno' =`slno' + 1
    }
    }
    and gives the error r(100) varlist required. Could anybody help me with this ? Thankyou in advance

  • #2
    You are treating `slno' and `count' as if they were variable names by using them in the -replace- commands. But they are not variable names: they are local macro names. You can't use -replace- to increment them that way. You need to use

    Code:
    local ++count
    As for slno, it is automatically incremented once in each loop under the control of your -forval- loop. So separately incrementing it, while legal, is almost surely a logic error. Hence, I omit any such statement. It may be that the logic of your computation actually requires that, but if so, there is probably a much better way to express it in Stata. The combination of incrementing the same local macro explicitly and under control of a loop is, at best, confusing, and, at worst, a recipe for a meaningless computation.

    As for `sequence', that is simply undefined: you never created a local macro sequence, so the reference to `sequence' is just an empty string. You did -generate- a variable named sequence, but that does not license the use of notation `sequence'.

    There are some other problems in the syntax that you will stumble upon when you fix those problems.

    1. The variables flightnum and tailnum do not exist (or at least if they do, they were created in some part of the code that you don't show.

    2. Stata does not have a && operator: it is just a single &. (Stata is not C; they're similar in many ways, but this is a key difference.) The use of && will, in this case, produce an error message that is far from self explanatory and it will probably take you a long time to figure out that this is what's causing it. So fix that first!

    I don't know quite what you are trying to do with this code. But, unless there is other stuff going on that you haven't shown here, I note that count and slno are both initialized at 1 and are incremented by 1 each time through the loop. Therefore they are always equal. So you don't need both.

    Even with that simplification, I notice that the thrust of your loop is to do some calculation by looping over observations. That is rarely appropriate in Stata. Most such calculations can be accomplished with the appropriate use of the -by- prefix, perhaps supplemented by some -egen- functions. As I don't know what this calculation is supposed to accomplish, I can't suggest anything more specific than that. But whenever you find yourself looping over observations in Stata you should think long and hard whether there isn't a better way to do it. There usually is.

    So, my cleaned up version of your code (that does not deal with the issue of looping over observations) is this:

    Code:
    set obs 509519
    local slno = 1
    gen sequence = _n
    // gen flightnum = runiform()
    // gen tailnum = runiform()
    forval slno = 1(1)509517{
        replace sequence = `count' in `slno'
        while ((flightnum[`slno'] == flightnum[`slno' + 1]) & (tailnum[`slno'] == tailnum[`slno'+1])){
            replace sequence = `slno' in `slno'
        }
    }
    (The commented out lines generating flightnum and tailnum werre just so that those variables would exist before they were referenced in the -while- statement. I assume that they already exist in your data set prior to the code here and you won't need to do that.)

    In the future, when posting code, please place it inside code delimiters, as I have done here. It makes the code much easier to read. If you are not familiar with code delimiters, please read FAQ #12 for instructions.

    Comment


    • #3
      Hi Clyde,

      I have a similar problem, however STATA writes nameslist required.

      My code is: bys ymdate : asgen = EX_1(cond(VOL_w_5 == 1, ex_ret, .)), w(MV_USD_w)

      but then I always get the error message:
      namelist required
      r(100);

      Do you know how I could deal with this? Thank you.

      Comment


      • #4
        Kate:
        as per the help file of the community-contributed command -asgen- (please note that for good reasons explained in a dedicated FAQ, you should always inform the list that your analysis was based on a unofficial Stata contribution), you should put the name of a variable between -asgen- and the -=- sign (rnethelp "http://fmwww.bc.edu/RePEc/bocode/a/asgen.sthlp"):
        Code:
        asgen <thenameyoulike>= EX_1(cond(VOL_w_5 == 1, ex_ret, .)), w(MV_USD_w)
        Kind regards,
        Carlo
        (Stata 18.0 SE)

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          Kate:
          as per the help file of the community-contributed command -asgen- (please note that for good reasons explained in a dedicated FAQ, you should always inform the list that your analysis was based on a unofficial Stata contribution), you should put the name of a variable between -asgen- and the -=- sign (rnethelp "http://fmwww.bc.edu/RePEc/bocode/a/asgen.sthlp"):
          Code:
          asgen <thenameyoulike>= EX_1(cond(VOL_w_5 == 1, ex_ret, .)), w(MV_USD_w)
          Thank you Carlo! Of course, makes sense.

          Comment

          Working...
          X