# Stata/Programming

Stata
* Basics
** Introduction
** Settings
** Documentation
** Probability Function
** Random Number Generation
** Data Management
** Graphics
** Programming
** Mata
** Natural Language Processing
** Publication Quality Output
** Descriptive Statistics
* Methods
** Maximum Likelihood
** Bayesian Methods
** Bootstrap
** Non Parametric
** Matching
** Quantile Regression
* Topics
** Linear Models
** Binomial Outcome Models
** Multinomial Models
** Count Data Models
** Tobit and Selection Models
** Duration Analysis
** Multilevel Modeling
** Time Series
** Spatial Econometrics
** Factor Analysis
* Problems

## Macros

Stata includes 2 types of macros, local and global.

## Programs

* A program in Stata is a kind of function. It takes arguments and produces a result.
* which checks if a program already exists
A program can also return an r-class object

### 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.

## Personal ado-files

The best practice is to store your personal ado-files in the personal directory.

## 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.

## Unix-like function

* rmdir remove a directory
* mkdir create a new directory
* erase erase a file
* 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 :
* type print a file in the result window.
* copy will copy a file to disk
* filefilter replace a character by another one in a text file. The following command remove quotes from temp1.txt and replace them with a "-".
* tmpdir gives the temporary working directory

### Encoding problems

Sometimes, you need to convert the encoding of a dataset from latin1 to macroman. You can use the unix iconv command.

## Shell programs calling Stata

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:
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:
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. Programs that rely on these commands will also not work in this setting. Examples include: dirlist and parallel.
By default, Stata will inherit the working directory from the Shell. For example:
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.

## References

* ↑ http://www.stata.com/statalist/archive/2010-01/msg00537.html
* ↑ http://www.stata.com/statalist/archive/2011-07/msg00559.html