Piecewise execution of do-files in Stata

Do-files in Stata easily get a bit lengthy. Of course, you can try to shorten do-files and distribute code onto several do-files and have one master file that runs all of the respective sub-do-files (which are included by do dosubfile1.do). Alternatively, you can leave the do-file longish but write your code such that you only run parts of the code at once:

Continue reading “Piecewise execution of do-files in Stata”

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”

Check whether variable exists in if-conditions

In some applications, e.g. if you want to save coefficient estimates from a regression with many dummies (e.g. fixed effects), you might want to store coefficients as estimates. In this example, we are interested in storing the estimates of the GROUPVAR dummies, but not the dummies of OTHERVAR. While this is usually straightforward by writing

Continue reading “Check whether variable exists in if-conditions”

Standardize variables by group

Unfortunately, the otherwise great Stata command egen does not allow to standardize variables group, e.g. for each year separately. There is a small get-around by calculating mean and SD first, and then manually creating the standardized the variable (and then you really wonder why this is not implemented in Stata).

Continue reading “Standardize variables by group”

Manipulating variable labels

Occasionally we want to use variable labels in loops or we want to apply them to other labels. a very simple local syntax function for that is local localname: variable label varname.

As an example, suppose that I have three variables (x, y, z) with their respective labels. I want to create three other variables with their mean and I want to label those variables using the labels from x, y and z. I can type:

global varlist1 x y x

foreach var in $varlist1 {
local label`var': variable label `var'
egen mean_`var' = mean(`var')
label variable mean`var' "Mean of `label`var' ' "
}

The same idea is also useful for graphs and other commands

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”

Recording time stata needs for running a file

Running do-files with a large number of simulations or iterations often takes a lot of time. Either just for fun or in order to make your do-file more efficient, you may want to record the time Stata needs in order to run the whole file or just a part of it. The solution is –timer–. Just specify in the beginning Continue reading “Recording time stata needs for running a file”

Repetitive tasks … let STATA do the work

Especially in the process of data preparation, but also when one runs whole sets of analysis, we start repeating commands and sets of commands for similar variables. For example in one of my projects, I had to process salary information, that was monthly in wide format, and for several reasons I could not use reshape:

I could have typed:
gen str10 v201_1=""
replace v201_1=c2 if c1=="201"
gen str10 v201_2=""
replace v201_2=c3 if c1=="201"
[...]
gen str10 v201_12=""
replace v201_12=c13 if c1=="201"

Continue reading “Repetitive tasks … let STATA do the work”

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”

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”