SAS Macro for multiple datasets

2019-08-03 06:28发布

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

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.

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.

标签: sas sas-macro
2条回答
做自己的国王
2楼-- · 2019-08-03 07:00

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 ;

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 ;
查看更多
可以哭但决不认输i
3楼-- · 2019-08-03 07:10

I think this will do the trick:

%macro keep(table,var1,var2,var3,set);
data &table (keep=&var1 &var2 &var3);
set &set;
run;
%mend keep;
查看更多
登录 后发表回答