我有一个宏变量&myfiles,它包含四个数据集的名称列表。
%put &myfiles;
cpo.CDR_2016jun cpo.Cog_2016jun cpo.Mile_2016jun cpo.Path_2016jun其中cpo是一个libname。
我正在尝试创建四个新的数据集,它们的名称来自我命名为&New_Datasets的另一个宏变量
%put &New_Datasets;
CDR Cog Mile Path我试着使用这样的数据步骤:
data &New_Datasets;
set &myfiles;
run;但是,这导致将&mylist中引用的四个数据集的所有观测结果组合在一起,并将其放入&New_Datasets中引用的四个数据集中的每一个,日志的输出如下:
NOTE: There were 1482 observations read from the data set CPO.CDR_2016JUN.
NOTE: There were 1444 observations read from the data set CPO.COG_2016JUN.
NOTE: There were 255 observations read from the data set CPO.MILE_2016JUN.
NOTE: There were 7 observations read from the data set CPO.PATH_2016JUN.
NOTE: The data set WORK.CDR has 3188 observations and 1580 variables.
NOTE: The data set WORK.COG has 3188 observations and 1580 variables.
NOTE: The data set WORK.MILE has 3188 observations and 1580 variables.
NOTE: The data set WORK.PATH has 3188 observations and 1580 variables.我想要完成的是让来自cpo.cdr_2016jun的1482个观察创建一个数据集work.cdr和1482个观察等等,而不是让每个新的数据集是set语句中引用的数据集的组合。任何帮助都将不胜感激,谢谢!
发布于 2016-08-12 16:21:05
您必须编写宏程序,该程序循环遍历宏变量中的值,并调用数据步骤或proc副本。
宏:
%macro rewriteDataSets(source_tables=, dest_tables=);
%local ii num_source_tables num_dest_tables source_name dest_name;
%let num_source_tables = %sysfunc(countw(&source_tables, %str( )));
%let num_dest_tables = %sysfunc(countw(&dest_tables , %str( )));
%if &num_source_tables ne &num_dest_tables %then %do;
%put ERROR: The number of source and destination tables must be the same in the call to rewriteDataSets;
%abort cancel;
%end;
%do ii=1 %to &num_source_tables;
%let source_name = %scan(&source_tables, &ii, %str( ));
%let dest_name = %scan(&dest_tables , &ii, %str( ));
data &dest_name;
set &source_name;
run;
%end;
%mend rewriteDataSets;示例用法:
%rewriteDataSets(source_tables = sashelp.class sashelp.class,
dest_tables = a b);或者使用您指定的表,您可以这样称呼它:
%rewriteDataSets(source_tables = cpo.CDR_2016jun cpo.Cog_2016jun cpo.Mile_2016jun cpo.Path_2016jun,
dest_tables = CDR Cog Mile Path);或者使用proc copy代替数据步骤。
https://stackoverflow.com/questions/38922128
复制相似问题