We have the very nice stack() function, that helps you turn a list of
series into a single panel series. Unless I'm missing something, I don't
think we have the opposite function, that is a function turning a
panel series into a multi-column object. So, I ended up writing this:
<hansl>
set verbose off
function matrix unstack(series x, series names)
errorif($datatype != 3, "This function needs panel data")
scalar N = $nobs / $pd
set skip_missing off
matrix mat = mshape({x}, $pd, N)
strings lab0 = strvals(names)
strings lab1 = array(N)
ndx = uniq(names) # ordering could be garbled
loop i = 1 .. N
j = ndx[i]
lab1[i] = fixname(lab0[j])
endloop
cnameset(mat, lab1)
return mat
end function
###
### example
###
open grunfeld.gdt
matrix mat = unstack(invest, firmname)
# save as a dataset and reopen it
y0 = sprintf("%d", min(year))
fname = "(a)dotdir/tmp.gdt"
store "@fname" --matrix=mat
open "@fname" --preserve
setobs 1 @y0
</hansl>
My question is: is there a simpler way to accomplish the above? And if
there isn't, would it be worthwhile to add this function to the extra
package?
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------