Ado-files: check and install automatically

If you need a certain ado-file to run your do-file and you are working on different computers, there is a way that Stata checks whether the needed ado-files are installed and that needed ado-files are automatically installed if they are not yet Continue reading “Ado-files: check and install automatically”

Weekly data: calendar week vs. Stata weeks

In my dataset, I have information on a number of workers for each week. The raw data I receive (no Stata format) contains information on the year and the week (1, 2, …, 52). Here, a week is defined as the first week of a year which has at least 4 days in January. E.g., the week 1/2009 starts already on December 29, 2008. A result of that is that for some years, a year has 53 calendar weeks. Continue reading “Weekly data: calendar week vs. Stata weeks”

Prevent variable abbreviation in Stata

In many cases, variable abbreviation is a handy tool in Stata (e.g. writing w instead of wage if wage is the only variable name starting with w). When using large numbers of variables with similar names, however, it can be quite dangerous, especially when do-files are run at once.

An easy way to prevent any variable abbreviation is stating:

set varabbrev off, permanent

Managing time variables in Stata and SPSS

I just came across the problem that a date in Stata is not the same as a date in SPSS. They are, however, based on the same principle: time variables, e.g. a variable containing days, contain the number of elapsed time units (days, weeks, months, seconds, or other date units) since a base date which is defined by the software.

In Stata, the base date is the first time unit in 1960 (e.g. January 1, 1960). For other time units, Continue reading “Managing time variables in Stata and SPSS”

Transform string variable to categorical integer variable

When cleaning datasets one often has string variables containing categories (e.g. country names). A simple way of transforming such a variable to one variable containing the same information is encode. Encode assigns numerical values 1, 2, … to newvar, while the original values (e.g. country names) are kept as labels. Continue reading “Transform string variable to categorical integer variable”

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”

Using question marks and stars in strings

I just came accross the following problem: suppose you have a string variable (stringvar) which contains text and question marks. Question marks are usually used as wildcards for single letters or numbers. So, when you want to apply some changes to the variable (here: removing the “?”), you should NOT type Continue reading “Using question marks and stars in strings”

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”