Leveraging personal programs with Stata

In the Stata New 40-1, it is easier to have access to template do-files from the menu. In this blog, I will show how to use a program to reduce the number of repetitive tasks that you have to do.

We intend to utilize the monthly dataset retrieved from this article:

I can start by creating an ado file in the desired folder. This program is an “ado” file that will estimate a four-variable VAR and recover the structural shocks relying on a recursive identification, as described in this blog. In addition, the program ‘svarjamel.ado’ will plot the reduced-form and structural shocks:

*! version 1.0.0  29jan2025
program define svarjamel
 version 18.5 // or desired version
	
	matrix A = (1,0,0,0\.,1,0,0\.,.,1,0\.,.,.,1)
	matlist A

	matrix B = (.,0,0,0\0,.,0,0\0,0,.,0\0,0,0,.) 
	matlist B

	varsoc $X, maxlag(12)

	svar $X, aeq(A) beq(B) ///
	lags(1/12) 

	/* compute the inv(B)*A matrix */
	matrix A=e(A)
	matrix B=e(B)
	matrix BA = inv(B)*A
	/* compute reduced form epsilon_t residuals */
	var $X
	capture drop epsilon*
	predict double epsilon1,residual eq(#1)
	predict double epsilon2,residual eq(#2)
	predict double epsilon3,residual eq(#3)
	predict double epsilon4,residual eq(#4)
	/* store the epsilon* variables in the epsilon matrix */
	mkmat epsilon*, matrix(epsilon) 
	/* compute e_t matrix of structural shocks */
	matrix e = (BA*epsilon')'
	/* store columns of e as variables e1, e2, and e3 */  
	svmat double e

	label variable epsilon1 "Reduced-form shocks"
	label variable e1 "Structural shocks"

	twoway (tsline e1 if Period>tm(2000m1)) (tsline epsilon1 ///
	if Period>tm(2000m1), yaxis(1)), ///
	name(G, replace) legend(position(6)) graphregion(margin(r+5))

end

In the code, the vector of variable is stored in a global macro called X. Then, I can launch the code in the same folder where the ado is stored:

cap program drop svarjamel // Delete previous running program

cd "C:\Users\jamel\Dropbox\Documents\blog-data" // Choose the current directory
qui do "svarjamel.ado" // Quietly run the program 

use database_pri_gpr.dta, clear // Use the database in Mignon and Saadaoui (2024)

global X lpri lpro ldem lrpo // Store the vector of variable in a macro

svarjamel // Execute the code

The results:

You can leverage the power of the programs and reduce the number of repetitive portions in your code. The code is now available from the editor menu:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.