Copying Variables to New Pages by Looping?

For questions regarding programming in the EViews programming language.

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

denverguy
Posts: 2
Joined: Tue Jan 12, 2010 8:23 am

Copying Variables to New Pages by Looping?

Postby denverguy » Tue Jan 12, 2010 8:38 am

Hello all,

I have a question concerning how you would go about creating new pages with new frequencies for a large number of variables.

For example, I create a new page using PAGECREATE and I need to copy monthly to monthly for 40 variables...

I use the command: COPY MONTHLY\VARIABLE_X MONTHLY_ECON\

I have been unable to figure out how to loop over all of my variables and instead have 40 lines of copy commands that seem unneccesary.

Any help would be appreciated.

Best

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Jan 12, 2010 9:01 am

The copy command takes wildcards, so you can use

Code: Select all

copy monthly\* monthly_econ\
to copy everything from one page to another.

denverguy
Posts: 2
Joined: Tue Jan 12, 2010 8:23 am

Re: Copying Variables to New Pages by Looping?

Postby denverguy » Tue Jan 12, 2010 9:08 am

Thx!

What if out of the 40 or so variables I need to copy only sub-groups of those 40? My first page has all of my equations and model solutions and then all the subsequent pages are different sub-groups and or frequencies.

Sorry for not being clear in my 1st post.

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Jan 12, 2010 9:46 am

Assuming there isn't some sort of naming convention that would let you use wildcards, you could put the 40 you're interested in into a group, then loop through the members of the group:

Code: Select all

for !i=1 to g.@count %seriesname = g.@seriesname(!i) copy monthly\{%seriesname} monthly_econ\ next
Where g is the name of your group.

khazeit
Posts: 12
Joined: Mon Nov 29, 2010 6:46 am

Re: Copying Variables to New Pages by Looping?

Postby khazeit » Tue Dec 21, 2010 8:14 am

hi Gareth,

Is there a way to loop the variables into a group and then do the copying to the new page of the same workfile? I tried to but it always links the last variable in the for loop.

for instance:

for %n var1 var2 var3
genr {%n}yy = @pcy(x{%n})
genr {%n}mm = @pc(x{%n})
group yycalcs {%n}yy
group mmcalcs {%n}mm
next

for !i=1 to yycalcs.@count
%seriesname = yycalcs.@seriesname(!i)
copy raw\{%seriesname} calcs\
next

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Dec 21, 2010 9:01 am

You're not adding all the variables to the group.

Declare the group outside of your first loop, then add them to the group.

Something like:

Code: Select all

group yycalcs group mmcalcs for %n var var2 var3 genr {%n}yy = @pcy(x{%n}) genr {%n}mm = @pc(x{%n}) yycalcs.add {%n}yy mmcalcs {%n}mm next

khazeit
Posts: 12
Joined: Mon Nov 29, 2010 6:46 am

Re: Copying Variables to New Pages by Looping?

Postby khazeit » Tue Dec 21, 2010 10:27 am

thanks very much Gareth, that worked.

Another follow up question but slightly different end game.

Is there a way I can loop the creation of graphs/charts? For instance, say I have 30 variables and want to plot 10 charts with 3 panels each.

Also, I'd like to plot each panel with some other measure I have calculated - for example a y/y rate and a moving avg of that rate. I've done this in the past but not with eviews...is this possible?

this is mainly to avoid repeating several lines of code with graphs.

thanks

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Dec 21, 2010 10:46 am

Of course. Just put the line of code you're using to generate the graphs inside the loop. Something like:

Code: Select all

for %i gdp inf unemp freeze({%i}_gr) {%i}.line

khazeit
Posts: 12
Joined: Mon Nov 29, 2010 6:46 am

Re: Copying Variables to New Pages by Looping?

Postby khazeit » Tue Dec 21, 2010 11:41 am

Much appreciated Gareth.

If you don't mind, I'd like to take it a step further.

Instead of writing out the variables (b/c I have 50 or so), I have created two groups with pertain to different kind of data manipulate. I'd like to take a series in yycalcs (say var1yy) and plot it with its counterpart in mmcalcs(say var1mm) so that the graph plot them on the same panel. Further, I'd like to specify Eviews to create a chart several times for these charts only to merge them in say a 3 panel chart. I thought of creating a nested for loop but I couldn't find a syntax/specify to graph series1 and series2 so alternatively I put "and" in my for loop. Not sure if this is correct.

for !i=1 to yycalcs.@count and mmcalcs.@count
%seriesname1 = yycalcs.@seriesname(!i)
%seriesname2 = mmcalcs.@seriesname(!i)
freeze({%seriesname1}_gr) {%seriesname1}.line
freeze({%seriesname2}_gr) {%seriesname2}.line
' {%seriesname1}_gr.draw(line,left,top) 0
next

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Dec 21, 2010 11:47 am

Not sure I follow exactly what you're doing. But...

You need to loop inside a loop. You also need to put the two series into a temp group so you can have them on the same chart.

Code: Select all

for !i=1 to yycalcs.@count for !j=1 to mmcalcs.@count %seriesname1 = yycalcs.@seriesname(!i) %seriesname2 = mmcalcs.@seriesname(!j) group temp {%seriesname1} {%seriesname2} freeze({%seriesname1}_{%seriesname2}_gr) temp.line d temp next next
As for merging them into one big graph, you'll need to use the merge command (look it up in the graph section of the object reference).

khazeit
Posts: 12
Joined: Mon Nov 29, 2010 6:46 am

Re: Copying Variables to New Pages by Looping?

Postby khazeit » Tue Dec 21, 2010 11:59 am

Thanks very much...oddly I wrote code similar to that but didn't know how to link series1 and series2 with underscore (I used comma :P).

That pretty much solves my problem and thanks again for the help. One more thing, it pretty much creates every graph combination - for ex. var1yy_var1mm, then var1yy_var2mm, etc., which creates a couple hundred charts...I just want to match var1yy and var1mm, var2yy and var2mm only. Can I specify that?

Thanks again, I think I'm done bugging you about the same issue :).

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Dec 21, 2010 12:04 pm

ah, well if there's the same number of items in each group and you only want to match like to like, then you don't need a double loop at all, and your first loop was almost right.

Code: Select all

for !i=1 to mmcalcs.@count %seriesname1 = yycalcs.@seriesname(!i) %seriesname2 = mmcalcs.@seriesname(!i) group temp {%seriesname1} {%seriesname2} freeze({%seriesname1}_{%seriesname2}_gr) temp.line d temp next

khazeit
Posts: 12
Joined: Mon Nov 29, 2010 6:46 am

Re: Copying Variables to New Pages by Looping?

Postby khazeit » Tue Dec 21, 2010 12:37 pm

really appreciate your help.

just curious as to why the for loop only has mmcalcs as opposed to an "and" statement for both groups (seems kind of odd to me)? (i.e. for !i=1 to mmcalcs.@count)

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

Re: Copying Variables to New Pages by Looping?

Postby EViews Gareth » Tue Dec 21, 2010 12:40 pm

Because you have the same number of items in each group, you only want !i to run from 1 to the number of items. Doesn't matter whether you put mmcalcs.@count or yycalcs.@count, since they're the same.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests