Creating Date Variable in Stata

Creating Date Variable in Stata

If you want to create daily for time series analysis you can use td() function.

In Stata you can generate date string using td() function.

Creating Date Variable in Stata

clear all
set obs 100
gen date = td(1may2021) + _n-1
format date %td date

Explain:

clear all is remove all data, value labels, matrices, scalars, constraints, clusters, stored results, frames, sersets, and Mata functions and objects from memory

set obs 100 is set observation to 100

gen is generate new variable

td(1may2021) function to make typing dates, date start from May 01, 2021.

tm(1may2021) + _n-1 the date start in May 01, 2021 and increasing by 1 day.

Creating Date Variable in Stata

format %tm date format the number into date string format.

Creating Date Variable in Stata




Comments