Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

For questions regarding programming in the EViews programming language.

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

mamo
Posts: 173
Joined: Wed Nov 07, 2012 9:11 am

Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby mamo » Mon Sep 21, 2020 3:28 am

Dear Eviewers ,

I use Eviews 11, Sept 2019 built, 64bit.

Using the workfile created by the example code below, what is the easiest approach to create a valuemap and apply it to the values of the variable "crossid" in the page "data", by using the labels in "label" corresponding to the codes in "code" as defined in the page "valuelables"?

One approach could be to group "label" and "code", freeze the group as a table and create a valuemap by for-looping through the rows of the table as implemented in the code. I am wondering, however, whether there is a more straightforward approach.

mamo


Code: Select all

wfcreate(page=data) a 2000 2010 3
pagestruct(none)
crossid=crossid^2
pagestruct crossid @date(dateid)
pagecreate(page=valuelabels) u 3
series code=@obsid^2
alpha label="label_"+@str(code)
' Here follows a cumbersome approach to create and assign the valuemap
' Can this be simplified ?
freeze(mode=overwrite, tab) code label
valmap vmap
!nr=tab.@rows
for !r=3 to !nr
   !value=@val(tab(!r,2))
   %label=tab(!r,3)
   vmap.append !value %label
next
pageselect data
copy valuelabels\vmap vmap
crossid.map vmap


EViews Matt
EViews Developer
Posts: 496
Joined: Thu Apr 25, 2013 7:48 pm

Re: Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby EViews Matt » Tue Sep 22, 2020 9:19 am

Hello,

While you do need a loop to build the valuemap programmatically, you can simplify much of the intermediate work. You can even leave the crossids unmodified, as shown below.

Code: Select all

wfcreate(page=data) a 2000 2010 3
vector tmp = @uniquevals(crossid)
valmap vmap
for !i = 1 to tmp.@rows
   !value = tmp(!i)
   %label = "label_" + @str(tmp(!i)^2)
   vmap.append !value %label
next
crossid.map vmap
delete tmp

mamo
Posts: 173
Joined: Wed Nov 07, 2012 9:11 am

Re: Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby mamo » Tue Sep 22, 2020 11:30 pm

Hi,

many thanks for the quick reply

The simplifications you are suggesting are obvious with respect to the structure of the concrete dummy data set the example code generates.

However, the question was not meant to relate to the specific data structure of this dummy data but rather to the more general case: there is one page with data (data page), and separate page (labels page) which contains an alpha series carrying the value labels, and another series (or alpha) containing the respective values to be labeled.

I was wondering what would be the best way to map the values of a series (or alpha) in the data page into the corresponding labels as implied by the labels page, looking for a more economical one than generating a table and looping over it.

Best, mamo

EViews Matt
EViews Developer
Posts: 496
Joined: Thu Apr 25, 2013 7:48 pm

Re: Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby EViews Matt » Wed Sep 23, 2020 1:18 pm

In which case, you can exploit an undocumented feature of the valuemap object's append procedure that lets it process multiple pairs in a single statement. Updating your original example,

Code: Select all

wfcreate(page=data) a 2000 2010 3
pagestruct(none)
crossid=crossid^2
pagestruct crossid @date(dateid)
pagecreate(page=valuelabels) u 3
series code=@obsid^2
alpha label="label_"+@str(code)

valmap vmap
alpha tmp = @str(code)
%tmp = @winterleave(@wjoin(@convert(tmp)), @wjoin(@convert(label)))
vmap.append {%tmp}

pageselect data
copy valuelabels\vmap vmap
crossid.map vmap

mamo
Posts: 173
Joined: Wed Nov 07, 2012 9:11 am

Re: Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby mamo » Thu Sep 24, 2020 12:17 am

Great, many thanks!
So kindly document this nice and usefuly feature of the valuemap object explicitly in your next update of the documentation.
Best, mamo

mamo
Posts: 173
Joined: Wed Nov 07, 2012 9:11 am

Re: Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby mamo » Thu Sep 24, 2020 12:41 am

PS: this exploits another apparently undocumented feature, namely, one of the function of @convert(x).
The available documentation requires the argument x to be a numeric series - however, @convert obviously works with an alpha series as well in which case it yields an svector. It would be nice to if this feature can be documented as well.

Funny, though, is the following inconsistency:
This works:

Code: Select all

vector myvec=@convert(@sqrt(code))

The following works as well:

Code: Select all

alpha temp=@str(code)
svector mysvec=@convert(temp)

But the following code doesn't, yielding an ERROR message that @convert requires a numeric argument!

Code: Select all

svector mysvec=@convert(@sqrt(code)) 

This is inconsistent, since both @sqrt(code) and @str(code) are just functions of the series code, generating a series or an alpha series, respectively.

Best mamo

AlyssaMckenzie
Posts: 1
Joined: Sun Apr 18, 2021 8:32 am

Re: Creating a valuemap from labels and numeric codes defined by an alpha and a series in another page

Postby AlyssaMckenzie » Fri Jun 11, 2021 10:21 am

[quote="EViefws Matt"]Hello,

While you do need a loop to build the valuemap programmatically, you can simplify much of the intermediate work. You can even leave the crossids unmodified, as shown below.

Code: Select all

wfcreeate(page=data) a 20030 2010 3
vector tmp = @uniquevals(crossid)
valmap vmap
for !i = 1 to trmp.@rows
   !value = tmp(!i)
   %label = "label_" + @str(tmp(!i)^2)
   vmap.append !value %labelSee option mask() for the different formats. Stata commands that display the value labels also show the associated numeric values. Prefixes are removed with​ .
next
crossid.map vmap
delete tmp
[/queote]See option mask() for the different formats. Stata commands that display the value labels also show the associated numeric values. Prefixes are removed with​ .


Return to “Programming”

Who is online

Users browsing this forum: Google [Bot] and 17 guests