The adventures of tin()

Using the if qualifer with time-series data is tricky. Until you meet tin(). Let us use quarterly German macro data, lutkepohl2, from Stata website to illustrate.
. webuse lutkepohl2
(Quarterly SA West German macro data, Bil DM, from Lutkepohl 1993 Table E.1)

. tsset
        time variable:  qtr, 1960q1 to 1982q4
                delta:  1 quarter

Suppose we want to plot investment (inv) and consumption (consump) over time but only from Q1 1970 to Q4 1980 using the if condition, how would we do this? First let us look at the data:

Screen Shot 2015-07-21 at 1.48.38 PM

qtr is displayed as 1960q1, 1960q2, and so on. Should our condition be “if qtr>=1970q1 & qtr<=1980q4"? No. This will return an error. Stata reads actual dates differently. To Stata, the time variable qtr are 0, 1, 2, ... Let us generate qtr2 to illustrate.
. gen qtr2 =  qtr
. list qtr qtr2 in 1/10

     |    qtr   qtr2 |
  1. | 1960q1      0 |
  2. | 1960q2      1 |
  3. | 1960q3      2 |
  4. | 1960q4      3 |
  5. | 1961q1      4 |
  6. | 1961q2      5 |
  7. | 1961q3      6 |
  8. | 1961q4      7 |
  9. | 1962q1      8 |
 10. | 1962q2      9 |

If we had monthly data, Stata would interpret time = 0 as January 1960, time = 1, as February 1960, and so on; and negative numbers represent months before January 1960.

Since 1960q1, 1960q2, etc. are only in display formats, we cannot use them literally as arguments in the if condition for our time-series chart, i.e., the following line will not work:
. tsline inv consump if qtr>=1970q1 & qtr<=1980q4 
1970q1 invalid name

One way is to use qtr2 but that would mean creating a new variable when you do not need one. Not cool.

Another way is to check the number Stata uses for the actual date using display.
. di q(1970q1)

. di q(1980q4)

. tsline inv consump if qtr>=40 & qtr<=83

And yet another (and the best) way is to use the time-series function tin(). We could just then write:
. tsline inv consump if tin(1970q1, 1980q4) 


See help tin() for more adventures of tin().

-destring- uncomplicated

In a comment to the previous post destring complication: negative numbers, Nick Cox pointed out “the most important advise” in using destring: “never destring, replace unless you are absolutely sure that you are right or are willing to do things again if you made the wrong decision. The generate() option is there for a purpose.”

In addition, his comment point to simpler solution than using regular expressions.

Continue reading

-destring- complication: negative numbers

Less than 2 hours flight…

In a Stata training, one of the students wondered why after importing an Excel file of financial indicators into Stata some were read as strings. A quick browse at the data indicates the presence of hyphens (“-“) and that these were used in different ways: one to indicate a negative number and another to indicate a missing observation.


How do we convert these variables to numeric as destring returns an error?

Continue reading

Import data from Excel sheets

How do we import data from all sheets in a number of Excel files? Each Excel file has a different number of sheets with names of no discernible pattern, but (thankfully) each sheet has the same structure: the first observation is in the same row and the columns correspond to each other. An example is the set of 17 Excel files of census data of barangays (villages) that was provided to me. Each Excel file corresponds to one region and within each file are sheets corresponding to the province in the region.  How do we consolidate all sheets in all files into one data file?

Continue reading

Haiku: cls

clean results window
in Stata thirteen, typing
cls clears screen


Where to buy Stata in the Philippines

In the Philippines, there has been an emerging demand in the public sector for data-driven analysis of various govenrment programs (yes, emerging.. don’t ask why.. but the leadership has a lot to do with this.. bless them). I am lucky enough to be invited to introduce Stata to some government agencies (and some private institutions) and how it can be used with their data. Often the the question where to get a copy of Stata pops up. So, for reference: here is the sole distributor of Stata in the country:

Continue reading

break me

Sometimes we want to break a continuous variable into a smaller set of categories—into evenly spaced or equally sized groups, or groups based on limits we specify, or groups based on another variable or a set of variables.

Let us take for example the variable price of cars in auto.dta.
sysuse auto.dta, clear    // open a Stata built-in data
summ price

Continue reading

Put anything anywhere in Excel without sweat

putexcel has recently become a very good friend. For those who (or working with people who) find comfort in working with tables in Excel after data processing or estimation in Stata (yes, there are others who don’t find comfort in this.) and already into Stata 13, learning putexcel could be very helpful (put an end to copy-pasting!). A number of user-written commands, such as outreg [1], outreg2, tabout, are also already available for similar purposes. What puts putexcel apart is its ‘user-friendliness’ and flexibility. You can put anything anywhere in Excel without sweat.

Continue reading

No -usespss- for Mac

In Reading SPSS data file into Stata, I describe Sergiy Radyakin’s -usespss- that loads SPSS data (.sav) into Stata. I was on Windows then. -usespss- is unfortunately not available for Mac OS. StatTransfer could easily do this if you have the software. Another option is to use R.

A quick Google search led me to a simple R routine that does exactly this. Following the steps outlined in Daniel Marcelino’s Loading SPSS (.sav) into Stata, I managed to covert an SPSS data set I downloaded from IDRA UCLA website into a Stata .dta file.

Try it out! It is easy to follow.

An interesting extension is Gabriel Rossman’s importspss.ado (requires R). It implements the R routine as an ado-file.

Tell me, where did I go wrong

If you are Filipino, you are most likely singing the title by now :)

Looking for a missing bracket, a misplaced comma, or a space that shouldn’t be there—or debugging in general—can be a pain sometimes. When the usual error message fails to point out where you messed up, try turning trace on to track down the error. trace literally traces the execution of programs. It echoes the lines that Stata executes internally. Reading through the whole thing on your results window can be daunting but you don’t really have to go through those. You just need to know which line it stops executing and see why it stopped there. To turn on trace, type:

set trace on
And.. don’t forget to turn it off when you don’t need it. They can be really very long.

set trace off
Next time you wonder where you went wrong, use trace before you lose your mind. For more options, see help set trace.

Now, continue with the singing.. “what did I do to make you change your mind completely..”