# Re: st: AW: For each fund-asset pair I have observations on some periods, and want have them on each possible period...

 From Jeph Herrin To statalist@hsphsun2.harvard.edu Subject Re: st: AW: For each fund-asset pair I have observations on some periods, and want have them on each possible period... Date Fri, 24 Jul 2009 16:31:09 -0400

```

/* create those after the first existing period */
bys fund asset (period) : gen byte gap=period[_n+1]-period
replace gap=60-period if mi(gap)
expand gap
bys fund asset (period) : replace holdings=0 if period==period[_n-1]

/* create those before the first existing period */
bys fund asset (period) : gen first=_n==1
expand period-1 if first
bys fund asset (period) : replace holdings=0 if period==period[_n+1]

/* create a new period variable */
bys fund asset (period) : gen newperiod==_n

?

hth,
Jeph

Stata Chris wrote:
```
```Many thanks, that already gets me much closer.

Unfortunately though a problem remains when I run this:
Since I have very many different assets, filling in all assets in all
periods turned out to kill my computer, i.e. it just got hung up for
half an hour before I exited, and I think that if I had let it
complete the task the resulting dataset would likely have been too
large as well.

Do you have an idea how I could fill in only those observations which
I actually need: Namely for each fund-asset pair I would like to fill
in a period only if the respective pair was observed in the previous
or subsequent period?

Thanks,
Chris

2009/7/24 Martin Weiss <martin.weiss1@gmx.de>:
```
```<>

Here is an example for "Stata Chris" using -fillin-

*************
clear*
set obs 5

// fund id
gen fundid=_n
lab var fundid "Fund ID"

// expand to 4 of time periods
expand 4
bys fundid: gen time=_n

// 3 assets
expand 3
bys fundid time:/*
*/  gen asset=_n

// generate value and holdings
gen quantity=/*
*/ rnormal()
gen value=/*
*/ 100*rnormal()

// inducing missings
drop if runiform()<0.1

/*
recover "full"
dataset with -fillin-
*/

fillin fundid time asset

//whenever filled in:
//really zero
//important to distinguish
//from "real" missings
replace quantity=0 if _fillin==1

//get the stock of holdings
bys fundid asset (time): /*
*/ gen holding=sum(quantity)

compress
list fundid time asset /*
*/ quantity holding, noobs /*
*/ sepby(fundid asset)

//get rid of aux var
drop _fillin
*************

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Stata Chris
Gesendet: Freitag, 24. Juli 2009 17:03
An: statalist@hsphsun2.harvard.edu
Betreff: st: For each fund-asset pair I have observations on some periods,
and want have them on each possible period...

Hi Statalisters,

I have the following challenge:

I have a dataset that gives me for each fund-asset-period triple the
quantity and value of holdings (i.e. how much of asset j did fund i
hold in period t).
>From this, I have for each such triple generated the flow (i.e. how
much did the fund buy or sell of a certain asset) from last period
until now.

The problem is that the original dataset does not list holdings or size
zero.
Consequence: If a fund held say 500 units of an asset and then sold
all of these, the respective fund-asset-period observation for the
next period does not exist, so when I compute the flow I get a missing
observation, although it should say flow=-500.
Analogously, if the fund has bought 500 units of an assets after
previously holdings of zero, I would like to have Stata tell me that
the flow equals +500, but instead it will tell me that the flow is
missing.

So I figured the best thing to do, to be on the safe side, would be to
make sure that for each fund-asset pair I have observations on all
periods, rather than just on those for which holdings were listed.

In upshot: My data are currently in long form, with each observation
being a unique fund-asset-period triple. However, for each fund-asset
pair I have observations on some but not all of up to 60 periods. The
question is: How can I add observations such that for each pair there
is an observation on each of the 60 periods (some of which may only
have missing values, but they need to be listed)?

Chris
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```