Category: Stata

  • 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

  • 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.,…

  • 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…

  • 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…

  • Comparing groups

    In some analyses you may want to compare to groups, e.g. wages of women and men, or random assignment in an experiment. Suppose you have a continuous variable (e.g. –wage–) and a dichotomous variable (e.g. –gender–). In this case,

  • Displaying numbers in Stata: commas, decimals, and decimal places

    There is an easy way to change commas to decimals and vice versa: set dp comma or

  • 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.

  • 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.

  • 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

  • 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–.…