BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NANANANA
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

4 REPLIES 4
art297
Opal | Level 21

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

 

Reeza
Super User

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

Tom
Super User Tom
Super User

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 ;

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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