Announcement

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

  • A pretty simple question about syntax (I think)

    I begin with
    Code:
    prog def abcd
    syntax anything
    and what I want to accomplish is that if the command issued is
    Code:
    abcd 1 2 3
    I would then subsequently treat `anything' as a numlist whereas if the command issued is
    Code:
    abcd v1 v2 v3
    I would then subsequently treat `anything' as a varlist. The contents of `anything' will thus either be numbers or strings=varnames.

    I have a clunky solution for determining whether to branch to the subsequent numlist-related or varlist-related commands but am wondering how the elegant programmers among you would handle this. I.e. what test would you apply to `anything' to determine which direction to branch.

    Thanks in advance for your advice.






  • #2
    Off the top of my head, you might try passing the anything macro first through -unab- to collect a valid varlist.If that fails, pass it to -nlist- to see if it's a valid numlist. If that fails, issue an error.

    Comment


    • #3
      Thanks Leonardo. That seems like a fine solution.

      Comment


      • #4
        Code:
        program abcd
            capture syntax varlist 
            if _rc == 0 {
                di "in business: varlist `varlist'"
            }
            else {
                capture numlist "`0'"
                if _rc == 0 di "in business: numlist `r(numlist)'"
                else exit 198 
            }
        end 
        
        
        
        . sysuse auto, clear
        (1978 automobile data)
        
        . abcd price mpg weight
        in business: varlist price mpg weight
        
        . abcd 0/9
        in business: numlist 0 1 2 3 4 5 6 7 8 9
        
        . abcd frog toad newt
        r(198);

        Comment


        • #5
          Also, consider whether a separate immediate command might be the more Stata-ish way to do what you want:

          Code:
          abcd v1 v2 v3
          abcdi 1 2 3

          Comment


          • #6
            Thanks Leonardo, Nick, and Daniel.

            Comment

            Working...
            X