Announcement

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

  • Syntax error with program using namelist and varlist as input

    Hello. I have a custom program that is designed to format some tables of results based on a regression function (eg logistic) and a varlist. I previously used just a namelist for the regression function and the varlist, and it worked great. However, I now need to allow it to accept factor variables. I cant use just a varlist(fv) because this wont allow me to also use the regression function. I tried to use a namelist followed by a varlist(fv), but this does not work due to a syntax error. Can you tell me why?


    For example, if I run this: (took out the actual code since it doesnt matter for this problem)

    Code:
    capture program drop testprog
    version 17
    
    program define testprog
    
    syntax namelist
    
    di "`namelist'"
    
    end
    and use

    Code:
    testprog logistic age bmi

    I get the result which repeats the input namelist as expected:

    Code:
    . testprog logistic age gender
    logistic age gender

    However, if I change the program to use a namelist for the regression function and a varlist for the variable input, this syntax error occurs.

    Code:
    capture program drop testprog
    version 17
    
    program define testprog
    
    syntax namelist(max=1) varlist
    
    di "`namelist'"
    
    end
    Code:
    . testprog logistic age gender
    invalid syntax

    Ive read on other threads that it may not be possible to use both a namelist and a varlist in a single program syntax.... Is this true? If so, how can I accomplish what I need to do while allowing factor variables?


    thanks for your help!

    hpw

  • #2
    No need to read in other threads.

    Code:
    help syntax
    tells you what is allowed and what is not.

    Also, you have omitted an important detail. The return code that you get is r(197) not r(198). The former means there is something wrong with your syntax command not with what is parsed. And, you are correct: syntax allows either namelist or varlist but not both.

    For what you (seem to) want, you need low-level parsing. Here is an approach:

    Code:
    program define testprog
        version 17
        
        gettoken cmd 0 : 0
        syntax varlist(fv)
        
        display "`cmd' `varlist'"
    end

    Comment


    • #3
      thank you for your help and for pointing out the difference in error codes. Your suggestion works great

      hpw

      Comment

      Working...
      X