前几天看到一个群友提的一个问题,根据数据集中的某一个变量的值将一人大数据集拆分为多个小数据集(见上图第15题),实现这一目的的方法有多种,最常见的方法应该是宏循环,下面以根据变量SEX来拆分数据集SASHELP.CLASS为例介绍其他几种方法:
proc sql;
create table sex as
select distinct SEX
from sashelp.class
;
quit;
data _null_;
set sex;
call execute('data sex_'||cats(SEX)||'(where=(SEX='||quote(cats(SEX))||')); set sashelp.class; run;');
run;
proc sql;
create table sex as
select distinct SEX
from sashelp.class
;
quit;
filename code temp;
data _null_;
file code;
set sex;
put ' sex_' SEX '(where=(SEX="' SEX '"))' @@;
run;
data %inc code;;
set sashelp.class;
run;
proc sort data=sashelp.class out=class;
by SEX;
run;
data _null_;
dcl hash h(multidata:'y');
h.definekey('SEX');
h.definedone();
do until(last.SEX);
set class;
by SEX;
h.add();
end;
h.output(dataset:cats('sex_', SEX));
run;
上面几种方法中第一种方法程序行数最少,第二种方法行数最多,但是我们可以看到第一、第三种方法有多次SET的操作,所以当要拆分的数据集较大时建议用第二种方法以提高效率。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有