Announcement

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

  • Too few variables specified error when using macros

    Hello everyone, I am trying to figure why does using the generate command with a local results in the error "Too few variables specified", but it works fine when used with a variable?

    Below is my code, trying to generate a variable/macro based off of arguments given to be later used in running a DID regression. The problem lies in when generating the `K' macro. It does not work with `K', but works fine with the variable K. Why is this? Thank you for your answers in advance!

    Code:
    program define testfunction, eclass sortpreserve
        version 17.0
        syntax varlist(min=5 max=5)
    
        /*initialize variables*/
        tokenize `varlist'
        local Y `1'
        local i `2'
        local t `3'
        local ei `4'
        local group `5'
    
        gen K = (`t' - `ei')
        
    end

  • #2
    generate creates a variable and variables need names. If you type gen `K' = ... then Stata sees a ` and a ', and before it starts executing any commands it first replaces the local macro with its contents. In your program, the local macro `K' is not defined and not defined macros contain empty strings. So what Stata sees next is gen = ... and returns an error message. What you can do is at the beginning of the program define a temporary variable, and use that: tempvar K, now the local macro `K' is defined (with something linke _000000001) you can use `K', and at the end of the program, the variable `K' will be removed.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      thank you for the very clear explanation!

      Comment

      Working...
      X