I would like to create several time sequence where each value is 6 months later than the previous one. For example January 1st, 2010; July 1st, 2010; January 1st, 2011;.....
I have created a loop that does this but I have problems if I start the sequence with July 1st, 2010 for example. Thank you.
4 Answers
0
Are you trying to create a vector of dates in dt scalar format? Like this:
my_date = 20100701 20110101 20110701 20120101
Numbers in this format can be used with the plotTS function and also converted to strings for printing with the dtToStr function like this:
print dtToStr(20100701, "YYYY-MO-DD");
returns:
2010-07-01
or like this:
print dtToStr(20100701, "MO/YYYY");
produces:
07/2010
0
Yes, I'm trying to create a long vector such as "my_date". Any ideas? Thank you.
0
The functions that work with dt scalar format treat 20080101 and 200801 both as January 1st, 2008. So if your date will always be the first day of the month you don't have to specify it. Since that is slightly simpler to look at my examples will assume that.
You can put this all on one line (or put it in a procedure so that you don't have to remember the details), but we will go through it in a more verbose fashion to make everything explicit and easy to follow.
- Create a vector with the years.
num_years = 9; //Create the additive sequence 2006, 2007, 2008... dt_vec = seqa(2006, 1, num_years);
- Create a vector of the same length as your dt_vec date sequence containing the months (i.e. alternating 1 and 7)
months = { 1, 7 }; months = reshape(months, num_years, 1);
- Multiply the years by 10 to make room for the months (i.e. 2006 to 200600)
dt_vec = dt_vec * 10;
- Add in the months
dt_vec = dt_vec + months;
Or you could use a more condensed version like this:
num_years = 9; dt_vec = seqa(2006, 1, num_years) * 10 + reshape(1|7, num_years, 1);
If I was going to do this a lot, I would create a procedure that I could call like this:
dt_vec = seqDate(first_year, month_increment, num_years);
That would be pretty simple to do starting from the code above. The only problem is that it will always start in January and you may not want that. You could either modify the procedure to deal with that, or just use the trimr function to trim off the first few rows that you don't want. Feel free to post if you would like help with something like this.
0
Thank you. That could work.
Your Answer
4 Answers
Are you trying to create a vector of dates in dt scalar format? Like this:
my_date = 20100701 20110101 20110701 20120101
Numbers in this format can be used with the plotTS function and also converted to strings for printing with the dtToStr function like this:
print dtToStr(20100701, "YYYY-MO-DD");
returns:
2010-07-01
or like this:
print dtToStr(20100701, "MO/YYYY");
produces:
07/2010
Yes, I'm trying to create a long vector such as "my_date". Any ideas? Thank you.
The functions that work with dt scalar format treat 20080101 and 200801 both as January 1st, 2008. So if your date will always be the first day of the month you don't have to specify it. Since that is slightly simpler to look at my examples will assume that.
You can put this all on one line (or put it in a procedure so that you don't have to remember the details), but we will go through it in a more verbose fashion to make everything explicit and easy to follow.
- Create a vector with the years.
num_years = 9; //Create the additive sequence 2006, 2007, 2008... dt_vec = seqa(2006, 1, num_years);
- Create a vector of the same length as your dt_vec date sequence containing the months (i.e. alternating 1 and 7)
months = { 1, 7 }; months = reshape(months, num_years, 1);
- Multiply the years by 10 to make room for the months (i.e. 2006 to 200600)
dt_vec = dt_vec * 10;
- Add in the months
dt_vec = dt_vec + months;
Or you could use a more condensed version like this:
num_years = 9; dt_vec = seqa(2006, 1, num_years) * 10 + reshape(1|7, num_years, 1);
If I was going to do this a lot, I would create a procedure that I could call like this:
dt_vec = seqDate(first_year, month_increment, num_years);
That would be pretty simple to do starting from the code above. The only problem is that it will always start in January and you may not want that. You could either modify the procedure to deal with that, or just use the trimr function to trim off the first few rows that you don't want. Feel free to post if you would like help with something like this.
Thank you. That could work.