Page 1 of 1

extract and store into a vector the numeric part of string

Posted: Thu Dec 30, 2010 7:28 am
by Gibus
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

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

Posted: Thu Dec 30, 2010 9:00 am
by EViews Gareth
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

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

Posted: Fri Dec 31, 2010 5:38 am
by Gibus
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