Home > Net >  How to transform date in Stata?
How to transform date in Stata?

Time:10-01

I've looked for help on the internet for the following, but I could not find a satisfying answer: for an assignment, I need to plot the time series of a certain variable (the term spread in percentages), with years on the x-axis.

However, we use daily data. Does anybody know a convenient way in which this can be done? The 'date' variable that I've got is formulated in the following way: 20111017 represents the 17th of October 2011.

I tried to extract the first 4 numbers of the variable 'date', by using the substr(date, 1, 4) command, but the message 'type mismatch' popped up. Also, I'm not quite sure if it gives the right information if I only use the years to plot daily data (over the years). It now gives the following graph, which doesn't look that nice.

CodePudding user response:

Answering the question in your title.

The date() function expects a string. If your variable with value 20111017 is in a numeric format you can convert it like this: tostring datenum , gen(datestr).

Then when using the date() function you must provide a mask that tells Stata what format the date string is in. Below is a reproducible example you can run to see how this works.

* Example generated by -dataex-. For more info, type help dataex
clear
input float datenum
20111016
end

* Convert numberic varaible to string
tostring datenum , gen(datestr) 

* Convert string to date
gen date = date(datestr, "YMD")

* Display date as date
format date %td

If this does not help you, try to provide a reproducible example.

CodePudding user response:

This adds some details to the helpful answer by @TheIceBear.

As he indicates, one way to get a Stata daily date from your run-together date variable is convert it to a string first. But tostring is just one way to do that and not essential. (I have nothing against tostring, as its original author, but it is better suited to other tasks.)

Here I use daily() not date(): the results are identical, but it's a good idea to use daily(): date() is all too often misunderstood as a generic date function, whereas all it does is produce daily dates (or missings).

To get a numeric year variable, just divide by 10000 and round down. You could convert to a string, extract the first 4 characters, and then convert to numeric, but that's more operations.

clear 
set obs 1 
gen long date = 20111017
format date %8.0f 

gen ddate = daily(strofreal(date, "%8.0f"), "YMD")
format %td ddate 
gen year = floor(date/10000)

list  

      ----------------------------- 
     |     date       ddate   year |
     |-----------------------------|
  1. | 20111017   17oct2011   2011 |
      ----------------------------- 

  • Related