generate trend by sequence

For requesting general information about EViews, sharing your own tips and tricks, and information on EViews training or guides.

Moderators: EViews Gareth, EViews Moderator

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

generate trend by sequence

Postby mmc23 » Sun May 07, 2017 5:35 am

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).

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: generate trend by sequence

Postby EViews Gareth » Sun May 07, 2017 6:41 am

1) How are you defining the last week of a month?
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

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Sun May 07, 2017 10:37 am

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

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: generate trend by sequence

Postby EViews Gareth » Sun May 07, 2017 10:42 am

That doesn't really answer the question.

You need to define how to decide which month a week is in.
Follow us on Twitter @IHSEViews

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Sun May 07, 2017 10:50 am

The day of the week in my workfile structured on Friday.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: generate trend by sequence

Postby EViews Gareth » Sun May 07, 2017 11:27 am

What does that have to do with months?
Follow us on Twitter @IHSEViews

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Sun May 07, 2017 11:35 am

Weeks and months do not align. day at the beginning of next week "a Friday" is within the next month.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: generate trend by sequence

Postby EViews Gareth » Sun May 07, 2017 11:46 am

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?
Follow us on Twitter @IHSEViews

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Sun May 07, 2017 11:56 am

Sometimes I do not find the words to express myself well.
Yep!.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: generate trend by sequence

Postby EViews Gareth » Sun May 07, 2017 2:41 pm

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

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Mon May 08, 2017 5:04 am

Good.
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)

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Tue May 09, 2017 10:10 am

Probably, I asked too many questions. make some many tricks of special cases, would be difficult for me. :oops:

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Tue May 09, 2017 2:52 pm

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. :)

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: generate trend by sequence

Postby EViews Matt » Tue May 09, 2017 4:08 pm

The expression @datediff(@date, @datefloor(@date, "mm"), "ww") + 1 will generate "week-of-the-month" numbers (1-5), 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,

Code: Select all

' Generate week-of-the-month 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.

mmc23
Posts: 21
Joined: Sat Jan 11, 2014 9:44 am

Re: generate trend by sequence

Postby mmc23 » Wed May 10, 2017 1:21 am

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).

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)))

2-1) 1st week take 1 of 1st month, 1st week take 2 of 2st month,....etc otherwise take zero.
2-2) 2st week take 1 of 1st month, 2st week take 2 of 2st month,....etc otherwise take zero
2-3) the two first week take 1 in the 1st month, the two first week take 2 in the 2st month,.....etc otherwise take zero.
2-4) 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.
2-5) 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.
2-6) 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 4 guests