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:*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 r(198);

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) 40 . di q(1980q4) 83 . 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()`

.Filed under: Basic functions, Graphics, Time series Tagged: | display, if, q(), tin(), tsline, tsset

## Leave a Reply