首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于宏变量值的复制/重命名多个sas数据集

基于宏变量值的复制/重命名多个sas数据集
EN

Stack Overflow用户
提问于 2016-08-12 15:59:01
回答 1查看 914关注 0票数 2

我有一个宏变量&myfiles,它包含四个数据集的名称列表。

代码语言:javascript
复制
%put &myfiles;
cpo.CDR_2016jun cpo.Cog_2016jun cpo.Mile_2016jun cpo.Path_2016jun

其中cpo是一个libname。

我正在尝试创建四个新的数据集,它们的名称来自我命名为&New_Datasets的另一个宏变量

代码语言:javascript
复制
%put &New_Datasets;
CDR Cog Mile Path

我试着使用这样的数据步骤:

代码语言:javascript
复制
data &New_Datasets;
     set &myfiles;
run;

但是,这导致将&mylist中引用的四个数据集的所有观测结果组合在一起,并将其放入&New_Datasets中引用的四个数据集中的每一个,日志的输出如下:

代码语言:javascript
复制
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语句中引用的数据集的组合。任何帮助都将不胜感激,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-12 16:21:05

您必须编写宏程序,该程序循环遍历宏变量中的值,并调用数据步骤或proc副本。

宏:

代码语言:javascript
复制
%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;

示例用法:

代码语言:javascript
复制
%rewriteDataSets(source_tables = sashelp.class sashelp.class,
                 dest_tables   = a b);

或者使用您指定的表,您可以这样称呼它:

代码语言:javascript
复制
%rewriteDataSets(source_tables = cpo.CDR_2016jun cpo.Cog_2016jun cpo.Mile_2016jun cpo.Path_2016jun,
                 dest_tables   = CDR Cog Mile Path);

或者使用proc copy代替数据步骤。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38922128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档