Illogical syntax?

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

mr.interested
Posts: 16
Joined: Wed Jul 14, 2010 5:57 am

Illogical syntax?

Postby mr.interested » Fri Jul 30, 2010 9:41 am

Hello there,

I'm trying to understand the syntax of EViews (7), but sometimes it just doesn't make sense. Two problems.

1. First:

Code: Select all

for !industry=1 to 10 !no_{!industry} = 0 for !firm=1 to 100 smpl 1/1/2010 1/1/2010 if d{!firm} = {!industry} !no_{!industry} = !no_{!industry} + 1 smpl @all next next
Let's say I have returns for 100 companies (!firm). Each of the company is categorized into a symbol from 1 to 10 (!industry). Since the first column in all 100 series is empty, I used that space to have the industry code (from 1 to 10).

I expected from the above code that it would create 10 variables (!no_1, !no_2, etc.), and at the end of the main loop, each of those variable will contain a number of firms having the specific industry code. But that doesn't work, and at the end of the loop, all of those variables have value of 100 (equal to a number of firms). That doesn't make sense, as there's a FOR statement, which increases a particular variable only if the first row fulfills the specific condition. For example, the statement

Code: Select all

smpl 1/1/2010 1/1/2010 if d{!firm} = 1 !no_1 = !no_1 + 1 smpl @all
should increase the variable !no_1 only if the first row is equal to 1. If found the solution by adding the following code (marked by the exclamation marks), which I don't really like (it makes the code messy), but I'm more interested why the first code doesn't work? The FOR statement is correct, since it works for a series, but it doesn't work for a variable. Why?

Code: Select all

for !industry=1 to 10 !! smpl 1/1/2010 1/1/2010 !! series no_{!industry} !! smpl @all for !firm=1 to 100 smpl 1/1/2010 1/1/2010 if d{!firm} = {!industry} !! series no_{!industry} = no_{!industry} + 1 smpl @all next !! !no_{!industry} = @mean(no_{!industry}) !! delete no_{!industry} next
2. Second

Now I want to reorganize the series by the industry code. For example, if firm 21, 72 and 98 have the same code (say 1), I want to create a new series: industry_1_1, industry_1_2, industry_1_3 containing values of d21, d72, and d98, respectively. So I add the following code (again, the new code is marked by the exclamation marks):

Code: Select all

for !industry=1 to 10 smpl 1/1/2010 1/1/2010 series no_{!industry} smpl @all for !firm=1 to 100 smpl 1/1/2010 1/1/2010 if d{!firm} = {!industry} series no_{!industry} = no_{!industry} + 1 smpl @all next !no_{industry} = @mean(no_{!industry}) delete no_{!industry} !! !count = 1 !! for !firm=1 to 100 !! smpl 1/1/2010 1/1/2010 if d{!firm} = {!industry} !! series industry_{!industry}_{!count} = d{!firm} !! !count = !count + 1 !! smpl @all !! next next
My logic was following. In the main loop (i.e. from 1 to 10), a new variable is created with the value of 1. Then, if, for example, the first row in d54 is equal to 1, then a new series is creates with a name of industry_1_1. The variable is then updates and takes value of 2. So, if then the first row in d79 equals 1, another series is created: industry_1_2, and again the variable is updated and takes a value of 3. When all firms with the first value of 1 are gone, the main loop looks then at the firms having the first row of 2. Before than, the variable (!count) is updated to have a value of 1 again.

The result of the program is that it creates series: industry_1_1 to industry_1_100, industry_2_1 to industry_2_100, industry_3_1 to industry_3_100, etc. The series industry_1_1, industry_2_1, industry_3_1, etc. are the same.

Why does it not work?

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

Re: Illogical syntax?

Postby EViews Gareth » Fri Jul 30, 2010 10:03 am

Trying to understand the first bit has made my head hurt, so I'm only going to answer that one for now. Once my head is better I'll see if I can follow the second bit.

I think you're confused over what the smpl statement does. You appear to be using it as an if statement, which is not really what it is. More generally, I'd say you're confused over workfile space and programming space.

Series and Group objects are in workfile space. That is, they are of a length defined by the size of the workfile, and each row/observation of the series/group is identified by the structure used in that workfile. If you want to work with a subset of the rows/observations, you can use the smpl statement to select which rows you want. This can include an if statement, whereby only rows that satisfy the if statement are selected. Note that the smpl command is only used to select which rows you want, not which columns (of a group). Also note that the smpl command will only select which rows of the series/groups you want to work with - it can not be used as a logical test.


Program variables (i.e. those beginning with a ! or a %) are not defined inside workfiles. They are defined within the program itself. They are single, scalar, variables that take a single, scalar, value. Thus they do not have rows, nor do they have a length. Thus it is nonsensical to use the smpl statement to try to impose a condition on them. If you want to test whether a program variable takes a certain value, you can use an if condition by itself (i.e. not along side the smpl).

mr.interested
Posts: 16
Joined: Wed Jul 14, 2010 5:57 am

Re: Illogical syntax?

Postby mr.interested » Fri Jul 30, 2010 10:36 am

Thank you for your reply.

I do understand that the program variables don't have rows, etc. In the below statement:

Code: Select all

!no_45 = 0 smpl 1/1/2010 1/1/2010 if d45 = 1 !no_45 = !no_45 + 1 smpl @all
I specify the variable !no_45 equal to 0. Then, I select the first row in the workfile by smpl statement. Then, the IF statement looks whether the first row in the series d45 is equal to 1. Should it be the case, it will take the variable !no_45 and add 1 into it. What am I doing wrong here?

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

Re: Illogical syntax?

Postby EViews Gareth » Fri Jul 30, 2010 10:46 am

You're mixing up workfile space and program space again.

The line

Code: Select all

if d45=1
Just tests whether the entire series d45 is equal to one or not. Because the if statement is program space based, it does not know what the current workfile sample is, so tests the entire series, not just the current sample observations.


If I wanted to do what you're doing in that code, one way to do it would be:

Code: Select all

!no_45 = 0 smpl 1/1/2010 1/1/2010 if d45 = 1 'set the sample to be only observations for which the date is 1/1/2010, and for which the series d45 is equal to 1 !numobs = @obssmpl 'check how many observations are in the current sample (i.e. how many observations match the above criteria) if !numobs > 0 then !no_45 = !no_45+1 endif smpl @all

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Illogical syntax?

Postby EViews Glenn » Fri Jul 30, 2010 2:24 pm

If I may make a meta comment here.

I haven't read the details of the loop restructuring that we are debating since they are rather complex for my limited capabilities. It seems to me, however, that the kinds of things that the user wants to do might better be done by stacking the data into a firm-panel and using the panel oriented tools such as @obsby, extract to new workfile, and other such things.

I'm not entirely clear on what exactly the operations we are trying to do are since, as Gareth indicates, the examples seem to be mixing variable and observation space, but I'm sure that there are ways to do the operations themselves with limited looping. My advice is that loops should be used as a last resort, particularly if you are looping over the observations in a workfile, as there is most likely a superior way of doing what you want to do.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest