Stata
Products Purchase Support Company
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

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.

        local fname `"`1'"'
        local vname `"`2'"'
        local val `3'
        use `fname' if `vname'==`val'
FAQs
What's new?
Statistics
Data management
Graphics
Programming Stata
Mata
Resources
Internet capabilities
Stata for Windows
Stata for Unix
Stata for Macintosh
Technical support
Resources & support
FAQs
Technical support
NetCourses
Short courses
Users Group meetings
Statalist
Links
Software updates
Software archives
Customer service
Manuals & supplements
Stata Journal
STB
Stata News
Stata Automation
Plugins

Site overview
Products
Resources & support
Company
Site index

© Copyright 1996–2008 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index