»  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 and Mia Lv, StataCorp

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.

`0'what the user typed, exactly as the user typed it.
`1'the first argument (first word of `0')
`2'the second argument (second word of `0')
`3'the third argument (third word of `0')
......
`*'the arguments `1', `2', `3', ... , listed one after the other and with one blank in between; similar to but different from `0' because odd spacing and double quotes are removed

* Please read manual [U] 18.4 for more information.

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'"
	display `"All the arguments, as typed by the user, are: `0'"'

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 third argument is: `3'"
 The thrid argument is: 3.14

 . display `"All the arguments, as typed by the user, are: `0'"'
  All the arguments, as typed by the user, are: 12 crawfish 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 third argument is: `3'"
 The thrid argument is: 1.4

 . display `"All the arguments, as typed by the user, are: `0'"'
 All the arguments, as typed by the user, are: "George Washington" 12 1.4

In the above code, we use compound double quotes for

	display `"All the arguments, as typed by the user, are: `0'"'

Compound quotes are used to distinguish the double quotes that delimit the string we want to display from any other double quotes that `0' might contain. For more information, please see this explanation of compound double quotes.

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.

 ***** test.do *****

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

 **** end of test.do ****

Then, we can type the following commands:

 sysuse auto, clear
 save auto
 do test auto foreign 1

The arguments auto, foreign, and 1 will be stored in the local macros `1', `2', and `3', respectively. As a result, all the observations in auto.dta satisfying foreign==1 are loaded.

In Stata, the default macros saving the arguments are named as `1', `2', `3' .... However, 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:

The above file test.do can be revised as

 ***** test.do *****

 args fname vname val
 use "`fname'" if `vname'==`val'

 **** end of test.do ****

Then, we run test.do. Using the same example, we can type

 sysuse auto, clear
 save auto
 do test auto foreign 1

The argument auto will be stored in the local macro `fname'; foreign will be saved in the local macro `vname'; and 1 will be stored in the local macro `val'. The macro names fname, vname, and val are defined by us in test.do. You can also customize these macro names.

Stata

Shop

Support

Company


The Stata Blog: Not Elsewhere Classified Find us on Facebook Follow us on Twitter LinkedIn Google+ YouTube Instagram
© Copyright 1996–2018 StataCorp LLC   •   Terms of use   •   Privacy   •   Contact us