Search
   >> Home >> Resources & support >> FAQs >> Passing arguments to do-files

How can I pass arguments to my do-files?

Title   Passing arguments to do-files
Author William Gould, StataCorp
Date February 1996; updated July 2011

You pass arguments to your do-files by adding the arguments to the run or do command line. Stata will save the extra arguments in the numbered macros so that you can access them.

Example 1

Let’s say that you have the following do-file testarg1.do:

        display "The first argument is: `1'"
        display "The second argument is: `2'"
        display "The third argument is: `3'"

Here are some sample output:

 . do testarg1 12 crawfish 3.14

 . display "The first argument is: `1'"
 The first argument is: 12

 . display "The second argument is: `2'"
 The second argument is: crawfish

 . display "The thrid argument is: `3'"
 The thrid argument is: 3.14


 . do testarg1 "George Washington" 12 1.4

 . display "The first argument is: `1'"
 The first argument is: George Washington

 . display "The second argument is: `2'"
 The second argument is: 12

 . display "The thrid argument is: `3'"
 The thrid argument is: 1.4

Example 2

Now, let’s say that we want to write a do-file that will accept three arguments. The first argument is the name of a file to use, the second argument is the name of a variable in the dataset, and the third argument is a value of the specified variable indicating the subset of the data that we want to load into memory.

        use `1' if `2'==`3'

We may also want to save the arguments into other macros that have more meaningful names so that we might use them throughout our do-file. We can do this with the args command:

        args fname vname val
        use "`fname'" if `vname'==`val'
The Stata Blog: Not Elsewhere Classified Find us on Facebook Follow us on Twitter LinkedIn Google+ Watch us on YouTube