Stata/Programming

From Wikibooks, open books for an open world
< Stata
Jump to: navigation, search

Contents

[edit] Macros

Stata includes 2 types of macros, local and global.

[edit] Programs

  • 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

[edit] Passing argument to a program

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|

[edit] Personal ado-files

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/

[edit] Help files

Help files are using a specific mark up language. You can open an help file in your text editor and see the syntax.

[edit] Unix-like function

  • 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

[edit] Encoding problems

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
Previous: Graphics Index Next: Mata
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export