- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So if it called APP_875 I need it to be called APP_875_F and the same for all other fields.
It would be much easier to do the rename and much easier to program afterwards if you wanted the variable to be renamed to APP_F_875.
Then you can use lists in your programming such as APP_F_800-APP_F_899, and all the variable names between APP_F_800 and APP_F_899 are selected. Even if the numbers are not consecutive, but you use APP_F_875, you can refer to them all (regardless of numeric suffic) by APP_F:
In addition, with consecutive numbers, in a RENAME statement in a data step, you could use a structure such as this (copied from the documentation)
rename score1-score3=Newscore1-Newscore3;
Paige Miller