Announcement

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

  • levelsof returns error "invalid name"

    Hi All,

    I am puzzled why levelsof is returning an "invalid name" error for the following:

    Code:
    local trper 1980 1985 1989
    levelsof `trper'
    or

    Code:
    local trper "1980 1985 1989"
    levelsof `trper'
    or


    Code:
    local trper "1980 1985 1989"
    levelsof  "`trper"'

    any help is appreciated!

    Thanks,

    Ariel

  • #2
    levelsof expects a variable name as the argument following. What you're supplying has unbalanced punctuation, which may be a typo here and not what you're really typing, but Stata is correct that it's not a variable name.

    More importantly, what you would expect levelsof to do here? The "levels" of 1980 1985 1989 are just those values.

    Comment


    • #3
      Hi Nick!

      In this case, the local trper represents an option in a command I am writing (actually modifying a command that you helped me with many moons ago). The user specifies one or more dates (which the option reads as dates).

      So in this specific example, local trper has 3 values 1980 1985 and 1989 (whereas in the command the user would set the option to read "trper(1980 1985 1990)".)

      My idea here is to first get a count of values in "trper" and then assess whether there are semicolons between them (because the user is required to separate the dates with a semincolon). So ultimately, in this case, the code should first see that there are 3 dates, and then determine if there are 2 semicolons). If not, there is an error...

      My block of code looks like this so far:

      Code:
              levelsof "`trper'"
              local trcnt = r(r)
              if `trcnt' > 1 {
                  if `semicnt' != (`trcnt' - 1) {
                      di as err "a semicolon ; must separate each date in trperiod()"
                      exit 498
                  }
              }
      I hope my description is clear?

      Thanks!

      Ariel

      Comment


      • #4
        Oops, I forgot to write how I assess `semicnt', which is also a Nick Cox special :-)

        Code:
        /* count of semicolons in trperiod */
        local semicnt = length("`trperiod'") - length(subinstr("`trperiod'", ";", "", .))

        Comment


        • #5
          OK, but as said levelsof can't help you here because it does not extend beyond the levels of a variable.

          You need to tokenize using semi-colons.

          Comment


          • #6
            Hi Nick,

            You're right. I was thinking of using levelsof only as a means of getting to the count of words, but I can get there by:

            Code:
            local trperiod 1980 1985 1989
            
            local trcnt: word count `trperiod'
            
            local semicnt = length("`trperiod'") - length(subinstr("`trperiod'", ";", "", .))
            
            if `trcnt' > 1 {
                if `semicnt' != (`trcnt' - 1) {
                   di as err "a semicolon ; must separate each date in trperiod()"
                   exit 498
                 }
            }


            Thank you, Nick!

            Ariel

            Comment

            Working...
            X