revelc 发表于 2011-9-19 16:23
可是这些都只是加坠子啊
简单修改下,原理是一样的
- /* 以此为例,4个变量 */
- DATA A;
- d23=1; e40=2; c50=3; zh9000=4;
- run;
- /* 原理,将变量读入红变量进行修改 */
- %macro rename(lib,dsn,newname);
- proc sql noprint;
- select nvar into :num_vars
- from dictionary.tables
- where libname="&LIB" and memname="&DSN";
- select distinct(name) into :var1-:var%trim(%left(&num_vars))
- from dictionary.columns
- where libname="&LIB" and memname="&DSN";
- quit;
- proc datasets library = &LIB;
- modify &DSN;
- rename
- %do i = 1 %to &num_vars.;
- &&var&i = &newname.&i.
- %end;
- ;
- quit;
- %mend rename;
- data b;
- set a;
- run;
- %rename(WORK,b,D);
复制代码运行结果:
NOTE: Renaming variable c50 to D1.
NOTE: Renaming variable d23 to D2.
NOTE: Renaming variable e40 to D3.
NOTE: Renaming variable zh9000 to D4.
NOTE: MODIFY was successful for WORK.A.DATA.
NOTE: PROCEDURE DATASETS used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds