Announcement

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

  • Examples when using syntax command

    Hello,

    The manuals provide examples using syntax varlist and syntax [varlist].

    Examples using syntax [newvarlist] and syntax [newvarname] would be appreciated!

    Thanks!

  • #2
    That would look exactly the same, except that now syntax checks whether the variables exists in the data and returns an error when the variables exists instead of returning an error when the variables does not exist.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      There would also be an error if the name suggested was not legal.

      Comment


      • #4
        Hello Maarten,

        I don't think I am understanding it very well. It wouldn't look exactly the same because you would need to include generate in the brackets, no?

        syntax [newvarlist (generate...]

        I am having a hard time figuring out how the syntax looks like and when it would be appropriate to implement.

        If someone provides an easy to understand example, something applicable I would appreciate it.

        Apologies if I can't grasp something simple as this.

        Comment


        • #5
          There is no need to specify generate; what makes you think so? My advice: play around a bit yourself or provide a bit more background on your ultimate goal so we can come up with a relevant example; here is a silly one: Say you want to write a program that generates a number (at least two) of identical variables. Here is how you could accomplish that

          Code:
          program generate2
              version 11.2
              
              syntax newvarlist(min = 2 generate) = exp [ if ] [ in ]
              
              foreach var of local varlist {
                  quietly replace `var' `exp' `if' `in'
              }
          end

          Best
          Daniel

          Comment


          • #6
            I'm in the same boat as the OP. It's my first time using -syntax-, and the lack of examples for newvar options has me a bit confused too.

            For example, I cannot figure out how to make -syntax newvarname- create a string variable. From reading the help and documentation, it seems like this should work:

            Code:
            cap program drop test2
            program test2
                version 15.1
                syntax newvarname(generate str40)
            end
            test2 my_new_var
            That does not work. Stata will create a new variable of type float if I remove "str40", so I know the rest of it's ok. Is it not possible to specify type with the generate varlist specifier? Seems like it should be possible somehow, but I'll be darned if I can figure out how. Particularly for novice programmers, a little more color on the generate specifier would be useful.

            I can solve my particular problem in this instance with -args- and -confirm variable- to check to make sure the variable doesn't already exist. I just wanted to add a second vote for a little more detail in the docs for -syntax-.

            Comment


            • #7
              I agree with Troy that the generate specifier is not well explained; in fact one might get the (wrong) impression that it can be combined with string, which actually works but probably not in the intended way.

              Having said this, I think the approach of hard-coding the type (e.g. str40) of the new variable is not a good one. In Stata, the variable type is usually either explicitly or implicitly supplied by the user. An example of the former is

              Code:
              generate str40 myvar = "foo"
              and an example for the latter is

              Code:
              generate myvar = "foo"
              where Stata will understand that "foo" is a string expression and create a string variable, accordingly.

              If Troy wants a str40 to be created regardless of the user's specification, then

              Code:
              program genstr40
                  version 11.2
                  
                  syntax newvarlist(max = 1)
                  
                  // completely ignore user's -typlist-; bad idea
                  quietly generate str40 `varlist' = "foo"
              end
              will do this. When the user then specifies

              Code:
              genstr40 float myvar
              where a float variable is requested explicitly, the program would still create a str40. I find it hard to imagine a situation where this would be useful.

              Best
              Daniel
              Last edited by daniel klein; 11 May 2018, 00:45.

              Comment


              • #8
                Interesting questions. An extra facet for me at least is why this doesn't seem a common question, or rather why I can't remember it being asked before. (That's just a reflection, not a criticism. An interesting new question is always welcome.)

                Mymain speculation is that programmers typically specify a new variable name through an option.

                Alternatively if you wanted a variant on generate it might have a syntax that you wouldn't parse using syntax newname. (See why these typographical distinctions can be helpful.)

                Suppose you wanted to support something like

                mygen [typename] newname = expression ["variable label"]

                where the main twist would be that the user can specify a variable label at the same time as a typename (optional), a new name and an expression. Typically a programmer might be best advised to use gettoken to peel off syntax elements one by one, or to use anything with syntax and then look at what it contains.

                This is just a hypothetical example, not an expression of intent.

                Comment

                Working...
                X