对变量批量重命名

这几种方法来自《Renaming in Batches》——Vincent Weng, Educational Testing Service, Princeton, NJ
Ying Feng, CTB/McGraw-Hill, Monterey, CA

建立数据

/* Creating a dataset */
DATA A;
input id $4. before_var1_after before_var2_after before_var3_after;
datalines;
i001 1 2 3
i002 3 4 5
i003 6 7 8
i004 9 10 12
;
run;

一、所有变量名前加前缀

/* Creating a dataset */
DATA A;
input id $4. before_var1_after before_var2_after before_var3_after;
datalines;
i001 1 2 3
i002 3 4 5
i003 6 7 8
i004 9 10 12
;
run;
/* Adding Prefix on all variables */
%macro rename(lib,dsn,newname);
proc contents data=&lib..&dsn;
title 'before renaming';
run;
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;
run;
proc datasets library = &LIB;
modify &DSN;
rename
%do i = 1 %to &num_vars.;
&&var&i = &newname._&&var&i.
%end;
;
quit;
run;
proc contents data=&lib..&dsn.;
title 'after renaming';
run;
%mend rename;
DATA B;
set A;
run;
%rename(WORK,B,Try1);

2.对选择的变量名加前缀

/* Adding Prefix on Selected Variables */
%macro addprefix(lib,dsn,start,end,newname);
proc contents data=&lib..&dsn;
title 'before renaming';
run;
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;
run;
proc datasets library = &LIB;
modify &DSN;
rename
%do i = &start. %to &end.;
&&var&i = &newname_&&var&i.
%end;
;
quit;
run;
proc contents data=&lib..&dsn;
title 'Adding Prefix on Selected variables';
run;
%mend addprefix;
DATA C;
set A;
run;
%addprefix(WORK,C,2,4,Try2);

3.替换选择的变量名的前缀

/* Replacing Prefix on Selected Variables */
%macro replaceprefix(lib,dsn,start,end,oldprefix,newprefix);
proc contents data=&lib..&dsn.;
title 'before renaming';
run;
data temp;
set &lib..&dsn.;
run;
%LET ds=%SYSFUNC(OPEN(temp,i));
%let ol=%length(&oldprefix.);
%do i=&start %to &end;
%let dsvn&i=%SYSFUNC(VARNAME(&ds,&i));
%let l=%length(&&dsvn&i);
%let vn&i=&newprefix.%SUBSTR(&&dsvn&i,&ol+1,%EVAL(&l-&ol));
%end;
data &lib..&dsn.;
set temp;
%do i=&start %to &end;
&&vn&i=&&dsvn&i;
drop &&dsvn&i;
%end;
%let rc=%SYSFUNC(CLOSE(&ds));
proc contents data=&lib..&dsn.;
title 'Replacing Prefix on Selected variables ';
run;
%mend replaceprefix;
DATA D;
set A;
run;
%replaceprefix(WORK,D,2,4,before,Try3);

/* Adding Prefix = Replacing BLANK Prefix on Selected Variables */
DATA D;
set A;
run;
%replaceprefix(WORK,D,2,4, ,Try3_);

4.替换选择的变量名的后缀

/* Replacing Suffix on Selected Variables */
%macro replacesuffix(lib,dsn,start,end,oldsuffix,newsuffix);
proc contents data=&lib..&dsn.;
title 'before renaming';
run;
data temp;
set &lib..&dsn.;
run;
%LET ds=%SYSFUNC(OPEN(temp,i));
%let ol=%length(&oldsuffix.);
%do i=&start %to &end;
%let dsvn&i=%SYSFUNC(VARNAME(&ds,&i));
%let l=%length(&&dsvn&i);
%let vn&i=%SUBSTR(&&dsvn&i,1,%EVAL(&l-&ol))&newsuffix.;
%end;
data &lib..&dsn.;
set temp;
%do i=&start %to &end;
&&vn&i=&&dsvn&i;
drop &&dsvn&i;
%end;
%let rc=%SYSFUNC(CLOSE(&ds));
proc contents data=&lib..&dsn.;
title ' Replacing Suffix on Selected variables ';
run;
%mend replacesuffix;
DATA E;
set A;
run;
%replacesuffix(WORK,E,2,4,after,Try4);

/* Adding Suffix = Replacing BLANK Suffix on Selected Variables */
DATA F;
set A;
run;
%replacesuffix(WORK,E,2,4, ,_Try4);

 

 

 

 

你可能感兴趣的:(sql,service,library,dataset,testing,variables)