Use %Index in Macro Do loop to rename variable

Accepted Solution Solved
Reply
Occasional Learner
Posts: 1
Accepted Solution

Use %Index in Macro Do loop to rename variable

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:

 

ACAL_ID_UCAL_ID_V
012

 

 

I have tried many different ways, but none of them work. Can you please tell me where goes wrong?

 

Thanks!


Accepted Solutions
Solution
‎07-03-2017 02:05 PM
PROC Star
Posts: 7,363

Re: Use %Index in Macro Do loop to rename variable

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

 

View solution in original post


All Replies
Solution
‎07-03-2017 02:05 PM
PROC Star
Posts: 7,363

Re: Use %Index in Macro Do loop to rename variable

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

 

Super User
Posts: 17,819

Re: Use %Index in Macro Do loop to rename variable

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:

https://gist.github.com/statgeek/82d9f2854edc01560e0f

Super User
Super User
Posts: 7,401

Re: Use %Index in Macro Do loop to rename variable

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.  

Super User
Super User
Posts: 6,500

Re: Use %Index in Macro Do loop to rename variable

[ Edited ]

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 ;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 183 views
  • 1 like
  • 5 in conversation