Bookmark and Share

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


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

Re: st: Panel data - Foreach-Collapse commands


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Panel data - Foreach-Collapse commands
Date   Tue, 14 Jan 2014 20:39:07 +0000

Too many questions (at least for me to want to answer)!  I'll peel off
the first.

Once you -collapse- the first time round your loop, -kstock- has
disappeared from your dataset. That's what -collapse- does: reduce
your dataset to a new one. But the -collapse- is (arguably) illogical.
Moving standard deviations are unique to each observation. A mean
moving SD could be calculated, but it seems unlikely to be what you
want. If it is really what you want, you need to read in the original
data again.  On the other hand, I want whether you really want a
moving SD. The rest of your question seems to imply otherwise.

General comment: Some Statalist members use MS Excel. Many don't in
practice or won't on principle.  It's not a good idea to imply that
members are inclined to try and work out how to read your data into
Stata from Excel.

Nick
[email protected]

On 14 January 2014 20:18, Sadia Khalid [edited]

I am working on panel data. I have to create the Rolling SD of Y
variable which I am able to create with the help of -mvsumm- (SSC).

The command for creating the 3 year moving SD is

mvsumm y, stat(mean) win(3) gen(`var'3avg) end

As I have to do 3 year moving SD for a number of variables I am trying
to use -foreach- and these are the commands in my do file

webuse grunfeld,clear
xtset company year
 *** Code for calculating 3 Year moving SD**

foreach var in   invest kstock {
     mvsumm `var', stat(sd) win(3) gen(`var'3sd) end
     collapse `var' , by(company)
}

I get the following error message.

 “variable kstock not found”

How can I overcome this?

<big snip>

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


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