IF command

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator

soungl
Posts: 7
Joined: Fri Oct 23, 2009 3:13 am

IF command

Postby soungl » Wed Feb 23, 2011 7:39 am

Hi, I have a question about IF command. I’m using EViews 6. I tried the following program with no success.

series a

if x>0 then
series a=1
endif

x is a numerical series as follows:
3
4
-3
10

The program keeps producing a with
NA
NA
NA
NA

when it should produce
1
1
NA
1

What am I doing wrong?

Thank you so much for your advice.

startz
Non-normality and collinearity are NOT problems!
Posts: 3202
Joined: Wed Sep 17, 2008 2:25 pm

Re: IF command

Postby startz » Wed Feb 23, 2011 7:44 am

You want to use smpl if
smpl if x>0
series a=1
smpl @all

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

Re: IF command

Postby EViews Glenn » Wed Feb 23, 2011 10:13 am

Or even easier...

Code: Select all

series a = @recode(x>0, 1, na)

Note that the IF test in the original post was testing whether all of the elements of the series were >0, hence the observed result.

Nau2306
Posts: 74
Joined: Thu Nov 17, 2011 11:51 am

Re: IF command

Postby Nau2306 » Wed Jul 11, 2012 1:55 am

hi

I am using eviews 6 and I am trying a similar thing as described above but in my case, I have two series namely x and y and the condition is that x > y. how can I access each element of x and y? for example if x is a series as follows:
4
6
-1
7

and y is
2
0
6
3

then the condition should be 4>2, 6>0, -1>6 and so on?

this is what I have tried but it says syntax error:

scalar a = 0

for !j=1 to !length-!ssize

a = @recode((x(!j) < y(!j)), a+1, a)

next

a


Please help...

Thanks

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

Re: IF command

Postby EViews Gareth » Wed Jul 11, 2012 7:44 am

What are you trying to do?
Follow us on Twitter @IHSEViews

Nau2306
Posts: 74
Joined: Thu Nov 17, 2011 11:51 am

Re: IF command

Postby Nau2306 » Wed Jul 11, 2012 10:39 am

Well I am trying to count the number of times elements of x is greater than that of y.

startz
Non-normality and collinearity are NOT problems!
Posts: 3202
Joined: Wed Sep 17, 2008 2:25 pm

Re: IF command

Postby startz » Wed Jul 11, 2012 10:45 am

Nau2306 wrote:Well I am trying to count the number of times elements of x is greater than that of y.

Something like

Code: Select all

series s = @sum(x>y)

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

Re: IF command

Postby EViews Glenn » Wed Jul 11, 2012 1:37 pm

To provide some context. The expression "x>y" evaluates to the series of 0, 1 values for whether X is indeed greater than Y. The @sum simply sums over the values of this auto-series. Note that the result needn't be put into a new series; it can go into a scalar if desired.

anhkhoa_lpt
Posts: 22
Joined: Thu Sep 29, 2011 8:28 pm
Location: Vietnam

Re: IF command

Postby anhkhoa_lpt » Mon Aug 04, 2014 3:29 am

I don't know how to fix it. Anyone help me, thanks !

Code: Select all

'create a workfile
wfcreate q 1990 2010

'create a y series
series y=nrnd

'create 15 X series
for !i=1 to 15
   series x{!i}=nrnd
next
genr t=@trend()
smpl if t<40

'if xi (-2) < xi (-4) then creating a series named a1=1  (2 and 4 are the lags of xi.)
for !i=1 to 15
if x{!i}(-2) < x{!i}(-4) then series a{!i}=1
endif
next

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

Re: IF command

Postby EViews Gareth » Mon Aug 04, 2014 4:26 am

Code: Select all

if x{!i}(-2) < x{!i}(-4) then series a{!i}=1


This line doesn't make sense. Not sure what you're trying to do
Follow us on Twitter @IHSEViews

anhkhoa_lpt
Posts: 22
Joined: Thu Sep 29, 2011 8:28 pm
Location: Vietnam

Re: IF command

Postby anhkhoa_lpt » Mon Aug 04, 2014 4:48 am

EViews Gareth wrote:

Code: Select all

if x{!i}(-2) < x{!i}(-4) then series a{!i}=1


This line doesn't make sense. Not sure what you're trying to do

I want to filter series which have value at lag 2 < value at lag 4. If a series 's suitable with this rule, creating a new series (name a) and set the value = 1.

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

Re: IF command

Postby EViews Gareth » Mon Aug 04, 2014 5:13 am

See the rest of the comments in this thread. You can't use an of statement to do that, you need to use a smpl
Follow us on Twitter @IHSEViews

anhkhoa_lpt
Posts: 22
Joined: Thu Sep 29, 2011 8:28 pm
Location: Vietnam

Re: IF command

Postby anhkhoa_lpt » Mon Aug 04, 2014 5:26 am

Okay, thank Mr. Gareth :)


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 11 guests