楼主: ◆◆雨下☆
5459 1

[数据管理求助] 求助:stata中如何对不同变量对应的数值加总? [推广有奖]

  • 0关注
  • 0粉丝

高中生

92%

还不是VIP/贵宾

-

威望
0
论坛币
21 个
通用积分
0
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
366 点
帖子
37
精华
0
在线时间
30 小时
注册时间
2012-7-16
最后登录
2016-10-13

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
car38_0car39_0car40_0car41_0car42_0car44_0car45_0car46_0

1.737921

2.468084

1.182506

1.505058

0.693578

0.940331

-2.71841

-0.13494


问题1:我希望生成一个新的变量car_0,为上述后缀为_0的变量对应数值的平均值,这个在stata中如何才能实现?
因为这样后缀为0的car`i'变量太多,手动计算很麻烦,我用最笨的方法试着算了下,结果显示错误。问题2:forvalues命令中还能嵌入forvalues命令吗?像下面generate各公司的car,我本来希望用forvalues来生成,但显示错误。

我的命令如下:
forvalues k=0/20{
mvreg ret1 ret2 ret3 ret4 ret5 ret6 ret7 ret8 ret9 ret11 ret13 ret14 ret15 ret16 ret17 ret18 ret19 ret20 ret21 ret22 ret23 ret24 ret25 ret26 ret27 ret28 ret29 ret30 ret31 ret32 ret33 ret34 ret35 ret36 ret37 ret38 ret39 ret40 ret41 ret42 ret44 ret45 ret46 ret47 ret48 ret49 ret50 ret51 ret52 ret53 ret54 ret55 ret56 ret57 ret58 ret59 ret60 ret61 ret63 ret64 ret65 ret66 ret67 ret68 ret69 ret70 ret71 ret72 ret73 ret74 ret75 ret76 ret77 ret78 ret79 ret80 ret81 ret82 ret83 ret84 ret85 ret86 ret87 ret88 ret89 ret90 ret91 ret92 ret93 ret94 ret95 ret96 ret97 ret98 ret99 ret100 ret101 ret102 ret103 = mar_ret dummy_`k'

gen car1_`k'= (`k'+1)*[ret1]_b[dummy_`k']
...
gen car103_`k'=(`k'+1)*[ret103]_b[dummy_`k'] /*这里我试图用forvalues来分别生成,但似乎不行*/

gen  car_`k'=( car1_`k' + car2_`k' + car3_`k'+ car4_`k'+ car5_`k'+ car6_`k'+ car7_`k'+ car8_`k'+ car9_`k'+ car11_`k'+ car13_`k'+ car14_`k'+ car15_`k'+ car16_`k'+ car17_`k'+ car18_`k'+ car19_`k'+ car20_`k'+ car21_`k'+ car22_`k'+ car23_`k'+ car24_`k'+ car25_`k'+ car26_`k'+ car27_`k'+ car28_`k'+ car29_`k'+ car30_`k'+ car31_`k'+ car32_`k'+ car33_`k'+ car34_`k'+ car35_`k'+car36_`k'+ car37_`k'+ car38_`k'+ car39_`k'+ car40_`k'+ car41_`k'+ car42_`k'+ car44_`k'+ car45_`k'+ car46_`k'+ car47_`k'+ car48_`k'+ car49_`k'+ car50_`k'+ car51_`k'+ car52_`k'+ car53_`k'+ car54_`k'+ car55_`k'+ car56_`k'+ car57_`k'+ car58_`k'+ car59_`k'+ car60_`k'+ car61_`k'+ car63_`k'+ car64_`k'+ car65_`k'+ car66_`k'+ car67_`k'+ car68_`k'+ car69_`k'+ car70_`k'+ car71_`k'+ car72_`k'+ car73_`k'+ car74_`k'+ car75_`k'+ car76_`k'+ car77_`k'+ car78_`k'+ car79_`k'+ car80_`k'+        car81_`k'+ car82_`k'+ car83_`k'+ car84_`k'+ car85_`k'+ car86_`k'+ car87_`k'+ car88_`k'+ car89_`k'+ car90_`k'+ car91_`k'+car92_`k'+ car93_`k'+car94_`k'+ car95_`k'+ car96_`k'+ car97_`k'+ car98_`k'+ car99_`k'+ car100_`k'+ car101_`k'+ car102_`k'+ car103_`k')/99 *
}

显示 car_0 not found. error
请教各位同学有什么简单的方法可以解决以上两个问题?看过sum()函数的help手册,但是解决不了问题。
谢谢!





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Stata tata forvalues forvalue generate 如何

沙发
ermutuxia 发表于 2014-10-14 09:51:11 |只看作者 |坛友微信交流群
你首先在循环外写一个命令
gen car_0=.
然后循环里的命令用replace

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-11-5 19:44