Stata/Programming
Macros
[edit | edit source]Stata includes 2 types of macros, local and global.
Programs
[edit | edit source]- A program in Stata is a kind of function. It takes arguments and produces a result.
- which checks if a program already exists
. which hello /Applications/Stata/ado/personal/hello.ado
which hello cap program drop hello program define hello di "say hello" end hello program drop hello
A program can also return an r-class object
. clear . set obs 100 obs was 0, now 100 . gen u=invnorm(uniform()) . *** rclass . program example, rclass 1. return scalar x=1 2. end . example . ret list scalars: r(x) = 1
Passing argument to a program
[edit | edit source]By default arguments are called using macros `1', `2',…,`N'. Here is an example of how it works. The program is very simple and just display the arguments if any.
. cap program drop tester . program tester 1. di "argument 1 is |`1'|" 2. di "argument 2 is |`2'|" 3. di "argument 3 is |`3'|" 4. di "argument 4 is |`4'|" 5. end . . tester argument 1 is || argument 2 is || argument 3 is || argument 4 is || . tester a b c argument 1 is |a| argument 2 is |b| argument 3 is |c| argument 4 is || . tester ab cd ee pei argument 1 is |ab| argument 2 is |cd| argument 3 is |ee| argument 4 is |pei|
Personal ado-files
[edit | edit source]The best practice is to store your personal ado-files in the personal directory.
. personal dir your personal ado-directory is /Applications/Stata/ado/personal/
Help files
[edit | edit source]Help files are using a specific mark up language. You can open an help file in your text editor and see the syntax.
Unix-like function
[edit | edit source]- rmdir remove a directory
- mkdir create a new directory
- erase erase a file
erase temp.dta
- shell or ! execute a unix/dos command. For instance in a unix environment (Mac/Linux), one can use the iconv command line to change the encoding from macroman to latin1 :
! iconv -f latin1 -t macroman dataxmlWin.xml > dataxmlMac.xml
- type print a file in the result window.
type textfile.txt
- copy will copy a file to disk
copy http://en.wikibooks.org/wiki/Stata wikibook.txt
- filefilter replace a character by another one in a text file. The following command remove quotes from temp1.txt and replace them with a "-".
filefilter temp1.txt temp2.txt, from("\Q") to("-") replace
- tmpdir gives the temporary working directory
Encoding problems
[edit | edit source]Sometimes, you need to convert the encoding of a dataset from latin1 to macroman. You can use the unix iconv command.
cap prog drop win2mac prog define win2mac args basewin use `basewin', clear xmlsave dataxmlWin, doctype(dta) replace ! iconv -f latin1 -t macroman dataxmlWin.xml > dataxmlMac.xml xmluse dataxmlMac.xml, doctype(dta) end
Shell programs calling Stata
[edit | edit source]For ongoing projects one may want to embed Stata scripts into an automated workflow. This section will show you how to run Stata do files from the command-line. For these examples you will have to include the folder of your Stata executable into your PATH environment variable. Depending on your "flavor" of Stata (SE, MP, IC, Small) your Stata executable may also have a slightly different name.
If you just call Stata with the do-file as its only argument, such as:
StataSE myscript.do
Then Stata will open in normal (interactive) mode and do one of two things depending on your user preferences. It may execute the script (running -do myscript.do-) or open the do-file editor (-doedit myscript.do-). To set this preference open the do-file editor and then go to Edit, Preferences, and set the option for "Edit do-files opened from Windows instead of executing them".
Alternatively, you can call Stata in background (batch) mode, such as:
StataSE /e do myscript.do
This will execute your script, keep the Stata window minimized, and automatically log everything to myscript.log in the current working directory (there is no way to turn off this automatic logging in batch mode).
Calling shell commands (!/shell/winexec) in batch mode will not work from Window[1]. Programs that rely on these commands will also not work in this setting. Examples include: dirlist[2] and parallel.
By default, Stata will inherit the working directory from the Shell. For example:
cd /myproject/ Stata /e do code/myscript.do
Stata will run with the working directory as /myproject. The log file will be there as well so this is a way to have your log files end up in a different directory than you do files.