Cauane Blumenberg

Data scientist . PhD Epidemiology . MSc Computer Science

Youtube video featuring labtrunc12

I’have recently started a new project: a Youtube channel that will focus on the usage of several Stata commands. I’ll cover both beginner and advanced commands.

We still have only 3 videos published. One of the videos show how to use the labtrunc12 command to enable a Stata dataset to be saved using an older Stata version. The video can be found at the end of this post.

All videos will be in Portuguese, but we plan to add some subtitles in English. So, don’t forget to subscribe to the channel and stay tuned!

Truncating variable labels to save databases in Stata 12 version

Sometimes we have to save our Stata databases using an old version. This has to be done, because some people still work with the long standing Stata 12 version. However, when we use the saveold command to do so, we sometimes face a problem with the long variable labels.

For this reason I developed an ado file called labtrunc12 that truncated all variable labels to an adequate length in order to save the database using the Stata 12 version. The syntax is very simple, you just have to type labtrunc12 and all database labels will be truncated. Instructions to use the command can be obtained typing help labtrunc12 into Stata.

Download labtrunc12 from my git repository: https://github.com/cauanebs/labtrunc12

Installing ado files in Stata

Perform the following steps to install ado files in Stata:

  1. Open Stata
  2. Type personal and hit enter to discover the location of your personal ado folder
  3. Navigate to the personal ado folder (if after navigating there is no folder called personal, create one)
  4. Unzip the downloaded file into the personal ado folder

Merging three numeric variables into one date format variable

There are many advantages of using datetime variables in Stata, including the possibility to perform simple calculations between dates. However, working with dates in Stata is not a simple task.

Trying to skip the difficulties of using datatime variables in Stata, some research groups collect dates into three separate variables containing the day, the month and the year. But, by doing this, it becomes very difficult to perform calculations.

For this reason, I developed a Stata command called mergedate. This command merges three numeric variables (day, month, and year), and creates on single variable using the datetime format. Instructions to use the command can be obtained typing help mergedate into Stata.

Download mergedate from my git repository: https://github.com/cauanebs/mergedate

Installing ado files in Stata

Perform the following steps to install ado files in Stata:

  1. Open Stata
  2. Type personal and hit enter to discover the location of your personal ado folder
  3. Navigate to the personal ado folder (if after navigating there is no folder called personal, create one)
  4. Unzip the downloaded file into the personal ado folder

Finding nonnumeric characters within string variables

The destring is one of the main commands used to clean databases in Stata, as it can be used to convert string variables to numeric variables. This is a very important feature, especially if a database from another source (e.g. Excel) is imported into Stata.

The destring command will only work if the string variable that we are trying to convert to numeric contains no nonnumeric characters. For example, if we have a variable coded as “0” or “1”, but in a given observation it was coded by mistake as “1b”, the destring command will not work. If we are dealing with small databases, it might be easy to visually identify which observation was wrongly coded. However, if we have a large database, containing lots of observations, this process might be difficult.

For this reason, I developed a Stata command called findnonnumeric. This command searches form nonnumeric characters within a string variable and lists the observations that have this issue. Instructions to use the command can be obtained by typing help findnonnumeric

Download findnonnumeric from my git repository: https://github.com/cauanebs/findnonnumeric

Installing ado files in Stata

Perform the following steps to install ado files in Stata:

  1. Open Stata
  2. Type personal and hit enter to discover the location of your personal ado folder
  3. Navigate to the personal ado folder (if after navigating there is no folder called personal, create one)
  4. Unzip the downloaded file into the personal ado folder