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

Hi, 

 

I am trying to build a macro to rename columns by replacing a pattern to another one. 

 

The following code seems to work when all columns names do have the matching pattern but it does not work in case some columns names do not have the matching pattern. 

 

Could you help me to improve my macro ?

 

Thanks in advance. 

 

Clement

 

%macro colrename_pattern(table, oldpattern,newpattern);
  %local rename_list sqlobs;
  proc sql noprint;
  select catx('=',nliteral(name),tranwrd(trim(name),&oldpattern,&newpattern))
    into :rename_list separated by ' '
    from sashelp.vcolumn
      where libname=%upcase("%scan(work.&table,-2,.)")
        and memname=%upcase("%scan(&table,-1,.)")
		and indexc(trim(name),&oldpattern)
    ;
  quit;
  %if &sqlobs %then %do ;
      proc datasets lib=%scan(WORK.&table,-2);
        modify %scan(&table,-1);
          rename &rename_list;
        run;
      quit;
  %end;
%mend colrename_pattern;

data faminc;
	input famid1 faminc1-faminc12;
	cards;
1 3281 3413 3114 2500 2700 3500 3114 -999 3514 1282 2434 2818
2 4042 3084 3108 3150 -999 3100 1531 2914 3819 4124 4274 4471
3 6015 6123 6113 -999 6100 6200 6186 6132 -999 4231 6039 6215
;
run;

%colrename_pattern(faminc,'famid','gfgfgf');

 

1 ACCEPTED SOLUTION
2 REPLIES 2
ctisseuil
Fluorite | Level 6
Thanks it works perfectly !

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 567 views
  • 0 likes
  • 2 in conversation