我有一个SAS数据集,看起来像这样:
id | dept | ...
1 A
2 A
3 A
4 A
5 A
6 A
7 A
8 A
9 B
10 B
11 B
12 B
13 B
每个观察代表一个人。
我想分裂成集“团队”的数据集,每个数据集最多可以有3个观测指标。
对于以上示例将意味着创建3个数据集为部门A(这些数据集的2将包含3个意见和第三数据集将包含2周的观察结果)。 和2点的数据集用于部门B(1条含有3条意见和其他含有2个观察值)。
像这样:
第一个数据集(deptA1):
id | dept | ...
1 A
2 A
3 A
第二个数据集(deptA2)
id | dept | ...
4 A
5 A
6 A
第三个数据集(deptA3)
id | dept | ...
7 A
8 A
第四数据集(deptB1)
id | dept | ...
9 B
10 B
11 B
第五数据集(deptB2)
id | dept | ...
12 B
13 B
我使用的完整数据集包含成千上万的观察与超过50个科指南 我可以工作,如何要求每个部门的许多数据集,我觉得宏是去为需要数据集的数量是动态的最佳途径。 但我想不通的逻辑,使他们有最多有3个观测创建数据集。 任何帮助表示赞赏。
另一个版本。 相比于DavB版本,它只处理输入一次数据,并将它分为单datastep几个表。 此外,如果需要更复杂的分割规则,它可以在datastep视图WORK.SOURCE_PREP实现。
data WORK.SOURCE;
infile cards;
length ID 8 dept $1;
input ID dept;
cards;
1 A
2 A
3 A
4 A
5 A
6 A
7 A
8 A
9 B
10 B
11 B
12 B
13 B
14 C
15 C
16 C
17 C
18 C
19 C
20 C
;
run;
proc sort data=WORK.SOURCE;
by dept ID;
run;
data WORK.SOURCE_PREP / view=WORK.SOURCE_PREP;
set WORK.SOURCE;
by dept;
length table_name $32;
if first.dept then do;
count = 1;
table = 1;
end;
else count + 1;
if count > 3 then do;
count = 1;
table + 1;
end;
/* variable TABLE_NAME to hold table name */
TABLE_NAME = catt('WORK.', dept, put(table, 3. -L));
run;
/* prepare list of tables */
proc sql noprint;
create table table_list as
select distinct TABLE_NAME from WORK.SOURCE_PREP where not missing(table_name)
;
%let table_cnt=&sqlobs;
select table_name into :table_list separated by ' ' from table_list;
select table_name into :tab1 - :tab&table_cnt from table_list;
quit;
%put &table_list;
%macro loop_when(cnt, var);
%do i=1 %to &cnt;
when ("&&&var.&i") output &&&var.&i;
%end;
%mend;
data &table_list;
set WORK.SOURCE_PREP;
select (TABLE_NAME);
/* generate OUTPUT statements */
%loop_when(&table_cnt, tab)
end;
run;
你可以试试这个:
%macro split(inds=,maxobs=);
proc sql noprint;
select distinct dept into :dept1-:dept9999
from &inds.
order by dept;
select ceil(count(*)/&maxobs.) into :numds1-:numds9999
from &inds.
group by dept
order by dept;
quit;
%let numdept=&sqlobs;
data %do i=1 %to &numdept.;
%do j=1 %to &&numds&i;
dept&&dept&i&&j.
%end;
%end;;
set &inds.;
by dept;
if first.dept then counter=0;
counter+1;
%do i=1 %to &numdept.;
%if &i.=1 %then %do;
if
%end;
%else %do;
else if
%end;
dept="&&dept&i" then do;
%do k=1 %to &&numds&i.;
%if &k.=1 %then %do;
if
%end;
%else %do;
else if
%end;
counter<=&maxobs.*&k. then output dept&&dept&i&&k.;
%end;
end;
%end;
run;
%mend split;
%split(inds=YOUR_DATASET,maxobs=3);
只需更换您的输入数据集的名称%分配宏调用的INDS参数值。