用于多个数据集的 SAS 宏 [英] SAS Macro for multiple datasets

查看:22
本文介绍了用于多个数据集的 SAS 宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SAS 新手.我在一个文件夹中有 12 个(每月数据)数据集.数据集的名称是:

I am new to SAS. I have 12(Monthly data) data sets in a folder. Names of data sets are:

201401
201402
201403
...
201411
201412

每个数据包含 10 个变量.所有数据的变量名称都相同.我只想要 10 个变量中的 3 个变量,并通过 new_201401 等重命名数据.

Each data contain 10 Variables. Variable names are same for all data. I want only 3 Variables among 10 and rename data by new_201401 and so on.

我正在使用 Keep Var1 Var2 Var3; 手动尝试它,但有没有简单的方法或宏让我们可以让它更快?提前致谢.

I am trying it manually by using Keep Var1 Var2 Var3; but is there any easy way or macro so we can make it fast? Thanks in advance.

推荐答案

您可以使用以下宏重命名它们(注意:%if 条件只是拆分为包含前导 0 表示个位数月份):

You can rename them using the following macro (note: the %if conditions are just split out to include a leading 0 for single digit months):

%macro monthly(year=,prefix=) ;
  %do i=1 %to 2 ;
    %if %eval(&i<10) %then Data_&year.0&i=&prefix&i ;
    %else                  Data_&year&i=&prefix&i ;
  %end ;
%mend monthly ;

然后,您可以将这些值传递到 proc datasets 中,无论您需要什么年份:

You can then then for example pass these values into proc datasets for whatever years you need:

proc datasets library=work ;
  change %monthly(year=2014,prefix=new_) %monthly(year=2015,prefix=new2_);
run ;

这篇关于用于多个数据集的 SAS 宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆