generate trend by sequence
Moderators: EViews Gareth, EViews Moderator
generate trend by sequence
Hi for everyone,
I have three workfile with three different frequency from 2004 to 2015 in three workfile (weekly "02/01/2004 25/12/20015", quarterly "2004q1 2015q4, unstructured 80 observation).
i need that:
for weekly: the last week of "Jan" take value 1, the last week of "Feb" take value 2, the last week of "Mar" take value 3, ...and so on (otherwise take zero).
for quarterly: "2004q4" take value 1, "2005q4" take value 2, "2006q4" take value 3, ...and so on (otherwise take zero).
for unstructured: the forth obs take value 1, the eight obs take value 2, the twelfth obs take value 3, ...and so on (otherwise take zero).
I have three workfile with three different frequency from 2004 to 2015 in three workfile (weekly "02/01/2004 25/12/20015", quarterly "2004q1 2015q4, unstructured 80 observation).
i need that:
for weekly: the last week of "Jan" take value 1, the last week of "Feb" take value 2, the last week of "Mar" take value 3, ...and so on (otherwise take zero).
for quarterly: "2004q4" take value 1, "2005q4" take value 2, "2006q4" take value 3, ...and so on (otherwise take zero).
for unstructured: the forth obs take value 1, the eight obs take value 2, the twelfth obs take value 3, ...and so on (otherwise take zero).

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11811
 Joined: Tue Sep 16, 2008 5:38 pm
Re: generate trend by sequence
1) How are you defining the last week of a month?
2)
3)
2)
Code: Select all
series mytrend = @recode(@quarter=4, @recode(mytrend(4)=na,1,mytrend(4)+1),0)
3)
Code: Select all
series mytrend = @recode(@mod(@trend,4)=3, @recode(mytrend(4)=na,1,mytrend(4)+1),0)
Follow us on Twitter @IHSEViews
Re: generate trend by sequence
If i understand:
my first week start on "Friday" 02/01/2004 for January
my last week start on "Friday" 30/01/2004 for January
my first week start on "Friday" 02/01/2004 for January
my last week start on "Friday" 30/01/2004 for January

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11811
 Joined: Tue Sep 16, 2008 5:38 pm
Re: generate trend by sequence
That doesn't really answer the question.
You need to define how to decide which month a week is in.
You need to define how to decide which month a week is in.
Follow us on Twitter @IHSEViews
Re: generate trend by sequence
The day of the week in my workfile structured on Friday.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11811
 Joined: Tue Sep 16, 2008 5:38 pm
Re: generate trend by sequence
What does that have to do with months?
Follow us on Twitter @IHSEViews
Re: generate trend by sequence
Weeks and months do not align. day at the beginning of next week "a Friday" is within the next month.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11811
 Joined: Tue Sep 16, 2008 5:38 pm
Re: generate trend by sequence
I think we're on the right track, but what you wrote is a little hard to follow.
I think you are saying that the last week of a month is the week containing the last Friday in the month?
I think you are saying that the last week of a month is the week containing the last Friday in the month?
Follow us on Twitter @IHSEViews
Re: generate trend by sequence
Sometimes I do not find the words to express myself well.
Yep!.
Yep!.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11811
 Joined: Tue Sep 16, 2008 5:38 pm
Re: generate trend by sequence
Here's the first way that springs to mind:
Code: Select all
series mytrend = @recode(@recode(@month<@month(1),1,0)=0,0,@cumsum(@recode(@month<@month(1),1,0)))
Follow us on Twitter @IHSEViews
Re: generate trend by sequence
Good.
Can i asking another questions? just by curiosity.
Are there another way for:
I need that for quarterly and unstructured and weeky:
Quarterly:
2004q1 and 2004q2 take value 1, 2004q3 and 2004q4 take 2, 2005q1 and 2005q2 take value 1,....and so on.
another format: 2004q1 take value 1 2004q2 take value 2, 2004q3 take value 1 2004q4 take value take 2, 2005q1 take value 1 and 2005q2 take value 2,....and so on.
Unstructured:
two obs take value 1, followed by two obs take value 0, two obs take value 2, followed by two obs take value 0...and so on.
the first obs take value 1 , the second obs take value 2, followed by two obs take value 0, the fifth obs take value 1, the sixth obs take value 2, followed by two obs take value 0,... and so on.
Weekly:
first week and second week take value 1 for the first month, first week and second week take value 2 for the second month, ...and so on.(otherwise take zero)
first week take value 1 second week take value 2 for the first month, first week take value 1 second week take value 2 for the second month,..and so on.(otherwise take zero)
Can i asking another questions? just by curiosity.
Are there another way for:
EViews Gareth wrote:Here's the first way that springs to mind:Code: Select all
series mytrend = @recode(@recode(@month<@month(1),1,0)=0,0,@cumsum(@recode(@month<@month(1),1,0)))
I need that for quarterly and unstructured and weeky:
Quarterly:
2004q1 and 2004q2 take value 1, 2004q3 and 2004q4 take 2, 2005q1 and 2005q2 take value 1,....and so on.
another format: 2004q1 take value 1 2004q2 take value 2, 2004q3 take value 1 2004q4 take value take 2, 2005q1 take value 1 and 2005q2 take value 2,....and so on.
Unstructured:
two obs take value 1, followed by two obs take value 0, two obs take value 2, followed by two obs take value 0...and so on.
the first obs take value 1 , the second obs take value 2, followed by two obs take value 0, the fifth obs take value 1, the sixth obs take value 2, followed by two obs take value 0,... and so on.
Weekly:
first week and second week take value 1 for the first month, first week and second week take value 2 for the second month, ...and so on.(otherwise take zero)
first week take value 1 second week take value 2 for the first month, first week take value 1 second week take value 2 for the second month,..and so on.(otherwise take zero)
Re: generate trend by sequence
Probably, I asked too many questions. make some many tricks of special cases, would be difficult for me.
Re: generate trend by sequence
After some manipulation from your code, i have solved my two cases for unstructured and quarterly frequency, but for weekly i need your help.
This part of your code generate value one to the last week.
series mytrend = @recode(@recode(@month<@month(1),1,0)=0,0,1)
So, how can generate value one for:
the first week for each month, or just the second week for each month, just the 1st and 2nd week for each month, or the first and last week for each month.
I forgot to thank you, for the first three code.
This part of your code generate value one to the last week.
series mytrend = @recode(@recode(@month<@month(1),1,0)=0,0,1)
So, how can generate value one for:
the first week for each month, or just the second week for each month, just the 1st and 2nd week for each month, or the first and last week for each month.
I forgot to thank you, for the first three code.

 EViews Developer
 Posts: 262
 Joined: Thu Apr 25, 2013 7:48 pm
Re: generate trend by sequence
The expression @datediff(@date, @datefloor(@date, "mm"), "ww") + 1 will generate "weekofthemonth" numbers (15), under the assumption that the first week starts on the 1st of the month. For weekly data, this will probably number things the way you want, i.e., the first observation in a complete month will be 1, the second observation will be 2, etc. You can generate your dummy variables from these, for example,
Identifying the last week in a month is a little trickier, but one way to do it is to check whether the current observation and the current observation plus seven days are in the same month. For example, here are several ways to perform this check,
You can, of course, combine these to produce more complicated dummies.
Code: Select all
' Generate weekofthemonth numbers.
series week = @datediff(@date, @datefloor(@date, "mm"), "ww") + 1
' Dummy for the first week only.
series first_week = week = 1
' Dummy for the second week only.
series second_week = week = 2
' Dummy for the first and third weeks.
series first_third_week = week = 1 or week = 3
Identifying the last week in a month is a little trickier, but one way to do it is to check whether the current observation and the current observation plus seven days are in the same month. For example, here are several ways to perform this check,
Code: Select all
' Dummy for the last week only.
series last_week = @datefloor(@date, "mm") <> @datefloor(@date + 7, "mm")
series last_week2 = @datefloor(@date, "mm") <> @datefloor(@enddate + 1, "mm")
series last_week3 = @enddate + 1 >= @dateadd(@datefloor(@date, "mm"), 1, "mm")
You can, of course, combine these to produce more complicated dummies.
Re: generate trend by sequence
It help me. Thank you (honestly the weekly frequency is very complicated)
Just a last two question:
1°This produce value one, for the first week , this is the only way to do this. (for last week there is three way to do this).
2°I would to make trend four some cases: the code doesn't work
21) 1st week take 1 of 1st month, 1st week take 2 of 2st month,....etc otherwise take zero.
22) 2st week take 1 of 1st month, 2st week take 2 of 2st month,....etc otherwise take zero
23) the two first week take 1 in the 1st month, the two first week take 2 in the 2st month,.....etc otherwise take zero.
24) the first week take value 1 and second week take 2 for the first month, the first week take value 1 and second week take 2 for the 2nd month,...etc otherwise take zero.
25) the 2nd and 3rd week take 1 for the first month, the 2nd and 3rd week take 2 for the second month,.....etc otherwise take zero.
26) the 2nd week take 1 the 3rd week take 2 for the first month, the 2nd week take 1 the 3rd week take 2 for the second month...etc otherwise take zero.
Just a last two question:
1°This produce value one, for the first week , this is the only way to do this. (for last week there is three way to do this).
Code: Select all
series first_week=@date<@datefloor(@date, "mm")+7
2°I would to make trend four some cases: the code doesn't work
Code: Select all
series mytrend = @recode(@date<@datefloor(@date, "mm")+7,0,@cumsum(@recode(@date<@datefloor(@date, "mm")+7,1,0)))
21) 1st week take 1 of 1st month, 1st week take 2 of 2st month,....etc otherwise take zero.
22) 2st week take 1 of 1st month, 2st week take 2 of 2st month,....etc otherwise take zero
23) the two first week take 1 in the 1st month, the two first week take 2 in the 2st month,.....etc otherwise take zero.
24) the first week take value 1 and second week take 2 for the first month, the first week take value 1 and second week take 2 for the 2nd month,...etc otherwise take zero.
25) the 2nd and 3rd week take 1 for the first month, the 2nd and 3rd week take 2 for the second month,.....etc otherwise take zero.
26) the 2nd week take 1 the 3rd week take 2 for the first month, the 2nd week take 1 the 3rd week take 2 for the second month...etc otherwise take zero.
Return to “General Information and Tips and Tricks”
Who is online
Users browsing this forum: No registered users and 3 guests