Page 1 of 1

Sum by Iterating through Variables by Name

Posted: Tue Mar 05, 2024 9:05 am
by oleviasharbaugh
I’m trying to loop through variables in Eviews to create a sum variable depending on the name of other variables. My variable names follow this pattern: amuse0_area, amuse1_area, amuse2_area, auto0_area, auto1_area and so on. So there will be multiple categories and number combinations. Some categories won’t have amuse9_area for example, but will have amuse12_area or amuse25_area. I need to be able to have the command loop through all the numbers, but just ignore those that don’t exist. I can’t really tailor it to the numbers that currently exist for each category because I will need to run this again and I don’t want to worry about excluding a number that might pop up in the future. What I currently have is below but it isn't working as of right now:

Code: Select all

logmode msg 'Just generating a bunch of empty series for %x amuse auto miscnr pub rel warenm genr {%x}_12v1 = 0 genr {%x}_35v1 = 0 genr {%x}_610v1 = 0 genr {%x}_10plusv1 = 0 next !i = area.@count %name = area.@seriesname(!i) 'Setting up a way to query all the variables in the group called 'area' !k = @obs({%name}) 'Setting up a way to look at the time series of each of the variables we care about for !i = 1 to area.@count 'For all the series in the group named area.... %right = @right(area.@seriesname(!i),6) 'Grabs the last six characters of the series name %stories = @left(%right,1) 'Then grabs the first character of the chunked off version, isolating the number of stories present !stories = @val(%stories) 'Converts that stories number from a scalar to a number for %x amuse auto miscnr pub rel warenm if !stories <=2 then smpl @all {%x}_12v1 = {%x}_12v1 + {%x}{%right} 'Program Breaks here - it doesn't want to iterate through, just grabs the zero stories data (ie amuse0_area) then quits else if !stories <=5 then smpl @all {%x}_35v1 = {%x}_35v1 + {%x}{%right} else if !stories <=10 then smpl @all {%x}_610v1 = {%x}_610v1 + {%x}{%right} else smpl @all {%x}_10plusv1 = {%x}_10plusv1 + {%x}{%right} next next endif endif endif

Re: Sum by Iterating through Variables by Name

Posted: Tue Mar 05, 2024 9:39 am
by EViews Gareth
I'm not sure I follow exactly, but I think the way to go is to create groups using the group wildcard constructor, and then use the @rsum function to generate the sums.

So, for example, if you want to create a sum of all the series who match the pattern "amuseXX_area", you'd have:

Code: Select all

group mygroup amuse*_area series mysum = @rsum(mygroup)
And you could loop through this, looping through both the "area" part and the "amuse" part.

Re: Sum by Iterating through Variables by Name

Posted: Tue Mar 05, 2024 10:12 am
by oleviasharbaugh
I think that could be a good start. I'm trying to group them into buckets, though. I should have specified that, my bad. I need to sum them based on the number, creating 4 buckets: 0-2, 3-5, 6-10, 10+.

Again, some of the categories won't have all of the numbers so whatever numbers they do have need to be taken into account and any missing ones should be ignored. I tried to incorporate your suggestion and it may work, but my code leading up to it doesn't look like its working. I am trying to create a variable that isolates the number for each category (for example, I want to pull the "00", "01", etc from amuse00_area and amuse01_area). I am trying to do this with this code:

Code: Select all

%right = @right(area.@seriesname(!i),7) 'Grabs the last seven characters of the series name %stories = @left(%right,2) 'Then grabs the first two characters of the chunked off version, isolating the number of stories present !stories = @val(%stories) 'Converts that stories number from a scalar to a number
After I did that, I tried to use that stories variable to create the sum for the first bucket that includes the numbers 0-2:

Code: Select all

if !stories <=02 then group amuse12 amuse*_area series amu12 = @rsum(amuse12) endif
However, it's including quite a few more variables in that group than just the ones that include 00, 01, and 02. I'm not sure where the breakdown is. Maybe with the inclusion of a 0 in front of the 2?

Re: Sum by Iterating through Variables by Name

Posted: Tue Mar 05, 2024 3:24 pm
by EViews Gareth
Still not 100% sure I follow, but perhaps create the group with all the amuse*_area series, then loop through the series in that group, extract the numbers, and then create groups for the buckets.
Roughly:

Code: Select all

group mygroup amuse*_area group mygroup12 group mygroup35 for !i mygroup.@count %j = mygroup.@seriesname(!i) %right = @right(%j, 7) !stories = @val(@left(%right,2)) if !stories<3 then mygroup12.add {%j} endif if !stories>2 and !stories<6 then mygroup35.add {%j} endif next
Then use @rsum to do the sums again.

Re: Sum by Iterating through Variables by Name

Posted: Wed Mar 06, 2024 1:25 pm
by oleviasharbaugh
That worked perfectly! Thanks for your help!