Hello, SAS experts!
Here is my code:
Data one;
A=0;
ID_U=1;
ID_V=2;
Run;
/* Running the renaming macro */
options macrogen mprint mlogic;
%macro rename(lib,dsn,prefixtarget);
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;
%PUT &&VAR&i;
%IF %index(&&var&i,'ID') GT 0 %THEN &&var&i=&prefixtarget._&&var&i.;
%end;
;
quit;
run;
%mend rename;
%rename(WORK,ONE,CAL);
So my goal is to rename the variables based on whether the variables name contains a specific text expression(in this case, "ID"). So the final result I expect is:
A | CAL_ID_U | CAL_ID_V |
0 | 1 | 2 |
I have tried many different ways, but none of them work. Can you please tell me where goes wrong?
Thanks!
Your macro will work correctly with the following changes:
Data one; A=0; ID_U=1; ID_V=2; Run; /* Running the renaming macro */ options macrogen mprint mlogic; %macro rename(lib,dsn,prefixtarget); /*ADD*/ 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; %PUT &&VAR&i; /* remove quotes around ID */ %IF %index(&&var&i,ID) GT 0 %THEN &&var&i=&prefixtarget._&&var&i.; %end; ; run; quit; /*not needed*/ /*run;*/ %mend rename; %rename(WORK,ONE,CAL);
Art, CEO, AnalystFinder.com
Your macro will work correctly with the following changes:
Data one; A=0; ID_U=1; ID_V=2; Run; /* Running the renaming macro */ options macrogen mprint mlogic; %macro rename(lib,dsn,prefixtarget); /*ADD*/ 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; %PUT &&VAR&i; /* remove quotes around ID */ %IF %index(&&var&i,ID) GT 0 %THEN &&var&i=&prefixtarget._&&var&i.; %end; ; run; quit; /*not needed*/ /*run;*/ %mend rename; %rename(WORK,ONE,CAL);
Art, CEO, AnalystFinder.com
Are you running this for datasets with a large number of variables? Over 1000?
If not, consider creating one macro variable in the rename.
See here:
A much simpler method:
%let lib=work; %let dsn=abc; %let pf=cal; data _null_; set sashelp.vcolumn (where=(libname="&LIB." and memname="&DSN." and index(name,"ID") > 0)) end=last; if _n_=1 then call execute('proc datasets lib=work; modify abc;'); call execute(catx(' ','rename',name,"=&PF._",name,';')); if last then call execute('run; quit;'); run;
Do note however that if you go over the given length of SAS names, or include characters not allowed etc. any code will not work.
There is no need add quotes around string literals in macro code. Everything is a string to macro code. Also if you want to use %INDEX() to test if a string begins with specific characters then you need to worry both about the case of the string and the position that the characters are found. So to find names that start with 'ID_' you could use:
%IF %index(%upcase(&&var&i),ID_) = 1 %THEN ....;
But in reality you can take the testing of the variable name out of macro logic and put it into regular SAS logic where it is easier to code and to debug.
Here is version of your macro that greatly simplies the metadata query to get the list of variables that begin with 'ID_'.
%macro rename(lib,dsn,prefix);
%local i nvar ;
proc sql noprint ;
select name into :var1-
from dictionary.columns
where libname=%upcase("&LIB")
and memname=%upcase("&DSN")
and upcase(name) like 'ID^_%' escape '^'
;
%let nvar=&sqlobs;
quit;
%if (&nvar) %then %do;
proc datasets lib=&lib nolist ;
modify &dsn ;
rename
%do i=1 %to &sqlobs ;
&&var&i = &prefix._&&var&i
%end;
;
run; quit;
%end;
%mend rename ;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.