Announcement

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

  • Accomodating arguments of an option in the -syntax- command

    Dear all,
    I have a question related the -syntax- command, namely how to accomodate a varlist specified as part of an option to use it later within the program. I am using Stata 13 in Windows 10.

    In particular, I am defining a program (called descriptives) which requires a varlist. For the subset of continuous variables (specified in the option continuous() ), I apply a set of commands; for the subset of discrete variables (the remaining variables of the varlist), I apply another sets of commands. A MWE of what I have is
    Code:
    capture: program drop descriptives
    program define descriptives
    version 13
    syntax varlist(numeric min=1), CONTinuous(varlist numeric min=1)
    
    foreach var of local varlist {
        if SOMETHING {
           ....
           [A set of commands here for the continuous variables]
           ....    
        }
        else  {
           ....
           [Another set of commands here for the discrete variables]
           ....
        }    
    }
    
    end
    My question is how to turn SOMETHING into a condition that uses the content in the continous() option to specify the continuous variables in the varlist. E.g. say that I have 5 variables, x, y and z which are continuous and w1 and w2 which are discrete. Thus, I would like to specify
    Code:
    descriptives x w1 y z w2, cont(x y z)
    so that after accomodating "x y z", SOMETHING turns into
    "`var'"=="x" | "`var'"=="y" | "`var'"=="z"
    which would tell the program that these are the continous variables and do the appropiate analysis. Maybe there is another way to do this more efficiently that does not imply the conditional branching, which I would appreciate to know.

    Thank you
    Last edited by Juan del Pozo; 14 Jul 2018, 11:26.

  • #2
    You have your list of continuous variable in the macro, so to get the discrete vars you can use
    Code:
    local discrete: list varlist-continuous
    Code:
    capture: program drop temp
    program define temp
    version 13
    syntax varlist(numeric min=1), CONTinuous(varlist numeric min=1)
    
    local discrete: list varlist-continuous
    noi di "discrete vars: `discrete'"
    noi di "continuous var: `continuous'"
    
    noi di "frequency tab of discrete"
    foreach dvar of local discrete {
        tab `dvar'
        }
    noi di "summary stats of continuous"    
    foreach cvar of local continuous {
        sum `cvar'
        }
    end
    
    
    sysuse auto
    temp price weight foreign, cont(price weight)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Carole's elegant answer does the job, and nicely groups your discrete and continuous variables separately. If however you actually want to go through the variables in the order given in the varlist, as your code showed, the following will do that.
      Code:
      clear
      cls
      capture program drop descriptives
      program define descriptives
      version 13
      syntax varlist(numeric min=1), CONTinuous(varlist numeric min=1)
      
      foreach var of local varlist {
          local cont : list var in continuous
          if `cont' {
             display "`var' is continuous"
          }
          else  {
             display "`var' is not continuous"
          }    
      }
      end
      
      set obs 1
      foreach v in x y z w1 w2 {
          generate `v' = 42
          }
      
      descriptives x w1 y z w2, cont(x y z)
      Code:
      . descriptives x w1 y z w2, cont(x y z)
      x is continuous
      w1 is not continuous
      y is continuous
      z is continuous
      w2 is not continuous
      Both Carole's code and mine make use of the macro list commands, see help macro list for more details about these powerful commands.

      Comment


      • #4
        Dear Carole and William,
        I appreciate your prompt help. Indeed, Carole's answer is very elegant since it reduces the looping by using the extended macro function "list"; however, I wanted to preserve the order given in the varlist so I went for William's suggestion. My main takeaway of his approach, besides the usage of the "list in" extended macro function, is that, in general, the "if" in the conditional branching can be accesed whenever the expression that succedes it is nonzero. This is what "list in" does: it gives access to that branch only to a continous variable (stated in the program's option) if it is in the list of continuous variables, in which case the local is 1. After seeing this, I realized that "`var'"=="x" in my original code is not intepreted by Stata literally but instead is translated to 1. Indeed, this holds in general
        Code:
        if 2+2==4 {
            display "Shown"
        }
        if 0 {
           display "Not shown"
        }
        Thank you so much for this!
        JM

        Comment


        • #5
          See also e.g. https://www.stata.com/support/faqs/d...rue-and-false/ for more on true and false in Stata.

          Comment

          Working...
          X