Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Difficulties in variable calculation using panel data

From   "Degas Wright" <>
To   <>
Subject   st: RE: Difficulties in variable calculation using panel data
Date   Sat, 30 Oct 2010 08:41:49 -0400

I ran into similar challenges analyzing my panel data. 

On the gaps issue, you can use -tsfill- it fills in the gaps in the time variable but not the other variables in the dataset. You can review the data and drop the observations missing data(i.e. the weekend dates) and create a new time variable.  This works if you do not need weekend dates to conduct special analysis such as weekend anomalies research.

Once you have dropped the weekend dates, the code will be;

. generate newtimevariable = _n

.tsset newtimevariable
  Time variable: newtimevariable, 1 to _n
            Delta: 1 unit

Now your return calculations should work since Stata will use every observation.

To conduct a cross sectional regression, you will need to use -reg- rather than -xtreg- . Also, you can use the -forvalues- command to loop through your data one observation at a time.

The code will be:

. forvalues test=1/_n{
   2. display "Period="`test'
   3. regress dev return
   4. }

Where _n is the number of observations.

I do not understand which outputs that you want to average over all regressions. But I hope the other information will be helpful.

Degas A. Wright, CFA
Chief Investment Officer
Decatur Capital Management, Inc.
250 East Ponce De Leon Avenue, Suite 325
Decatur, Georgia  30030
Voice: 404.270.9838
-----Original Message-----
From: [] On Behalf Of Léon Bueckins
Sent: Friday, October 29, 2010 7:42 PM
Subject: st: Difficulties in variable calculation using panel data


I hope somebody will be so kind to help me to get my approach "translated" to Stata, as I'm facing some difficulties:

I have a panel dataset and set it this way via 'xtset group_id date, daily'

Hence my data looks like this:

date | price | group_name | group_id 

01-01-2005 | 34,78 | group A | 1
01-01-2005 | 12,56 | group B | 2
01-01-2005 | 17,21 | group C | 3
01-02-2005 | 67,53 | group A | 1
01-02-2005 | 25,90 | group B | 2
01-02-2005 | 44,78 | group C | 3
...                  | ...        | ...           | ...

a) Now I want to calculate the daily return for each group across time using:

gen return = 100 * (log(price) - log(L.price))

When doing this, I face two problems:

I. Weekends are treated as gaps, thus no return for Monday is computed, but Stata should rather use Fridays' value to compute it. May I skip the 'daily' option in the xtset command?

II. The results are wrong when I proof them randomly by hand. I guess Stata mixes up the groups. How can I tell Stata to look at the timeseries for each group separately?

b) Second, I want to compute another variable using the following formula but don't know how to tell it Stata:
dev = 1/N * sum( abs(return_i, t     -     return_m,t) ) 

N = total number of groups within panel (can be substituted by [ `=_N' ], right?)
return_i,t = return of group i at time point t
return_m,t = average return across all groups at time point t

c) finally I try to run a regression on dev

xtreg dev return ...

But in the regression I want Stata to (theoretically) do one regression per t and compute the average values over all regressions, so I need just the cross-sectional and not the timeseries information in the data.

Help is greatly appreciated, so thank you very much for helping me!

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index