Announcement

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

  • Passing a boolean property to a command?

    Hello Statalisters,
    I am trying to write a program with an option that the user can specify as true or false. If the property is given as "option(true)", I want the command to do one thing, if it's given as "option(false)", I want it to do something else.
    How could I write a Stata command that accepts a boolean property?
    Here is the code for my ado file:
    Code:
    syntax,  red(boolean)
        if red == true{
            di in red "Hello world"
        }
        if else {
            di "Hello world"
        }
    
    end
    Here is the code I type in the console to run this program:
    Code:
    set trace on
    helloworld, red(true)
    This code returns the error "invalid syntax" for the line "syntax, red(boolean)". I have successfully passed strings, varnames and varlists with syntax to custom commands I've written in the past with similar code--can I pass a boolean like this? If so, how?
    Thank you and please let me know if I can clarify this question in any way.
    Last edited by Daniel Ram; 23 Feb 2021, 13:18.

  • #2
    Notwithstanding some similarities, Stata is neither Python nor C++. There is no boolean data type in Stata. Nor is there a reserved word for true or false. Boolean expressions are evaluated as numeric 0 or 1 (though any non-zero number or even a missing value will also be interpreted as true.). Once you get past that, you have some other errors in your syntax that will trip you. I have dealt also with those in the code here:

    Code:
    syntax, red(integer)
        if `red' {
            display in red "Hello world"
        }
        else {
            display as text "Hello world"
        }
    end

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Notwithstanding some similarities, Stata is neither Python nor C++. There is no boolean data type in Stata. Nor is there a reserved word for true or false. Boolean expressions are evaluated as numeric 0 or 1 (though any non-zero number or even a missing value will also be interpreted as true.). Once you get past that, you have some other errors in your syntax that will trip you. I have dealt also with those in the code here:
      Thank you for you helpful response, Clyde.

      Comment


      • #4
        -syntax- also allows for optionally on or off keywords. Here I show an equivalent approach using the optionally on approach.

        Code:
        cap program drop myprog
        program myprog
          syntax , [myflag]
            
          if "`myflag'"!="" {
            di "-myflag- is passed - TRUE"
          }
          else {
              di "-myflag- is not passed - FALSE"
          }
        end
        
        myprog
        myprog, myflag

        Comment


        • #5
          Originally posted by Leonardo Guizzetti View Post
          -syntax- also allows for optionally on or off keywords. Here I show an equivalent approach using the optionally on approach.


          [/code]
          Thank you for the helpful reply Leonardo. I appreciate it!

          Comment

          Working...
          X