Tag Archives: programming

Learning the basics of Unix

Teaching computer skills to students has always been a challenge in Economics. The first difficulty is the limited amount of time for them to learn the basics of any software. A second difficulty is when the software is only available for Unix. Recently, I found a very simple yet comprehensive tutorial for the basics of Unix. Most of my students that used it were able to quickly gain some proficiency and write better code, especially when using bash.

Nesting local macros in Stata

Local macros are a very useful feature of Stata. Here is a simple example.

local macro1 “Hello!”

local macro2 = “How are you?”

local macro3 2+2

local macro4 = 2+2

di ” `macro1'`macro2′ ”

di “Here’s some math: `macro3' = `macro4′ ”

 

A few comments are in order here.  macro1′ tells Stata to replace this expression with the contents of the local macro1. Make sure to leave no spaces inside the `’. Also, note how macro3 and macro4 lead to different outcomes. You need to use the equal sign to make Stata to evaluate the expression, otherwise it will treat it like a string.

Now, Stata applies the “parentheses rule” when replacing local macros for their contents. That is, it first replaces the innermost local macro, then the second innermost local macro, and so on. Here is an example.

local macro5 “nest”

local nestmacro “Local macros can be nested!”

di ” “macro5’macro’ ”

Let’s add another layer to the nesting of the previous example and play with it a little bit more:

local macro5 “nest”

local macro6 “macro”

local macro7 5

local nestmacro “Local macros can be nested!”

di ” “macro`macro7”`macro6” ”

I guess you have a pretty good idea of how this works now. Good luck in your Stata coding.

 

 

A little bit more about loops and macros in Stata

Loops and lists are important tools for Stata programmers. Although these tools may not be the most computationally efficient technique, they can be of great help during the early stages of code development. By the way, it is always good to keep in mind that a good programming practice is to always keep definitions local to the do-file you are coding whenever possible.

The first example is about using a counter. You can place it on the do-file editor and then run it.

local fruits apple banana pear
local counter 0
foreach d of local fruits{
local counter= `counter’+1
display “ `counter’ – `d’”
}

After running it, the output should be something like this:

1 – apple
2 – banana
3 – pear

Notice that I used the equal sign “=” to build the counter. In short, you have to use it. Just for fun, see what happens when you replace “local counter= `counter’+1” by “local counter `counter’+1”.

Now suppose we have an ordered list of produce 4-digit codes and we would like to display the PLU code next to each fruit name.

local fruits apple banana pear
local plucode 3009 4011 4414
/* The first step is to find out the number of fruits */
local fruitnumber: word count `fruits’
di “Fruit – PLU code”
/* The second step is to build a loop to pair each fruit with  its PLU code */
forvalues j = 1/`fruitnumber’ {
/* The third step is to pick the j-th element of each list to have them paired */
local pair1: word `j’ of `fruits’
local pair2: word `j’ of `plucode’
display “`pair1′ – `pair2′”
}

Output:

. local fruits apple banana pear

. local plucode 3009 4011 4414

. /* The first step is to find out the number of fruits */
. local fruitnumber: word count `fruits’

. di “Fruit – PLU code”
Fruit – PLU code

.
. /* The second step is to build a loop to pair each fruit with  its PLU code */
. forvalues j = 1/`fruitnumber’ {

/* The third step is to pick the j-th element of each list to have them paired */

local pair1: word `j’ of `fruits’
local pair2: word `j’ of `plucode’
display “`pair1′ – `pair2′”
}

apple – 3009
banana – 4011
pear – 4414
.
end of do-file

 

So, the “word count” function returns the number of elements of the macro. And the “word `j’ of `fruits’” returns the j-th element of the macro fruits. You can find more about these macro extended  functions on Stata by typing “help extended_fcn”.

A few tips for programming in Stata

Stata is a very powerful and useful statistical software. Just like any sophisticated tool, it takes time to learn about it. And you need to invest some time to master it. Programming is one of those skills that knowing a little bit can be very beneficial. Below you will find four videos. The first video goes over the functionalities of the Stata Program Editor. The second video covers some basics of Stata commands. The third video talks about loops, which are an essential tool for programmers. Finally, the fourth video is about macros, which together with loops are very useful to handle repetitive tasks.

How to use the Stata Program editor:

Basics of Stata:

Quick guide to loops:

More about macros:

How to plot curves with different domains in Mathematica

Mathematica (or the Wolfram language) is a very useful tool to plot functions. Suppose you are interested in plotting two curves in the same diagram, let’s say y = 1/x and z = 2/x. Both functions have the same domain of x belonging to the [0,10] interval. This is easily accomplished by typing:

Plot[{1/x, 2/x}, {x, 0, 10}]

 

Suppose now that you would like to plot in the same diagram both functions but the domain of z change to x belonging to the [2,12] interval. How could you do this? This is not difficult. You just need to use the command show with the option PlotRange->All. Here is a piece of code that accomplish this:

p1 = Plot[{1/x}, {x, 0, 10}]

p2 = Plot[{2/x}, {x, 2, 11}]

Show[p1, p2, PlotRange->All]