这是一种相当直接的方式:
data student;
input v1 $ v2 $ v3;
cards ;
f j 20
f j 20
f j 22
f s 18
f s 18
m j 19
m j 19
m s 20
;
run;
%macro make_dsns;
proc sql;
create table dsn_list as
select distinct
v1,
v2,
v3
from student
;
run;
data _null_;
retain count 0;
set dsn_list end=last;
count = count + 1;
dsn_name = trim(left(v1)) || trim(left(v2)) || trim(left(v3));
dsn_reference = "dsn" || trim(left(count));
call symput(dsn_reference , dsn_name);
if last then do;
call symput("max_count" , count);
end;
run;
data
%do count = 1 %to &max_count;
&&dsn&count
%end;
;
set student;
drop reference;
reference = trim(left(v1)) || trim(left(v2)) || trim(left(v3));
%do count = 1 %to &max_count;
if trim(left(reference)) eq trim(left("&&dsn&count")) then output &&dsn&count;
%end;
run;
%mend make_dsns;
%make_dsns;