DATA Step, Macro, Functions and more

Adding a suffix to all varibles in a dataset

Reply
Super Contributor
Posts: 401

Adding a suffix to all varibles in a dataset

Hi,   I have about 700 variables, all named differently in 1 dataset... I want to add an _F to the end of each and every field in a dataset.

So if it called APP_875 I need it to be called APP_875_F and the same for all other fields.

How can this be done?.   Thanks.

Super User
Posts: 19,855

Re: Adding a suffix to all varibles in a dataset

[ Edited ]

Here's one method. If you have 700 variables the macro variable may be too long so you could consider doing multiple calls or create multiple macro variables instead and loop. 

 

Note that you should also check the lengths first to see if the variable is already greater than 30 characters, then you won't have the length you need to add the _F.

 

This example renames all variables with an _F but your situation is simpler because you're adding a suffix. 

 

/********************************************************************
Example : Rename variables based on suffix rather than prefix
********************************************************************/

data sample;
do i=10000 to 12000;
	start_date=i;
	middle_date=i+3;
	end_date=i+5;
	date_no_change=start_date;
	output;
end;
format start_date end_date middle_date date9.;
run;


proc sql noprint;
select catx("=", name, catt('DT_', tranwrd(upper(name), '_DATE', ' '))) 
into :rename_list
separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='SAMPLE'
and upper(trim(name)) like '%_DATE';
quit;


%put &rename_list;


proc datasets library=work nodetails nolist;
modify sample;
rename &rename_list;
run; quit;

proc print data=sample noobs;
run;

https://gist.githubusercontent.com/statgeek/82d9f2854edc01560e0f/raw/121d957553a84e07e92fef8f91ae967...

Super User
Posts: 10,044

Re: Adding a suffix to all varibles in a dataset

data class;
 set sashelp.class;
run;

data _null_;
 set sashelp.vcolumn(where=(libname='WORK' and memname='CLASS')) end=last;
 if _n_=1 then call execute('proc datasets library=work nolist nodetails; modify class;rename ');
 call execute(catt(name,'=',name,'_F'));
 if last then call execute(';quit;');
run;
Ask a Question
Discussion stats
  • 2 replies
  • 73 views
  • 0 likes
  • 3 in conversation