Announcement

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

  • Strings in an '=exp' after an anything

    I'm currently writing a program that functions as a wrapper for gen. It gets a type mismatch error (r109) when a string is used in the =exp that's passed to gen. This problem can be reproduced at least in Versions 12 - 14.2 by using the following program:

    Code:
    program define dia
    syntax anything =exp
    di "`anything'"
    end
    
    dia a = "1"
    The trace of the error indicates that it happens in the syntax line, when the program receives the string in the =exp:

    Code:
    dia a = "1"
      --------------------------------------------------------------------------------------------- begin dia ---
      - syntax anything =exp
    type mismatch
     ----------------------------------------------------------------------------------------------- end dia ---
    Is there any way for Stata to allow a string in the =exp? Is there a way around this problem that still allows anything in the syntax line?
    Last edited by Michael Rosenbaum; 06 Dec 2017, 01:49.

  • #2
    Interesting question. I don't have a story for you about syntax but

    Code:
    program define dia
       generate `0'
    end
    
    dia a = "1"
    does work. Presumably it's the bells and whistles you now intend to add that are the reason for doing this.

    Otherwise I tend to use gettoken in these cases, not syntax.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Interesting question. I don't have a story for you about syntax but

      Code:
      program define dia
      generate `0'
      end
      
      dia a = "1"
      does work. Presumably it's the bells and whistles you now intend to add that are the reason for doing this.

      Otherwise I tend to use gettoken in these cases, not syntax.
      That's bizarre. Unfortunately, the gettoken approach isn't an option for what I'm doing. The syntax line of in my current version of the program is:
      Code:
        syntax anything =exp [if] [in], [before(varname)] [after(varname)] characteristic1(string) characteristic2(string)
      That then gets passed to generate later on using this string:

      Code:
      gen `typegen' `anything' `exp' `if' `in', `before' `after'
      I can't think of a way around using syntax and passing all of the locals. The gettoken approach wouldn't allow that as far as I know. Is there a way to avoid the =exp and parse the =exp using regular expressions?
      Last edited by Michael Rosenbaum; 06 Dec 2017, 08:51. Reason: Grammar

      Comment


      • #4
        Code:
        syntax anything(equalok) [if] [in] [ , BEFORE(varname) AFTER(varname) ... ]
        ...
        generate `anything' `if' `in' , `before' `after'
        Obviously, you need to extract the variable name from anything if you plan to add characteristics. This can easily be done with gettoken.

        Best
        Daniel

        Comment


        • #5
          Originally posted by daniel klein View Post
          Code:
          syntax anything(equalok) [if] [in] [ , BEFORE(varname) AFTER(varname) ... ]
          ...
          generate `anything' `if' `in' , `before' `after'
          Obviously, you need to extract the variable name from anything if you plan to add characteristics. This can easily be done with gettoken.

          Best
          Daniel
          Great! I didn't know about the equalok option there. I'm doing some stuff with type options as well, so I'm already tokenizing it. Should be super simple to adjust my code.

          Thanks for all the help!

          Comment

          Working...
          X