Announcement

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

  • Setting positional argument to float type in program

    I'm writing a program, in which one of the arguments is always a decimal between 0 and 1. Here's some reproducible code showing my issue. Please direct me to how I can correct this code to allow for float (or double) arguments.

    cap program drop test
    program define test
    syntax, f(numeric)
    di `f'
    end

    test, f(.05)


    This code throws the invalid syntax error code.

    I've also tried switching numeric to float or double. If I change the type to str, it works but produces a string. Thanks in advance for the help.

  • #2
    Code:
    syntax, f(real)
    Added: The classification of options it not be storage type, as often what is specified is not a variable in any case. It is instead a classification by how the option itself will be used. Options that will be used as numbers are either integer (if non-integer values are not allowed) or real.

    Also added: It is not a good idea to name your program -test-, as that is also the name of an existing Stata command. I think the result of doing that is that you would be unable to use the Stata -test- command in the same do-file. At worst it might be undefined behavior. In any case, it will be confusing to others reading your code, and possibly to you as well if you come back to this after an absence of a few months.
    Last edited by Clyde Schechter; 03 Apr 2023, 15:32.

    Comment


    • #3
      Code:
      cap program drop myprog
      program define myprog
      args f
      syntax [, f(numlist max=1 >=0 <=1)]
      di "`f'"
      end
      
      myprog, f(.05)
      myprog, f(0)
      myprog, f(1)
      myprog, f(2)
      Res.:

      Code:
      . myprog, f(.05)
      .05
      
      . 
      . myprog, f(0)
      0
      
      . 
      . myprog, f(1)
      1
      
      . 
      . myprog, f(2)
      f() invalid -- invalid number, outside of allowed range
      r(125);

      Comment


      • #4
        Clyde Schechter Thanks! This was just a reproducible example, not my actual code or program name. Thanks for the help, both of you

        Comment

        Working...
        X