Difference between revisions of "Stata Coding Practices: Programming (Ado-files)"

Jump to: navigation, search
(Created page with "Programs and ado-files are the main methods by which Stata code is condensed and generalized. By writing versions of code that apply to arbitrary inputs and saving that code i...")
 
Line 3: Line 3:
 
==Read First==
 
==Read First==
  
This article will refer somewhat interchangeably to the concepts of "programming", "ado-files", and "user-written commands". This is in contrast to ordinary programming of do-files. The article does not assume that you are actually writing an ado-file (as opposed to a <syntaxhighlight lang="stata" inline>program</syntaxhighlight> definition in an ordinary dofile; and it does not assume you are writing a command for distribution. That said, Stata programming functionality is achieved using several core features:
+
This article will refer somewhat interchangeably to the concepts of "programming", "ado-files", and "user-written commands". This is in contrast to ordinary programming of do-files. The article does not assume that you are actually writing an ado-file (as opposed to a <syntaxhighlight lang="stata" inline>program</syntaxhighlight> definition in an ordinary dofile); and it does not assume you are writing a command for distribution. That said, Stata programming functionality is achieved using several core features:
  
 
* The <syntaxhighlight lang="stata" inline>program</syntaxhighlight> command sets up the code environment for writing a program into memory.
 
* The <syntaxhighlight lang="stata" inline>program</syntaxhighlight> command sets up the code environment for writing a program into memory.
Line 9: Line 9:
 
* The <syntaxhighlight lang="stata" inline>tempvar</syntaxhighlight>, <syntaxhighlight lang="stata" inline>tempfile</syntaxhighlight>, and <syntaxhighlight lang="stata" inline>tempname</syntaxhighlight> commands all create  objects that can be used within the scope of program execution to avoid any conflict with arbitrary data structures.
 
* The <syntaxhighlight lang="stata" inline>tempvar</syntaxhighlight>, <syntaxhighlight lang="stata" inline>tempfile</syntaxhighlight>, and <syntaxhighlight lang="stata" inline>tempname</syntaxhighlight> commands all create  objects that can be used within the scope of program execution to avoid any conflict with arbitrary data structures.
  
==The <syntaxhighlight lang="stata" inline>program</syntaxhighlight> command
+
==The <syntaxhighlight lang="stata" inline>program</syntaxhighlight> command==

Revision as of 19:24, 24 November 2020

Programs and ado-files are the main methods by which Stata code is condensed and generalized. By writing versions of code that apply to arbitrary inputs and saving that code in a separate file, the application of the code is cleaner in the main do-file and it becomes easier to re-use the same analytical process on other datasets in the future. Stata has special commands that enable this functionality. All commands on SSC are written as ado-files by other programmers; it is also possible to embed programs in ordinary do-files to save space and improve organization of code.

Read First

This article will refer somewhat interchangeably to the concepts of "programming", "ado-files", and "user-written commands". This is in contrast to ordinary programming of do-files. The article does not assume that you are actually writing an ado-file (as opposed to a program definition in an ordinary dofile); and it does not assume you are writing a command for distribution. That said, Stata programming functionality is achieved using several core features:

  • The program command sets up the code environment for writing a program into memory.
  • The syntax command parses inputs into a program as macros that can be used within the scope of that program execution.
  • The tempvar, tempfile, and tempname commands all create objects that can be used within the scope of program execution to avoid any conflict with arbitrary data structures.

The program command