Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: extract rows from panel dataset


From   "Tom Boonen" <[email protected]>
To   [email protected]
Subject   st: extract rows from panel dataset
Date   Sun, 10 Sep 2006 10:24:37 -0400

Dear Statalisters,

please excuse if this question is a bit wordy, but I have tried to be
as precise as possible to state my problem. I am writing a program
where I need to extract rows from a panel dataset, according to which
time periods and units the user specifies. Needless to say, any help
on this would be highly appreciated (I am new to Stata programming).

Here is my problem. The user has to tsset his data with panel and time
var. Then my program starts:

program dataprep , rclass
 version 9.2
 preserve

/* check if data is tsset with panel and time var and get these into locals */
/* (probably could be done more elegantly) */
 qui tsset
   if "`r(panelvar)'" == "" {
   di as err "panel variable missing please use -tsset panelvar timevar"
   exit 198
   }
   else {
   local tvar `r(timevar)'
   local pvar "`r(panelvar)'"
   }

qui sort `pvar' `tvar'

/* Now the user enters a varlist and two numlists of integers for the
time periods and units he wants to use */
/* the numbers in these lists refer to the panel and time variable
used in tsset */

 syntax varlist(ts min=2 numeric) , timex(numlist >=0 integer)
units(numlist integer)
  tempvar YX
  qui mkmat `varlist', matrix(`YX')
.
.
end

Now my question: How can I extract from the varlist (i.e. the matrix
YX) only those rows that the user specified in timex() and units().

So for example:

clear
use   http://www.stata-press.com/data/r9/invest2.dta, clear
tsset company time

A panel dataset with 5 companies (1,...,5) and 20 time periods
(1,...,20). The user wants to use varibales invest, market and stock,
and wants to run the analysis using companies number 1, 2, and 4. And
time periods 3, 6, 7 and 9. Thus, she runs:

dataprep invest market stock, timex(3 6 7 9) units(1 2 4)

This will give me the three colum YX matrix

        invest     market      stock
 r1  317.60001     3078.5        2.8
 r2  391.79999  4661.7002  52.599998
 r3  410.60001  5387.1001  156.89999
 r4  257.70001     2792.2      209.2
 r5  330.79999  4313.2002  203.39999
 r6  461.20001  4643.8999      207.2
 r7        512  4551.2002      255.2
 r8        448  3244.1001  303.70001
 r9  499.60001     4053.7  264.10001

But now I want to extract from this matrix only the rows that
correspond to panelvar equal to 2,6,7, or 9 and time var equal to 1,2,
or 4. How can I do this?

NOTE: I cannot use the [if] or [in] statements to reduce the rows,
because in my full program the user needs to specify various timex,
timey, ... timez, and units and the YX matrix has to sliced in various
different ways.

Thank you very much!

Best,
Tom
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index