问题
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.
回答1:
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;
回答2:
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 ;
来源:https://stackoverflow.com/questions/30643590/sas-macro-for-multiple-datasets