A loop to group variables

For questions regarding programming in the EViews programming language.

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

ecardamone
Posts: 40
Joined: Wed Sep 17, 2014 12:23 pm

A loop to group variables

Postby ecardamone » Thu Apr 09, 2015 11:50 am

Hi. I am trying to group a whole bunch of variables with similar name structure. Can someone please help me figure out why this code I am trying isn't working? It creates a group titled "granulars" with only one variable "a_usgol_nd". Instead, I would like all of these variable to appear in the same table, a_usgo11_nd, a_usgo15_nd, a_usgo17_nd, etc.

Code: Select all

for %x 11 15 17 18 20 21 22 221 23 24 25 269 27 28 29 30 32 34 35 36 50 51 60 61 62 603 63 64 642 65 66 67 71 72 74 92 a_b c d e f h i j k m l group granulars a_usgo{%x}_nd next
Thanks a lot for help!

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

Re: A loop to group variables

Postby EViews Glenn » Thu Apr 09, 2015 12:44 pm

It's re-creating the group each time. You need to use add

Code: Select all

group granulars for %x 11 15 17 18 20 21 22 221 23 24 25 269 27 28 29 30 32 34 35 36 50 51 60 61 62 603 63 64 642 65 66 67 71 72 74 92 a_b c d e f h i j k m l granulars.add {a_usgo{%x}_nd} next
or build up a string

Code: Select all

%mystring for %x 11 15 17 18 20 21 22 221 23 24 25 269 27 28 29 30 32 34 35 36 50 51 60 61 62 603 63 64 642 65 66 67 71 72 74 92 a_b c d e f h i j k m l %mystring = %mystring + "a_usgo" + %x + "_nd" next group a {%mystring}

ecardamone
Posts: 40
Joined: Wed Sep 17, 2014 12:23 pm

Re: A loop to group variables

Postby ecardamone » Thu Apr 09, 2015 1:13 pm

Thank you! I tried the first method, and it works after I remove the outer curly brackets in

Code: Select all

granulars.add {a_usgo{%x}_nd}
Thanks, again!

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

Re: A loop to group variables

Postby EViews Glenn » Thu Apr 09, 2015 5:00 pm

Sorry about the error. I was away from my computer and doing it from memory.

ecardamone
Posts: 40
Joined: Wed Sep 17, 2014 12:23 pm

Re: A loop to group variables

Postby ecardamone » Fri Apr 10, 2015 8:07 am

Thank you so much! Could you help me figure out the second method - build up a string?

For example, I have two variables, a_usgo92_nd_arts and a_usgo92_nd_ent, and I want to create a group by building up a string. When I try the following, I get an error message "_ENT is not defined in "Group testindustries a_usgo92_nd_ARTS_ENT". What am I doing wrong? I don't have much experience with building strings - why is the code combining _ent and _arts?

Code: Select all

for %x "_arts _ent" %mystring = "a_usgo92_nd" + %x next group testindustries {%mystring}
But if I remove the quotation marks in %x "_arts _ent", then the code creates a group with just a_usgo92_nd_ent.

Thank you!

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

Re: A loop to group variables

Postby EViews Glenn » Fri Apr 10, 2015 8:12 am

Code: Select all

for %x _arts _ent %mystring = %mystring + " a_usgo92_nd" + %x next group testindustries {%mystring}
You have to accumulate the string

ecardamone
Posts: 40
Joined: Wed Sep 17, 2014 12:23 pm

Re: A loop to group variables

Postby ecardamone » Fri Apr 10, 2015 9:35 am

Great. Thank you!


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests