Use the RENAME statement or RENAME= dataset option.
To get the list of old=new name pairs you will need to do some code generation.
If the number of variables is small enough you could build a single macro variable. For example by querying the DICTIONARY.COLUMNS metadata. Remember that LIBNAME and MEMNAME values are always in uppercase in that metadata.
proc sql noprint;
select catx('=',nliteral(name),nliteral(cats('prefix',name)))
into :renames separated by ' '
from dictionary.columns
where libname='MYLIB' and memname='MYDS'
;
quit;
Then you could use this RENAMES macro variable to generate the RENAME statement.
For example by using PROC DATASETS to modify the existing dataset.
proc datasets lib=mylib nolist;
modify myds ;
rename &renames;
run;
quit;
Or use it in a data step. Where you could also try using the RENAME= dataset option instead.
data want;
set mylib.myds(rename=(&renames));
run;
Use the RENAME statement or RENAME= dataset option.
To get the list of old=new name pairs you will need to do some code generation.
If the number of variables is small enough you could build a single macro variable. For example by querying the DICTIONARY.COLUMNS metadata. Remember that LIBNAME and MEMNAME values are always in uppercase in that metadata.
proc sql noprint;
select catx('=',nliteral(name),nliteral(cats('prefix',name)))
into :renames separated by ' '
from dictionary.columns
where libname='MYLIB' and memname='MYDS'
;
quit;
Then you could use this RENAMES macro variable to generate the RENAME statement.
For example by using PROC DATASETS to modify the existing dataset.
proc datasets lib=mylib nolist;
modify myds ;
rename &renames;
run;
quit;
Or use it in a data step. Where you could also try using the RENAME= dataset option instead.
data want;
set mylib.myds(rename=(&renames));
run;
Why do you want a prefix added?
If you describe the use case we may come up with alternatives that may not require such.
Hint: if the purpose is to then add these variables to another data set and the prefix identifies a different set quite often that is not an optimal choice.
Your extremely brief problem description leaves me wondering if you want to add the prefix to the variable NAMES, or a prefix to the variable VALUES. You don't tell us. Which is it?
Also, if you are talking about variable NAMES, this could be a symptom of poor data layout or poor data arrangements, and it might be that re-arranging your data will eliminate the need to change variable names. Specifically, people create wide data sets with lots of somewhat repetitive variable names, except for a prefix or suffix; while a long data set eliminates the need for lots of variable names and eliminates the need for bulk renaming.
Which brings us to the main point I want to make. We need you to help us, which in turn helps you. We need much more detailed problem statement, and we need to know WHY you are doing this; once we know this we can come up with (possibly) a better approach that is easier to program, and get you to your final destination a lot faster than renaming would get you there. Please also take a few minutes and read up on the XY Problem as I think this is important and relevant in this case, and really in all cases; we need to know what you are doing and WHY you are doing it in all future posts as well.
The goal is to add a prefix to the field names itself. Not to alter the values of any given field.
@aamoen wrote:
The goal is to add a prefix to the field names itself. Not to alter the values of any given field.
WHY? What is the ultimate goal of this project/analysis/data manipulation? Please describe the project.
The macro in this thread will allow you add a prefix to each name and much more.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.