Monday, December 8, 2014

Convert string to date in Stata

Stata is a powerful tool for data analysis. It is also a data management tool that makes work easier.

Currently, I am exploring the Philippine employment data from 2002 to 2014 which looks like this:


The problem that I encountered when I copied and pasted these data in Stata is that the period variable is stored as string.
In time series, the period should be converted into date type.  The command to convert period into date type is:

generate period2=date(period, "MY", 2020)

The date () function takes 3 arguments.  The first is the variable you want to convert, the second is the format of the variable and the last is the top year or limit of the year.

format period2 %td

This command formats the period2 variable into day.

generate quarterly =qofd(period2)
format quarterly %tq

Finally, qofd() command takes the period2 variable and converts it to corresponding quarter.  I think qofd () stands for quarter of date.

Type list period emprate period2 quarterly in 1/5 to show that period has been properly converted into date format.

period                         emprate                        quarterly

1. Jan-02                      89.7                            2002q1
2. Apr-02                     86.1                            2002q2
3. Jul-02                      88.8                             2002q3
4. Oct-02                     89.8                             2002q4
5. Jan-03                     89.4                             2003q1 

No comments:

Post a Comment