我今年的每个月都有6个每月的数据集,每个月都会添加一个新的数据集。我需要一个宏来自动将增量数据集追加到现有数据集中
(通过附加6个现有数据集创建)
,依此类推,每个月继续。数据集的变量顺序完全相同,名称如下:
'completeextract8634.sas7bdat'
'completeextract8640.sas7bdat'
,依此类推。 有没有办法做到这一点?谢谢!
答案 0 :(得分:1)
一种方法可能是使用SET
语句堆叠数据,并使用通配符(:
指定要堆叠的数据集。通配符也称为快捷方式命名或正式称为"Data Set List"
示例数据步骤视图
DATA work.combined / view=work.combined;
SET EXTRACT.completeextract: ;
RUN;
Proc APPEND
也非常适合。请参阅文档讨论Concatenating Data Sets By Using the APPEND Procedure
示例宏
注意:此示例假定您的“系统”已设置为通过将附加的摘录移至其他库来管理这些摘录。如果没有移动文件,并且 growing 表没有唯一索引,则最终可能会多次添加摘录。
LIBNAME EXTRACT '<folder containing extract data sets>';
LIBNAME HISTORY '<folder containing the permanent and growing data set>';
LIBNAME DONE '<folder where extract data sets can be moved to>';
%macro appendFromLib(base=, fromLIb=, dataNamesLike=, doneLib=);
proc sql;
%if %sysfunc(exist(work.dataNames)) %then %do;
drop table dataNames;
%end;
create table dataNames as select
memname from dictionary.tables
where libname = "%upcase(&fromLib)"
and memname like %upcase(&dataNamesLike)
;
data _null_;
set dataNames;
appendStep = "PROC APPEND BASE=&BASE DATA=&FROMLIB.." || trim(memname) || ";" ;
call execute ('%NRSTR(' || trim(appendStep) || ')');
maintenanceStep = "PROC DATASETS NOLIST; COPY IN=&FROMLIB OUT=&DONELIB MOVE; SELECT " || trim(memname) || '; QUIT;' ;
call execute ('%NRSTR(' || trim(maintenanceStep) || ')');
run;
%let syslast = &BASE;
%mend;
%appendFromLib (
base=HISTORY.COMPLETE_ALL
fromLib=EXTRACT,
dataNamesLike='completeextract%',
doneLib=DONE
);
进一步的网络搜索 Proc APPEND 可能会找到其他示例,也许还有一些包含相同功能的现有宏库。
答案 1 :(得分:0)
回应评论
您可以更改宏定义以指定默认值,例如dataNamesLike=%
,然后添加新参数dataNamesWhere=1
。
附加参数可以在 dataNames 查询中使用,也具有and &dataNamesWhere
。您将负责提出一个足够健壮的where子句,该子句可以执行所需的选择。
您还可以添加参数dataNames=
,该参数包含预制的数据集列表,以追加/维护的方式处理。如果使用此参数,则会跳过默认的 discovery 步骤。
备用代码示例
* make my own list of data sets to process;
%let names = ;
proc sql outobs=3;
create table work.dataNamesX as
select memname
from dictionary.tables
where libname = "EXTRACT"
and memname like "COMPLETEEXTRACT%"
order by
memname descending
;
%appendFromLib(base=here.biggun, fromLib=EXTRACT, dataNames=work.dataNamesX, doneLib=there);
更新后的宏
%macro appendFromLib(base=, fromLIb=, dataNamesLike=%, doneLib=, dataNames=);
proc sql;
%if %length (&dataNames) = 0 %then %do;
%let dataNames = work.dataNames;
%if %sysfunc(exist(&dataNames)) %then %do;
drop table &dataNames;
%end;
create table &dataNames as select
memname from dictionary.tables
where libname = "%upcase(&fromLib)"
and memname like %upcase(&dataNamesLike)
;
%end;
data _null_;
set dataNames;
appendStep = "PROC APPEND BASE=&BASE DATA=&FROMLIB.." || trim(memname) || ";" ;
call execute ('%NRSTR(' || trim(appendStep) || ')');
maintenanceStep = "PROC DATASETS NOLIST; COPY IN=&FROMLIB OUT=&DONELIB MOVE; SELECT " || trim(memname) || '; QUIT;' ;
call execute ('%NRSTR(' || trim(maintenanceStep) || ')');
run;
%let syslast = &BASE;
%mend;