vba 转换为面板数据的最佳技术
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39747392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best technique to convert to panel data
提问by elbarto
I have some returns data on 1000+ firms that I want to convert into panel form.
我有一些关于 1000 多家公司的退货数据,我想将其转换为面板形式。
From my understanding, it is neither truly wide nor long form (at least from the examples I've seen).
根据我的理解,它既不是真正的宽也不是长形式(至少从我看到的例子来看)。
I have attached an example of the original data set and what I want it to look like. Is there a way to achieve this? I am intermediate with Excel/VBA, and new to SAS/Stata but can use them and self-teach myself.
我附上了一个原始数据集的例子以及我想要它的样子。有没有办法实现这一目标?我是 Excel/VBA 的中级,并且是 SAS/Stata 的新手,但可以使用它们并自学。
回答by J_Lard
This can be done very easily with proc transpose
in SAS. All you will need to add is a column name for column A. This will be your by
variable so that the following variables will be transposed along each specific date. Other than that just make sure your data is sorted by the date column. The code would look similar to this:
这可以proc transpose
在 SAS 中很容易地完成。您只需要添加 A 列的列名。这将是您的by
变量,以便以下变量将沿每个特定日期转置。除此之外,只需确保您的数据按日期列排序。代码看起来类似于:
proc sort data=have;
by date;
run;
proc transpose data=have out=want; /* you could add a name= or prefix= statement here to rename your variables */
by date;
run;
回答by ander2ed
Consider this example using reshape
in Stata:
考虑reshape
在 Stata 中使用的这个例子:
clear *
input float(date FIRM_A FIRM_B FIRM_C FIRM_D)
1 .14304407 .8583148 .3699433 .7310092
2 .34405795 .9531917 .6376472 .2895169
3 .04766626 .6588161 .6988417 .5564945
4 .21615694 .18380463 .4781089 .3058527
5 .709911 .85116 .14080866 .10687433
6 .3805699 .070911616 .55129284 .8039169
7 .1680727 .7267236 .1779183 .51454383
8 .3610604 .1578059 .15383714 .9001798
9 .7081585 .9755411 .28951603 .20034006
10 .27780765 .8351805 .04982195 .3929535
end
reshape long FIRM_, i(date) j(Firm_ID) string
rename FIRM_ return
replace Firm_ID = "Firm " + Firm_ID
list in 1/8, sepby(date)
+---------------------------+
| date Firm_ID return |
|---------------------------|
1. | 1 Firm A .1430441 |
2. | 1 Firm B .8583148 |
3. | 1 Firm C .3699433 |
4. | 1 Firm D .7310092 |
|---------------------------|
5. | 2 Firm A .3440579 |
6. | 2 Firm B .9531917 |
7. | 2 Firm C .6376472 |
8. | 2 Firm D .2895169 |
+---------------------------+
see help reshape
for more on the topic.
help reshape
有关该主题的更多信息,请参见。