Reputation: 593
I want to generate a date
variable starting from a count of the day and a starting date: I have a rolling cross-section with an integer variable that indicates the count of days since the survey was initially fielded. I also know this actual date. So, theoretically I should be able to generate a proper data variable, but I find no reference for this anywhere in the documentation.
Immagine I have one month of observations (in my case I have hundreds of observations for each day, but the problem is the same in this simplified case):
set obs 30
gen day=_n
I also know that day 1 is e.g. 12th of March 2015.
How do I generate a daily date
variable that ranges from 12th of March to the 11th of April?
Upvotes: 0
Views: 1036
Reputation: 10102
Good comments to read the help file for dates/times. This is also my most-read help file, which suggests that it may not be that approachable for new users.
I use mdy()
to determine the days since 1/1/1960, which I add to your day
variable. There are may ways to do this, but this should get you started.
clear
set obs 30
gen day=_n
generate dayCalendar = (day - 1) + mdy(3, 12, 2015)
format dayCalendar %td
This gives you the following.
. list in 1/5
+-----------------+
| day dayCale~r |
|-----------------|
1. | 1 12mar2015 |
2. | 2 13mar2015 |
3. | 3 14mar2015 |
4. | 4 15mar2015 |
5. | 5 16mar2015 |
+-----------------+
Upvotes: 1