Sum by Iterating through Variables by Name
Posted: Tue Mar 05, 2024 9:05 am
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