How to generate monthly in Stata, Creating Monthly Variable in Stata

How to generate monthly in Stata, Creating Monthly Variable in Stata

In Stata you can generate monthly with the format tm().

Creating Monthly Variable in Stata
clear all
set obs 100
gen month = tm(2021m1) + _n-1
format %tm month

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

tm is function to make typing monthly dates.

tm(2021m1) + _n-1 the month start in 1/2021 and increasing by 1 month.

Creating Monthly Variable in Stata

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

 Creating Monthly Variable in Stata

Comments