Subtraction of panel data with loop

For making suggestions and/or requests for new features you'd like added to EViews.

Moderators: EViews Gareth, EViews Moderator

Dove05
Posts: 4
Joined: Fri Nov 29, 2019 1:39 am

Subtraction of panel data with loop

Postby Dove05 » Tue Dec 17, 2019 7:50 am

Hi,
I have a data panel dim (4,3).
This is the code I use to subtract obsid=2- obsid=3 in crossid=4 for January:

smpl @all if @crossid=4 and @obsid=2
series result=@maxsby(JAN,@crossid=4, @obsid=2) - @maxsby(JAN,@crossid=4,"if @crossid=4 and @obsid=3")

I would like to perform the same subtraction for all @crossid with a loop.
This is the code I am working on:
for !i=1 to 4 'loop over the cross-sections
smpl @all if @crossid=!i and @obsid=2
series loop=@maxsby(JAN,!i, @obsid=2) - @maxsby(JAN,!i, "if @crossid=!i and @obsid=3") 'jan stands for january
next

It returns me the following error:
Error in Sample: !I is not defined in "SERIES
LOOP=@MAXSBY(JAN,1, @OBSID=2) - @MAXSBY(JAN,1, "IF
@CROSSID=!I AND @OBSID=3")".

How should I change the code that it would work properly?
I use EViews version 9.5.

Thank you!

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

Re: Subtraction of panel data with loop

Postby EViews Gareth » Tue Dec 17, 2019 8:14 am

Without understanding why you're using @maxsby, it looks like you've missed off the @crossid part?

Code: Select all

@maxsby(JAN,!i, @obsid=2)

Code: Select all

@maxsby(JAN,@crossid=!i, @obsid=2)
Follow us on Twitter @IHSEViews

Dove05
Posts: 4
Joined: Fri Nov 29, 2019 1:39 am

Re: Subtraction of panel data with loop

Postby Dove05 » Tue Dec 17, 2019 8:25 am

Thank you Gareth for the prompt reply.
Although I corrected the code as recommended:

for !i=1 to 4 'loop over the cross-sections
smpl @all if @crossid=!i and @obsid=2
series loop=@maxsby(JAN, @crossid=!i, @obsid=2) - @maxsby(JAN,@crossid=!i, "if @crossid=!i and @obsid=3") 'jan stands for january
next

I still get the error message:
Error in Sample: !I is not defined in "SERIES
LOOP=@MAXSBY(JAN, @CROSSID=1, @OBSID=2) -
@MAXSBY(JAN,@CROSSID=1, "IF @CROSSID=!I AND
@OBSID=3")".

Thank you!

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

Re: Subtraction of panel data with loop

Postby EViews Gareth » Tue Dec 17, 2019 8:33 am

Code: Select all

%spec = "if @crossid=" + @str(!i) + " and @obsid=3"
series loop=@maxsby(JAN, @crossid=!i, @obsid=2) - @maxsby(JAN,@crossid=!i, %spec)
Follow us on Twitter @IHSEViews

Dove05
Posts: 4
Joined: Fri Nov 29, 2019 1:39 am

Re: Subtraction of panel data with loop

Postby Dove05 » Wed Dec 18, 2019 1:42 am

The modified code
[for !i=1 to 4 'loop over the cross-sections
%spec = "if @crossid=" + @str(!i) + " and @obsid=3"
smpl @all if @crossid=!i and @obsid=2
series loop=@maxsby(JAN, @crossid=!i, @obsid=2) - @maxsby(JAN,@crossid=!i, %spec) 'jan stands for january
next]
returns only the result for the first crossid, while for the 2nd to 4th crossid it returns NA.

Dove05
Posts: 4
Joined: Fri Nov 29, 2019 1:39 am

Re: Subtraction of panel data with loop

Postby Dove05 » Thu Dec 19, 2019 6:30 am

Gareth,
just a note that the modified code you proposed works!
Thank you so much for your help!


Return to “Suggestions and Requests”

Who is online

Users browsing this forum: No registered users and 1 guest