Announcement

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

  • Arguments don't work in .do files?

    Hello,

    This seems annoyingly simple and I can't believe that Google has failed to help, but it is causing a lot of frustration.

    The Stata documentation and many tutorials show how to use arguments in .do files, but it does not appear to work. Even cutting and pasting code from Stata docs. My own testing suggests that arguments are only for .ado files. Is this the case, or am I getting something wrong? As an example, in Stata 13.1 a file called argue.do with this content:

    Code:
    capture program drop argue
    program argue
        args arg1
        display "`arg1'"
        mata: printf("The arguments are: %s ",`arg1')
    end
    argue
    prints a blank line then prints "The arguments are: " before erroring due to insufficient arguments (printf - 3001). Changing to the following:

    Code:
    capture program drop argue
    program argue
        display "`1'"
        mata: printf("The arguments are: %s ",`1')
    end
    argue
    gives the same result. In contrast, the following argue.ado file works perfectly:

    Code:
    program argue
        args arg1
        display "`arg1'"
        mata: printf("The arguments are: %s ",`arg1')
    end
    Am I missing something? Are arguments only for .ado files, despite the Stata documentation?

    Thanks for any help
    Lee.

  • #2
    You'll probably want to give your argue program an argument, because you seem to have made it mandatory in the code. Anyway, you can make Stata programs accept arguments, even when they're defined inside do-files. See the following. (The do-file is attached below, if you want to try it out.)

    .do"F:\argue.do"

    .version13.1

    .
    .clear*

    .setmoreoff

    .
    .programdefineargue
    1.version13.1
    2.syntaxanything(name=arg1)
    3.displayinsmclasresult"`arg1'"
    4.mata:printf("Theargumentsare:%s",st_local("arg1"))
    5.end

    .
    .argueMyargument!
    Myargument!
    Theargumentsare:Myargument!
    .
    .exit

    endofdo-file


    .
    Attached Files

    Comment


    • #3
      Tangle (aka Lee) (or Lee aka Tangle) (*) did not actually give any examples of his or her failure to get do files to accept arguments.

      Whether there is more (or less) interest in getting programs to accept arguments, Joseph showed nicely that that can work easily. But if you use a do-file to create a program, that is a different thing.

      So, we really can't say what is going wrong over the do files. Combining a willingness to believe that all tutorials might be wrong on this point with a lack of examples to the contrary is an interesting combination! Be assured: It works.

      As a silly example, I created this file and called it mydo.do

      Code:
      di "first argument is  " "`1'"
      di "second argument is " "`2'"
      di "third argument is  " "`3'"
      and played with it:

      Code:
      . do mydo OK
      
      . di "first argument is  " "`1'"
      first argument is  OK
      
      . di "second argument is " "`2'"
      second argument is
      
      . di "third argument is  " "`3'"
      third argument is  
      
      .
      end of do-file
      
      . do  mydo no problem
      
      . di "first argument is  " "`1'"
      first argument is  no
      
      . di "second argument is " "`2'"
      second argument is problem
      
      . di "third argument is  " "`3'"
      third argument is  
      
      .
      end of do-file
      
      . do mydo works for me
      
      . di "first argument is  " "`1'"
      first argument is  works
      
      . di "second argument is " "`2'"
      second argument is for
      
      . di "third argument is  " "`3'"
      third argument is  me
      (*) Using full real names, as is requested here, really does help easy and effective communication. For example, Lee/Tangle now knows that Joseph Coveney knows his stuff and gives detailed examples and would recognise his other posts easily.
      Last edited by Nick Cox; 03 Jun 2014, 04:37.

      Comment


      • #4
        Thank you Joseph! I see now why nothing was coming through, it was annoyingly simple.

        --

        Nick, I'm sorry but I don't understand how I failed to provide an example. Joseph was able to point out my mistake.

        I'm unconvinced by your name argument, but I will delete this account and create a new one before I start anymore threads.

        Comment


        • #5
          You used a do file to define a program, which takes arguments; except that your testing, as reported, did not include instances of the programs being supplied with arguments, which is what Joseph pointed out. In fact your use of a .do file was irrelevant to that issue, as you would have got the same results had you typed the code interactively.

          That is not the same as do files themselves taking arguments, which is what my example showed.

          Joseph tackled part of your difficulties in understanding and I tackled a second part.

          Here is another part: It is not correct that your last program "works perfectly". Here is the program and a test:

          Code:
          . program argue
            1.     args arg1
            2.     display "`arg1'"
            3.     mata: printf("The arguments are: %s ",`arg1')
            4. end
          
          . argue frog
          frog
                           <istmt>:  3499  frog not found
          The display command works, but then Mata is asked to print a scalar with the name supplied, but it knows of no such scalar and an error is issued. The difficulty is the difference between the name of something and a literal string, for which " " are needed.

          In my view, the best place to start learning this stuff is [U]. The first thing you can do is just repeat the examples there; the second thing is to vary them to test your understanding. If the first seems too tame, but the second gets you into difficulties, then retreat to the first.
          Last edited by Nick Cox; 04 Jun 2014, 19:39.

          Comment

          Working...
          X