Page 1 of 1
looping through an alpha series
Posted: Wed Feb 03, 2021 3:33 pm
by amrsherif
Hi,
This seems very basic, but i am unabnle to loop through the elements of an alpha series.
I have an alpha series with names of series that i want to fetch.
How can i loop through them so i can add them to a string?
See attached photo for an example of what the series looks like
I have tried:
1) for %series in alpha_series_name
2) for !i=1 to @ilast(alpha_series_name)
3) i've also converted it into an svector but could still not access the elements in that
Thanks
Re: looping through an alpha series
Posted: Wed Feb 03, 2021 4:55 pm
by EViews Matt
Hello,
If what you're ultimately after is a string containing the data in an alpha series, you can generate that with a single expression,
Code: Select all
%names = @wjoin(@convert(alpha_series_name))
Otherwise, if you actually need to loop through the alpha series to perform other work, you can in a number of ways. For example, looping over observations,
Code: Select all
for !i = @ifirst(alpha_series_name) to @ilast(alpha_series_name)
... alpha_series_name(!i) ...
next
Or by looping over an equivalent svector,
Code: Select all
svector tmp
stom(alpha_series_name, tmp)
for !i = 1 to @rows(tmp)
... tmp(!i) ...
next
Re: looping through an alpha series
Posted: Thu Feb 04, 2021 6:17 am
by amrsherif
This is strange, I have looped through alpha series many times before, but i cannot get this one to work.
I have tried this:
svector tmp
stom(alpha_series_name, tmp)
for !i = 1 to @rows(tmp)
show tmp(!i)
next
No luck
TMP(1) is not defined in show tmp(1)
WHen i tried this:
for !i = @ifirst(alpha_series_name) to @ilast(alpha_series_name)
show alpha_series_name(!i)
next
it loops through the numbers, but it lags the series by the index, instead of giving the ith element
Re: looping through an alpha series
Posted: Thu Feb 04, 2021 6:22 am
by amrsherif
I figured it out.
It works when i convert the ith element into a string or temporary string, then use it
Re: looping through an alpha series
Posted: Thu Feb 04, 2021 8:13 am
by EViews Matt
Right, the show command is predominantly used for displaying entire objects rather than individual elements. As such, ( ) notation is interpreted as lag notation rather than element-of notation. Any normal non-genr expression works as intended, as you discovered.