data test;
input id $ amount;
datalines;
A 189
B 160
B 188
B 165
A 145
A 199
B 198
B 345
;
data wanted;
if 0 then set test;
if _n_=1 then do;
declare hash h();
h.definekey('id');
h.definedata('id','total');
h.definedone();
end;
do until(last);
set test end=last;
if h.find()=0 then do;
total+amount;
h.replace();
end;
else do;
total=amount;
h.add();
end;
end;
do until(last1);
set test end=last1;
if h.find()=0 and total>=600 then output;
end;
drop total;
stop;
run;
非常感谢,再多问一句,如果有另一个表(test1),内有字段id、name等,想用一个data步实现关联和筛选,我写的程序如下,结果是name一栏全为空?:
data wanted;
if 0 then set test1;
if _n_=1 then do;
declare hash h();
h.definekey('id');
h.definedata('id','name','total');
h.definedone();
end;
do until(last);
set test end=last;
if h.find()=0 then do;
total+amount;
name=name;
h.replace();
end;
else do;
total=amount;
name=name;
h.add();
end;
end;
do until(last1);
set test end=last1;
if h.find()=0 and total>=600 then output;
end;
drop total;
stop;
run;
data test;
input id $ amount;
datalines;
A 189
B 160
B 188
B 165
A 145
A 199
B 198
B 345
;
data test1;
input id $ name $;
datalines;
A Jose
B Hose'B'
;
data wanted;
if 0 then set test test1;
if _n_=1 then do;
declare hash h();
h.definekey('id');
h.definedata('id','total');
h.definedone();
declare hash h_name(dataset:'test1');
h_name.definekey('id');
h_name.definedata('id','name');
h_name.definedone();
end;
do until(last);
set test end=last;
if h.find()=0 then do;
total+amount;
h.replace();
end;
else do;
total=amount;
h.add();
end;
end;
do until(last1);
set test end=last1;
if h.find()=0 and total>=600 then do;
h_name.find();
output;
end;
end;
drop total;
stop;
run;
楼主,你好,请问hash、hiter类能否在SAS8版本中使用?我把上面的代码原封不动的搬到PGM窗口运行,错误如下:
180
NOTE: SCL source line.
230 h.definekey('id');
-
22
ERROR: Invalid variable specification, h.definekey.
Variable names of the form X.X must be either FIRST.X or LAST.X
NOTE: SCL source line.
231 h.definedata('id','total');
-
22
76
ERROR: Invalid variable specification, h.definedata.
Variable names of the form X.X must be either FIRST.X or LAST.X
只写上面这些了,楼主帮我找下原因吧