BookmarkSubscribeRSS Feed
podarum
Quartz | Level 8

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.

4 REPLIES 4
Reeza
Super User

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...

Ksharp
Super User
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;
docgryerson
Calcite | Level 5
Much cleaner solution. I prefer this over the dictionary table solution, although both work just fine.
PaigeMiller
Diamond | Level 26

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10284 views
  • 2 likes
  • 5 in conversation