Announcement

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

  • how to past a Number to an ado Command in Stata

    I have a number, for instance, the number 6, stored in a local variable, such as:
    Code:
    local var1 42
    Now, if I have a custom command named cmdx, how should the syntax within this command be writed to accept this var1?

  • #2
    Well, that depends on the syntax of -cmdx- itself. But the principle is this: wherever you would put 42 in the -cmdx- command, put `var1' instead.

    Comment


    • #3
      In my experience, defined locals do not pass through to programs. You have to args it.

      Code:
      clear all
      
      ** THIS DOES NOT WORK
      program testprog
      di `var1'
      end
      
      local var1 = 42
      testprog
      
      ** THIS WORKS
      program testprog1
      args var11
      di `var11'
      end
      
      local var1 = 42
      testprog1 `var1'

      Comment


      • #4
        Gotta go to it and add it yourself! I use adoedit (user written, opens ado files into do files), and you'll need to pass said number as an argument

        Comment


        • #5
          Locals do not pass through into the code of programs, that is correct. But I don't think that's what O.P. is asking about. As I understand the question, there is some user-written program cmdx.ado that has some syntax. It might be:
          Code:
          cmdx list_of_number_arguments
          
          OR
          
          cmdx varlist, some_option(real n)
          and wants to know how to use the contents of local macro var1 as one of the arguments, or the value of some_option. That is what my code responds to.

          Of course, without knowing more about what -cmdx- is (specifically, its syntax) nobody can be sure. And O.P. provides no clues about that.

          Comment


          • #6
            By "custom command" I assumed he was writing the code.

            Another option is to use a scalar, which would pass through.

            Comment


            • #7
              George Ford's code works.
              I use the following code, which works well.
              Code:
              program testprog2
              syntax anything
              di `anything'
              end
              local var1 = 42
              testprog2 `var1'
              However, I am confused about how to write the syntax when I need to receive external data such as numbers, scalar, strings, variables, matrices local/glabal, etc. What is the best way to write this syntax? Are there any good reference materials available?

              Comment


              • #8
                See the help file for the syntax command. Typing -help syntax- into the console should pull up the help file with a link to the current documentation. There are several useful resources for programming with Stata, including this one that you may want to read.

                Comment


                • #9
                  When I'm only passing a few things through to a program, I'll use args.

                  For more complex calls, I'll use syntax. I'd start with a basic setup program and learn from there. You'll need to learn to tokenize and so forth.

                  I recall Stata having a class on writing ado files that was useful.

                  Comment

                  Working...
                  X