如何在 SAS 的数据集中循环不同的 where 语句
How can I loop different where statements in the dataset in SAS
我有多个变量的条件 where 语句,例如:
where 1<ColA<3;
where 4<ColB<6;
where 2<ColC<6;
.....
where 5<ColN<8;
我想根据 where 语句对每个数据集进行子集化,例如:
data newds1;
set original;
where 1<ColA<3;
run;
data newds2;
set original;
where 4<ColB<6;
run;
.....
data newdsn;
set original;
where 5<ColN<8;
run;
如何在 SAS 中循环执行此操作?
创建一个宏来为您执行循环。您可以 pipe-separate 所有的 where 语句并循环遍历每个语句。
%macro subset(data=, conditions=, out=);
%let n = %sysfunc(countw(&conditions., |) );
%do i = 1 %to &n.;
%let where = %sysfunc(scan(&conditions., &i., |) );
data &out.&i.;
set &data.;
where &where.;
run;
%end;
%mend;
%subset(data=original, conditions=1<ColA<3 | 4<ColB<6 | 2<ColC<6, out=newds);
我有多个变量的条件 where 语句,例如:
where 1<ColA<3;
where 4<ColB<6;
where 2<ColC<6;
.....
where 5<ColN<8;
我想根据 where 语句对每个数据集进行子集化,例如:
data newds1;
set original;
where 1<ColA<3;
run;
data newds2;
set original;
where 4<ColB<6;
run;
.....
data newdsn;
set original;
where 5<ColN<8;
run;
如何在 SAS 中循环执行此操作?
创建一个宏来为您执行循环。您可以 pipe-separate 所有的 where 语句并循环遍历每个语句。
%macro subset(data=, conditions=, out=);
%let n = %sysfunc(countw(&conditions., |) );
%do i = 1 %to &n.;
%let where = %sysfunc(scan(&conditions., &i., |) );
data &out.&i.;
set &data.;
where &where.;
run;
%end;
%mend;
%subset(data=original, conditions=1<ColA<3 | 4<ColB<6 | 2<ColC<6, out=newds);