extract and store into a vector the numeric part of string

For questions regarding programming in the EViews programming language.

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

Gibus
Posts: 9
Joined: Tue Dec 07, 2010 2:57 am

extract and store into a vector the numeric part of string

Postby Gibus » Thu Dec 30, 2010 7:28 am

I am Eviews7 user.

I have a group containing series names:
RES
RES1
X1
X4
X11
X100


I would like to create a (numeric) vector containing only the numeric part i.e
NA
1
1
4
11
100

When using string vector with bellow code,

scalar lmk = group3.@count
svector (lmk) varnames
for !k =1 to group3.@count
%xname=group3%2E%40seriesname%28%21k%29
%kname=%40mid%28%25xname%2C%40instr%28%25xname%2C%26quot%3BX%26quot%3B%29%2B1%29
varnames(!k) = %kname
next

I got:
RES
RES1
1
4
11
100

that can not be store as numeric vector. How can I get a numeric vector e.g.
NA
1
1
4
11
100

thanks

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

Re: extract and store into a vector the numeric part of string

Postby EViews Gareth » Thu Dec 30, 2010 9:00 am

Can't think of an easy way to do it, but something like this will work:

Code: Select all

group g res res1 x1 x4 x11 x100 vector(g.@count) names for !i=1 to g.@count %name = g.@seriesname(!i) %out = "" call getnumber( %out, %name) names(!i) = @val(%out) next subroutine getnumber(string %out, string %in) %out = "" for !c = 1 to @len(%in) %char = @mid(%in,!c,1) !val = @val(%char) if !val<>na then %out = %out + @str(!val) endif next endsub

Gibus
Posts: 9
Joined: Tue Dec 07, 2010 2:57 am

Re: extract and store into a vector the numeric part of string

Postby Gibus » Fri Dec 31, 2010 5:38 am

that worked fine. Thanks a lot

'create a workfile
wfcreate q 1990 2010

'create a group which will contain the xs
group group3

'create 6 series
for %i res res1 x1 x4 x11 x100 X399 Re3
series {%i}=nrnd
group3.add {%i}
next

scalar lmk = group3.@count
vector(lmk) varnames

for !k=1 to lmk
%xname=group3%252E%2540seriesname%2528%2521k%2529
%out = ""
call getnumber( %out, %xname)
varnames(!k) = @val(%out)
next

subroutine getnumber(string %out, string %in)
%out = ""
for !c = 1 to @len(%in)
%char = @mid(%in,!c,1)
!val = @val(%char)
if !val<>na then
%out = %out + @str(!val)
endif
next

endsub


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests