Announcement

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

  • How do you set up 'syntax' code to be able to reference specific input variables later in a program?

    Long time Stata user, relatively new to writing Stata programs.

    I'm trying to write a new program (something trivial for the purposes of practice) and I am having difficulty getting off the ground.

    When setting up the 'syntax' line of the program, I want the program to accept two numeric variables only, that will be used for other tasks through the program...

    For example:

    program define example
    syntax varlist(min=2 max=2 numeric) [if] [in] , [...]

    This will meet my first requirement of exactly two variables needed for program, however, I want to be able to refer to the variables specifically moving forward with the program, treating one variable as 'y-variable' and one as the 'x-variable'. I imagine there is a simple way to do this but I can't figure it out.

    In other words, when a varlist is provided with a command, how am I able to reference the first or second variable specifically when doing other tasks?

    Any help is appreciated. Thanks.
    Last edited by Matt Warkentin; 27 May 2016, 14:56.

  • #2
    Code:
    local var1: word 1 of `varlist'
    local var2: word 2 of `varlist'
    
    // THEREAFTER REFERENCE THESE AS `var1' AND `var2'

    Comment


    • #3
      Clyde is right but a nice alternative is

      Code:
      tokenize "`varlist'" 
      args y x somethingelse president2017 whatever
      Notice how we can use two lines even though (in this example) we are expecting five variables.

      Comment


      • #4
        Thanks to both of you for the responses! Both worked...now on to troubleshooting other programming issues.

        Comment


        • #5
          My favorite:

          Code:
          gettoken y x: varlist
          y gets the yvar, x gets all the x vars.

          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment

          Working...
          X