Saturday, March 7, 2026

Programming an estimation command in Stata: World macros versus native macros


I talk about a pair of examples that illustrate the variations between world macros and native macros. You possibly can view this put up as a technical appendix to the earlier put up within the #StataProgramming sequence, which launched world macros and native macros.

In each command I write, I take advantage of native macros to retailer stuff in a workspace that won’t alter a consumer’s knowledge and to make my code simpler to learn. A great understanding of the variations between world macros and native macros helps me to put in writing higher code. The important variations between world macros and native macros will be summarized in two factors.

  1. There is just one world macro with a selected identify in Stata, and its contents will be accessed or modified by a Stata command executed at any Stata stage.
  2. In distinction, every Stata stage can have an area macro of a selected identify, and every one’s contents can’t be accessed or modified by instructions executed at different Stata ranges.

If you’re already snug with 1 and a pair of, skip the rest of this put up.

That is the third put up within the sequence Programming an estimation command in Stata. I like to recommend that you simply begin originally. See Programming an estimation command in Stata: A map to posted entries for a map to all of the posts on this sequence.

World macros are world

The do-files globala.do and globalb.do in code blocks globala and globalb illustrate what it means to be world.

Code block 1: globala.do


*-------------------------------Start globala.do ---------------
*! globala.do
*  On this do-file we outline the worldwide macro vlist, however we
*  don't use it
world vlist var1 var2 var3

do globalb
*-------------------------------Finish globala.do ---------------

Code block 2: globalb.do


*-------------------------------Start globalb.do ---------------
*! globalb.do
*  On this do-file, we use the worldwide macro vlist, outlined in globala.do

show "The worldwide macro vlist incorporates $vlist"
*-------------------------------Finish globalb.do ---------------

The best method to see what this code does is to execute it; the output is in instance 1.

Instance 1: Output from do globala


. do globala

. *-------------------------------Start globala.do ---------------
. *! globala.do
. *  On this do-file we outline the worldwide macro vlist, however we
. *  don't use it
. world vlist var1 var2 var3

. 
. do globalb

. *-------------------------------Start globalb.do ---------------
. *! globalb.do
. *  On this do-file, we use the worldwide macro vlist, outlined in globala.do
. 
. show "The worldwide macro vlist incorporates $vlist"
The worldwide macro vlist incorporates var1 var2 var3

. *-------------------------------Finish globalb.do ---------------
. 
finish of do-file

. *-------------------------------Finish globala.do ---------------
. 
finish of do-file

Line 5 of globalb.do can entry the contents of vlist created on line 5 of globala.do as a result of vlist is a world macro.

Determine 1 makes this similar level graphically: the worldwide macro vlist is in world reminiscence, and a command executed wherever can entry or change the contents of vlist.

Determine 1: A world macro in world reminiscence

Native macros are native

The do-files locala.do and localb.do in code blocks 3 and 4 illustrate what it means to be native.

Code block 3: locala.do


*-------------------------------Start locala.do ---------------
*! locala.do
native mylist "a b c"
show "mylist incorporates `mylist'"

do localb

show "mylist incorporates `mylist'"
*-------------------------------Finish locala.do ---------------

Code block 4: localb.do


*-------------------------------Start localb.do ---------------
*! localb.do
native mylist "x y z"
show "mylist incorporates `mylist'"
*-------------------------------Finish localb.do ---------------

The best method to see what this code does is to execute it; the output is in instance 2.

Instance 2: Output from do locala


. do locala

. *-------------------------------Start locala.do ---------------
. *! locala.do
. native mylist "a b c"

. show "mylist incorporates `mylist'"
mylist incorporates a b c

. 
. do localb

. *-------------------------------Start localb.do ---------------
. *! localb.do
. native mylist "x y z"

. show "mylist incorporates `mylist'"
mylist incorporates x y z

. *-------------------------------Finish localb.do ---------------
. 
finish of do-file

. 
. show "mylist incorporates `mylist'"
mylist incorporates a b c

. *-------------------------------Finish locala.do ---------------
. 
finish of do-file

The code in blocks 3 and 4 and the output in instance 2 illustrate {that a} command executed on the stage of localb.do can’t change the native macro mylist that’s native to locala.do. Line 8 of locala.do shows the contents of the mylist native to locala.do. The contents are nonetheless a b c after localb.do finishes as a result of the native macro mylist created on line 3 of locala.do is native to locala.do and it’s unaffected by the mylist created on line 3 of localb.do.

Determine 2 makes this level graphically. The contents of the native macro mylist that’s native to locala.do will be accessed and altered by instructions run in locala.do, however not by instructions run in localb.do. Analogously, the contents of the native macro mylist that’s native to localb.do will be accessed and altered by instructions run in localb.do, however not by instructions run in locala.do.

Determine 2: Native macros are native to do-files
graph1

Finished and Undone

I primarily supplied you with a technical appendix to the earlier #StataProgramming put up. I illustrated that world macros are world and that native macros are native. I take advantage of the ideas developed up to now to current an ado-command within the subsequent put up.



Related Articles

Latest Articles