楼主: breeze112
44095 14

_n和_N的区别和具体用法 [推广有奖]

  • 0关注
  • 0粉丝

高中生

35%

还不是VIP/贵宾

-

威望
0
论坛币
688 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
200 点
帖子
24
精华
0
在线时间
23 小时
注册时间
2012-1-27
最后登录
2018-6-15

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
请教一下,STATA里面的_n和_N有什么区别,具体在什么情况下用啊?
谢谢

二维码

扫码加我 拉你入群

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

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

关键词:Stata tata

沙发
空山空语 在职认证  发表于 2012-10-28 11:07:50 |只看作者 |坛友微信交流群
_N是指观测值的总数,_n是第n个观测值。
比如有 2,4,6,8,10 五个观测值,_N就是5,也就可以定位到10,如果令_n==3,那就定位到6.
等待

使用道具

藤椅
breeze112 发表于 2012-11-4 08:39:36 |只看作者 |坛友微信交流群
空山空语 发表于 2012-10-28 11:07
_N是指观测值的总数,_n是第n个观测值。
比如有 2,4,6,8,10 五个观测值,_N就是5,也就可以定位到10, ...
谢谢!

使用道具

板凳
蓝色 发表于 2012-11-4 11:08:40 来自手机 |只看作者 |坛友微信交流群
gen x1=_n
gen x2=_N
list
看见结果就知道了'
学习命令的学会方法'知道如何判断

使用道具

报纸
gongxuhong110 发表于 2012-11-19 22:11:18 |只看作者 |坛友微信交流群
谢谢,学习了
能选择的时间选择自己喜欢的,不能选择的时间做好手头的事,为以后的选择做准备!

使用道具

地板
shetianlang 发表于 2012-11-20 00:10:59 |只看作者 |坛友微信交流群

_n and _N are Stata system variables—they exist whether you like them or not. They are also referred to as “underscore variables” for the obvious reason that they are written as _variable. Little  _n contains the line number of the current observation, while its big brother _N contains the total number of observations in the data.

_n is often used to generate unique codes for each observation:

gen code=_n          /* generates the variable code that contains the integer 1 (for the first obervation, _n==1) to _N (for the last observation, _n==_N) */

Or to refer to neighboring observations (also called subscripting):

gen gdplag=gdp[_n-1]          /* generates the variable gdplag, which is equal to the preceding observation’s gdp */

gen gdpgrowth=(gdp /gdp[_n-1] - 1)*100         /* generates the variable gdpgrowth, the growth rate for the variable gdp */

[Note: The gdplag and gdpgrowth for the first observation (_n==1) will be missing since observation [_n-1] does not exist for _n==1.]

Make sure, however, that you refer to the right neighbor! For example, if you are calculating the growth rate of variable gdp between 1999 and 2000, gdp must be in order such that the gdp subscripted by [_n-1] is the gdp for 1999. This is easily addressed by invoking the -sort- command, “sort year,” before generating the growth rate variable. There is another complication, however, when you are calculating this for different groups of observations, say by country. Will “sort country year” before generating the variable suffice? No. Why? Because the [_n-1] for the first observation of country B refers to the last observation of country A. Here is where Super -bysort- comes to the rescue:

bysort country year: gen gdplag=gdp[_n-1]

bysort country year: gen gdpgrowth=(gdp /gdp[_n-1] - 1)*100      

// Another syntax for bysort is: by country (year), sort: …

_n may also be used to keep the nth observation by group:

bysort householdid: keep if _n==1         /* keeps the first observation for each householdid */

Big brother _N, on the other hand, may be used to generate  a variable that contains the number of observations by group:

bysort householdid: gen householdsize=_N         /* generates the variable householdsize, which is equal to the number of observations  for each householdid. */

What we have illustrated above are just a few examples to showcase the potential of underscore variables _n and _N. For sure, you will find other uses of _n and _N. Another underscore variable is the beautiful number π, which, as you would’ve guessed, is written as _pi.

Source: http://statadaily.wordpress.com/2010/09/01/_n_bysort/
文库:微观数据研讨

使用道具

7
无他 发表于 2012-11-20 09:25:09 |只看作者 |坛友微信交流群
蓝色 发表于 2012-11-4 11:08
gen x1=_n
gen x2=_N
list
嗯,好方法!
大方无隅

使用道具

8
我主沉浮之天地 学生认证  发表于 2016-12-4 15:09:26 |只看作者 |坛友微信交流群
蓝色 发表于 2012-11-4 11:08
gen x1=_n
gen x2=_N
list
谢谢 很受启发

使用道具

9
我主沉浮之天地 学生认证  发表于 2016-12-4 15:10:28 |只看作者 |坛友微信交流群
蓝色 发表于 2012-11-4 11:08
gen x1=_n
gen x2=_N
list
很受 启发  谢谢

使用道具

10
远行1238 发表于 2016-12-29 18:18:52 |只看作者 |坛友微信交流群
蓝色 发表于 2012-11-4 11:08
gen x1=_n
gen x2=_N
list
请问蓝色版主,程序中local N=_N是用来做什么的呢?

使用道具

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

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

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

GMT+8, 2024-5-22 23:15