Information about Stata macros: number of elements and order within locals and globals

Especially when running loops (see our posts here), it can be important to get more information about a macro, such as the length, or its first or last element.

Continue reading “Information about Stata macros: number of elements and order within locals and globals”

Format how locals are displayed in Stata

Often I use locals to calculate something (such as the mean of a variable for a specific group) and use this in a loop (say, over groups or over years). If you, for example, want to calculate the share of observations that belong to one group (say: female==1), you could simply write

Continue reading “Format how locals are displayed in Stata”

Add current date to Stata file names (and others)

We wrote earlier about how to add time stamps (current date and/or time) to your Stata figures, e.g. to keep track of work in progress results. A similar strategy can be used when storing log-files, which are typically overwritten with a more recent version of the same log.

Continue reading “Add current date to Stata file names (and others)”

Use of embedded quotation marks within locals in Stata

Proper use of single and double quotation marks is essential when working in Stata, especially when writing loops where locals can be a huge time and memory saver. The use of single and double quotation marks is rather straightforward (using ` and ‘ for single, and ” for double quotation marks). You can rather easily define a local, e.g. based on the average of a variable

Continue reading “Use of embedded quotation marks within locals in Stata”

Technical note: use of quotation marks

Be aware of the proper use of single and double quotation marks when copying Stata or other codes from this blog (but also when copying codes from other websites). Often opening or closing quotation marks are wrongly formatted. Continue reading “Technical note: use of quotation marks”

Running regressions with similar sets of variables

Quite often we run variations on regressions, including or excluding (sets of) variables. Copy-pasting the regression and eliminating the variables to be excluded is one way, but given that we speak of sets of variables why not use locals to do the work for you:
Continue reading “Running regressions with similar sets of variables”

Using Stata to randomise vignettes in NetQuestionnaire

One huge disadvantage of NetQuestionnaire (NetQ) is that randomisation is only possible for the order of sub-items of questions. There is, however, a way to use Stata to make anything random (e.g. the order of questions, content of questions). Below, I describe a way how to generate randomly sets of vignettes. At the end of the do-file, text is saved to an Excel-file). Merged with an address-list, this information can be imported to NetQ, and used in questions using the NetQ-variables. Continue reading “Using Stata to randomise vignettes in NetQuestionnaire”

Why doesn’t this do-file run through …?

Quite often a do-file is written to run on various data-files that all seem to be the same. Say, you have done it for one year of a data-set and want to repeat the same for the subsequent years that you have data. Now, a check whether the data actually has some data (given your selection) is often a good idea, here is how I did it in a recent project:


count
assert `r(N)'>1

I am using the saved return local variable r(N) that STATA automatically generates after count. This is a need feature that you should consider for many other commands (try return list after your favourite command).

Continue reading “Why doesn’t this do-file run through …?”

Export single numbers to LaTeX or MS Word

In a text, you often you refer to a number (e.g. the number of observations in the estimation sample). There is a simple way to automise the export of this number from a Stata-do-file to a Latex-document. Continue reading “Export single numbers to LaTeX or MS Word”

Using locals and loops to generate long strings

The following command can be used to generate a command which consists of several new variables which are generated within a loop.

This could either be done by (e.g. generating a number of log variables)
gen newvar1 = log(var1)
gen newvar2 = log(var2)
etc.
Continue reading “Using locals and loops to generate long strings”