user1691278
user1691278

Reputation: 1885

Using Stata to graph dates

I'm trying to graph the CPI over the years. Therefore, I want to make the x-axis as the dates and the y-axis as the numerical values. I got the data in .csv file and the dates are in the format yyyy-mm-dd. However, whenever I use

graph twoway scatter date value

Stata returns: "varlist: date: string variable not allowed". How can I format the dates into such a way that Stata can read? Thanks.

Upvotes: 1

Views: 4065

Answers (2)

Vivek
Vivek

Reputation: 357

After formatting the date, you can also use xtline command to plot.

xtline GDP, i(country_id) t(time_variable)

Upvotes: 0

dimitriy
dimitriy

Reputation: 9460

Try help datetime to see how to convert string dates to Stata dates (integers that represent days since Jan 1, 1960 with nice labels). You most likely need something like:

gen stata_date = date(csv_date,"YMD")
format stata_date %td

or

format stata_date %tdCCYY/MM/DD

to use the same formatting as before.

There's also a twoway tsline command that works nicely with dates once you tsset your data as a time series.

Finally, you have x and y reversed.

BTW, you can get a lot of data from user-written quandl, freduse, or wbopendata. Here's the time-series for the CPI for All Urban Consumers (excluding farmers, soldiers, criminals who got caught, and really crazy people):

capture ssc install quandl 
quandl, q(FRED/CPIAUCSL) clear 
tw tsline value

This has the advantage of doing the formatting/tssetting for you.

Upvotes: 2

Related Questions