Announcement

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

  • tsappend does not work when the number of periods is given by a scalar

    Hi, so my first question on this forum is this (tsset Date done, where Date stores quarterly periods):

    Why
    Code:
    tsappend, add(12)
    works as expected, but
    Code:
    scalar H=12
    tsappend, add(H)
    does not?
    (taking `H', changing scalar to local, using int(H) instead of H - doesn't work, I get "add() must specify a positive integer " or "you must specify either add() or last()" error messages).

    I circumvented the problem by:
    Code:
    scalar H=12
    tsappend, last(Date[_N]+H) tsfmt(int)
    but I'm curious why add(H) does not work.

    Thanks!


  • #2
    The -add()- option requires a number. In Stata, a scalar is a different object from a number. (In fact, a scalar can contain non-numerical string content in Stata.) And in general, scalars and numbers cannot be substituted for each other in syntax. Scalars can often be substituted for variables in Stata syntax, but not for numbers.

    Here are some possibilities that will work:

    Code:
    scalar H = 12
    tsappend, add(`=H') // EVALUATE THE SCALAR
    
    local H 12
    tsappend, add(`H') // EVALUTE THE LOCAL

    Comment


    • #3
      The command will interpret the input literally. Since only positive integers are allowed, it will exit with an error. You want Stata to force evaluate the expression before parsing, and you can do this as:

      Code:
      webuse tsappend1, clear
      scalar H=12
      tsappend, add(`=H')
      Crossed with #2.

      Comment


      • #4
        Dear Clyde and Andrew,
        thank you for your answers. Both
        Code:
        scalar H=12
        tsappend, add(`=H')
        local S=12
        tsappend, add(`S')
        work indeed. But I would never guess that you need equality sign when using a scalar (!). Frankly, even the need to use `' is something I am slowly getting used to, and I believe is somewhat contrary to usual programming practices as e.g. in Matlab.

        Clyde, if I may ask, what do you mean by "scalar is a different object from a number", the manual says "Stata scalars are named entities that store single numbers or strings" and in examples given ther it behaves pretty much like numbers (the manual adds that "Stata scalars are allowed only in expressions", which I guess precludes their simple use in tsappend, add(H) or forv p=1/H{...}, but for what reason is not clear to me.)

        The last thing I noticed. The solution with local, although does not necessitate the equality sign, has a drawback: if there is some line of code between the definition of a local and its use in tsappend, you get an error, compare:
        Code:
        scalar H=12
        display 2*H
        tsappend, add(`=H')
        local S=12
        display 2*S
        tsappend, add(`S')
        Isn't it a bit confusing? I must admit to me it is very much so.

        Comment


        • #5
          Originally posted by Michal Markun View Post
          if there is some line of code between the definition of a local and its use in tsappend, you get an error, compare:
          Code:
          scalar H=12
          display 2*H
          tsappend, add(`=H')
          local S=12
          display 2*S
          tsappend, add(`S')
          Isn't it a bit confusing? I must admit to me it is very much so.
          Are you running the code line-by-line in a do-file? Local macros have local scope, as the title of the following article states: https://journals.sagepub.com/doi/10....36867X20931028. You need to make sure that when running the code line-by-line, you also select the line where the macro is defined.

          Comment


          • #6
            "Stata scalars are named entities that store single numbers or strings"
            Yes, and you must parse that very carefully.

            Numbers are, literally, numbers: a series of digits, optionally containing a single decimal point and optionally terminated with an exponent (letter e, optional -, some digits). Numbers do not have names, and they certainly don't store strings.

            Scalars are objects that contain information, and the information contained may be a number, or it may be a string. Scalars must have names.

            If you are foundations of mathematics oriented, you might think of a scalar as a set containing exactly one element, and the element of that set might be a number. The `=...' operation, applied to a scalar, extracts its member.

            (More generally, though, the `=...' operation in Stata is applied to expressions and returns the value of the expression when calculated with the current values of all things mentioned in the expression.)

            I think most Stata users find local macros and scalars confusing at first, but the learning curve is not all that steep and using them becomes second nature fairly quickly with practice.

            Comment


            • #7
              Clyde, thank you for the explanations.

              Originally posted by Andrew Musau View Post

              Are you running the code line-by-line in a do-file? Local macros have local scope, as the title of the following article states: https://journals.sagepub.com/doi/10....36867X20931028. You need to make sure that when running the code line-by-line, you also select the line where the macro is defined.
              Andrew, I just select these 6 lines of code (you quote from my #4 post, all of them at once) in a do-file editor and then press Ctrl+D. Dataset is appended with 12 observations (tsappend, add(`=H') works), but then "S not found" error is issued.

              Comment


              • #8
                Ah, you need macro quotes to reference the local. This is unlike scalars. See

                Code:
                help quotes
                Code:
                scalar H=12
                display 2*H
                tsappend, add(`=H')
                local S=12
                display 2*`S'
                tsappend, add(`S')

                Comment

                Working...
                X