Filling a matrix with data from panel series

For questions regarding programming in the EViews programming language.

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

Posts: 20
Joined: Fri Jan 30, 2015 6:46 am

Filling a matrix with data from panel series

Postby altaus » Wed Aug 02, 2017 1:30 am


I'm trying to fill a matrix with data from a numeric series in panel format. I am fairly new at working with panel data in this manner so I do not know how to refer to the individual values within the panel series, specially because the dimension of the panel are (reporter, partner, trade_flow, comm_code, year)=(7,8,4,12,6).

The matrix contains reporters in the rows and partners in the columns. The idea is to compute a three years average of trade_value and fill the matrix with the corresponding cross-average for reporter/partner. I use group-by statitstics to compute the three year average (see below) as it needs to be by reporter-partner-tradeflow for all comm_codes in these groups .

series avg_all=@meansby(trade_value,reporter,partner,trade_flow,threeyears) ' three-years average of trade_values

Also, I am not completely sure whether I should be using a panel structure instead of a pool structure.

The other way I think this could be worked out is to move to a lower panel structure, where I only need partner, reporter and trade_flow with the values being the three year average of trade_values, so that I get rid of year and comm_code.

I attach the workfile and code for your inspection. Can you help please?

Thanks a lot in advance,


For !i=1 to m ' no. of reporters

For !j=1 to n ' no. of partners

' Not sure how to proceed from here
if threeyears=2016 then

smpl If reporter="ITA" and partner="DEU" and trade_flow="import" and comm_code=9 'then


trade weights.prg
(1.3 KiB) Downloaded 55 times
(664.14 KiB) Downloaded 54 times

EViews Mirza
Posts: 40
Joined: Sat Apr 22, 2017 8:23 pm

Re: Filling a matrix with data from panel series

Postby EViews Mirza » Wed Aug 02, 2017 11:57 am

I believe this should do it. I've commented the code. Should be clear.

Code: Select all

smpl @all 'set full sample
stom(trade_flow,vtf) 'convert trade flow to vector
svector vtfunique = @uniquevals(vtf) 'get unique values of trade flow
%tfalpha = @wjoin(vtfunique) 'make string of unique trade flows

stom(comm_code, vcc) 'convert comm code to vector
vector vccunique = @uniquevals(vcc) 'get unique values
!rows = @rows(vccunique)
svector(!rows) vccstring 'make svector to hold string values of comm codes

'make string vector of comm codes
for !i = 1 to !rows
   vccstring(!i) = @str(vccunique(!i))
%vccalpha = @wjoin(vccstring) 'make string of unique comm codes

for %i {%vccalpha}
   for %j {%tfalpha}
      smpl if threeyears=2016 and year=2016 and comm_code={%i} and trade_flow=%j 'set the sample of interest
         stom(reporter, vrep) 'get vector of reps
         svector vrepunique = @uniquevals(vrep) 'get unique values of reps
         %repu = @wjoin(vrepunique) 'make string of unique reps
         %repstring = @wjoin(vrep) 'make string of all reps
         'do same things for partners
         stom(partner, vpart)
         svector vpartunique = @uniquevals(vpart)
         %partu = @wjoin(vpartunique)
         %partstring = @wjoin(vpart)
         %reppart = @winterleave(%repstring, %partstring) 'get a mesh of all the combinations of reps and partners in the sampel of interest
         'this is preliminary since reps and partners are all space delimited
         'create a new string of all reps and partner combos, where reps and partners are not space delimited, but the list is space delimited
         %reppart2 = ""
         while !k<@length(%reppart)
            %reppart2 = %reppart2 + " " + @replace(@mid(%reppart, !k, 7), " ", "")
         !rows = @rows(vrepunique) 'get rows of matrix to hold avg
         !cols = @rows(vpartunique) 'get cols of matrix to hold avg
         %jmod = @replace(%j, "-", "") 'due to naming, we have to remove - from re-import and re-export

         matrix(!rows, !cols) mavg_{%jmod}_cc{%i} 'declare matrix to hold averages for eachh comm code and each trade flow status
         stom(avg_all,mavg) 'create a vector to hold all averages of interest
         for %s1 {%repu}
            for %s2 {%partu}
               %s3 = %s1+%s2
               !s4 =    @wfind(%reppart2, %s3) 'search for where reppartner combo occurs in the vector mavg
               if !s4>0 then
                  mavg_{%jmod}_cc{%i}(@wfind(%repu, %s1), @wfind(%partu, %s2)) = mavg(!s4) 'if search is successful, enter the value
                  mavg_{%jmod}_cc{%i}(@wfind(%repu, %s1), @wfind(%partu, %s2)) = NA 'if search is not available, enter NA

Obviously you'll have to figure out which rep/partners are avaialble for each configuration. This, however, should be relatively easy to obtain from the code above.

Posts: 20
Joined: Fri Jan 30, 2015 6:46 am

Re: Filling a matrix with data from panel series

Postby altaus » Wed Aug 09, 2017 3:06 am

Thanks a lot! I'm looking at it know.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 8 guests