问题
Using SAS macro, I would like to generate subset of files using data driven method. Can someone direct me where to start? For example, I have a data set like following:
data student;
var v1 $ v2 $ v3;
datalines ;
f j 20
f j 20
f j 22
f s 18
f s 18
m j 19
m j 19
m s 20;
Instead of using 'if' statement for each variable category, I want SAS macro recognize each value in variables and make subsets of data. Do I need to look into loop function or symput (after making proc freq)? thank you, KKK
回答1:
This is one fairly straight forward way:
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;
回答2:
Without macro you can do:
data _null_;
declare hash group;
group = _new_ hash(ordered: 'ascending');
group.definekey('_unique_Key');
group.definedata('v1', 'v2', 'v3');
group.definedone();
do until (last.v3);
set student;
by v1 v2 v3 notsorted;
_unique_key + 1;
group.add();
end;
group.output(dataset: cats(v1,v2,v3));
rc = group.delete();
run;
来源:https://stackoverflow.com/questions/9572166/sas-macro-generating-data-driven-subset-data-files