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 !

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 758 views
  • 0 likes
  • 2 in conversation