全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2530 3
2009-03-27

请教大侠们,弱问,如何按照不同的类别给变量标上序号?

比如原来的数据集为:

year month

1998 9

1998 9

1998 10

1998 10

1999 5

1999 5

1999 5

1999 6

1999 6

想按照年份和月份给这些变量标上序号。即年份和月份相同的观测值的序号相同。目标数据集为:

year month id

1998 9 1

1998 9 1

1998 10 2

1998 10 2

1999 5 3

1999 5 3

1999 5 3

1999 6 4

1999 6 4

感觉上应该不是个很难的问题,但是本人真的不会,恳请大家帮忙,非常感谢!

二维码

扫码加我 拉你入群

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

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

全部回复
2009-3-27 18:49:00

data raw;
input year month;
cards;
1998 9
1998 9
1998 10
1998 10
1999 5
1999 5
1999 5
1999 6
1999 6
;
run;
proc sort data=raw;
by year month;
run;


data a;
set raw;
by year month;
retain id;
if  first.month then id=1;
else id+1;
run;

data result;
   set a;
   retain type 0;
   if id=1 then do;
     type+1;
  end;
  else;
  drop id;
  rename type=id;
run;

二维码

扫码加我 拉你入群

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

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

2009-3-27 18:53:00
data ym;
   input year moth @@;
   cards;
1998 9
1998 9
1998 10
1998 10
1999 5
1999 5
1999 5
1999 6
1999 6
;
proc sort data=ym;
   by year month;
   run;
data want;
  set ym;
  by year month;
  retain id o;
  if  first.month  then do;
  id+1;
  end;
run;

[此贴子已经被作者于2009-3-27 18:59:58编辑过]

二维码

扫码加我 拉你入群

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

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

2009-3-27 23:21:00

谢谢楼上的两位,我试了下,两种方法都可以!!

学习了!

谢谢!

二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群