SAS中文论坛

标题: 看看此帖有无简便解法 [打印本页]

作者: shiyiming    时间: 2004-9-15 14:56
标题: 看看此帖有无简便解法
一个表中有sex,age,name……字段,现在,需要将sex,age均相同的人识别为一组,并建立组号gid,gid依次累加。(即添加gid)

SQL server中的存储过程实现太复杂了

[code:9a20f]declare @sex char(2), @age char(2),
@i int
declare cur_group cursor
for
select sex,age from tbl group by sex, age
for read_only

open cur_group
fetch next from cur_group into @sex, @age

set @i = 1
while @@fetch_status = 0
begin
update tbl set gid = @i where sex=@sex and age=@age
fetch next from cur_group into @sex, @age
set @i = @i + 1
end

close cur_group
deallocate cur_group [/code:9a20f]
作者: shiyiming    时间: 2004-9-16 09:06
标题: test
不明白你的gid,依次累加是什么意思?
我有一个解法
[code:e5f31]proc sort data=tem;
  by sex age;
data tem1;
  retain no 0;
  set tem;
  by a b;
  flag=no;
  if last.b then no+1;
run;
proc sql;
  create table tem3 as
  select count(flag) as c,sex,age
  from tem2
  group by flag;
quit;[/code:e5f31]
作者: shiyiming    时间: 2004-9-16 11:54
标题: 感谢gbt
你提供的代码的确很简单,对我很有启发,多谢!




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2