To change the name 'Td_ha.t_test'n to t_test you will need a RENAME statement
rename 'Td_ha.t_test'n = t_test;
Or perhaps the RENAME= dataset option.
set have (rename=( 'Td_ha.t_test'n = t_test));
To convert the string 'Td_ha.t_test' to the string 't_test' you can use the SUBSTR() function.
new_name = substr(name,7) ;
If your input dataset is named HAVE you can get the list of names from it by using PROC CONTENTS.
proc contents data=have out=contents noprint;
run;
You can then query that list of names and find the ones that start with that string so you can generate the old=new pairs needed for either the RENAME statement or the RENAME= dataset option.
proc sql noprint;
select catx('=',nliteral(name),nliteral(substr(name,7)))
into :renames separated by ' '
from contents
where upcase(name) eqt 'TD_HA.'
;
quit;
You can then use that macro variable to generate the rename statement.
data want;
set have;
rename &renames;
run;
I have multiples columns starts with td_tou_ha.t_sadi , td_tou_ha.t_haba
It's not clear to me what you are talking about. Column names cannot have a dot in them. Unless you are referencing them in SQL, in which case the table is referred to as td_tou_ha and the column name already is what you want it to be t_sadi. Can you please explain in more detail what it is you are talking about? It would also help for you to show us a portion of the SAS data set you are working with.
You have asked a similar question earlier. I provided a reply asking for more information. Please answer at your other thread https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-multiples-columns/td-p/890350/jump-to/fir...
Please do not ask the same question more than once. Let's keep all discussion of this issue in that one thread.
@PaigeMiller wrote:
You have asked a similar question earlier. I provided a reply asking for more information. Please answer at your other thread https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-multiples-columns/td-p/890350/jump-to/fir...
Please do not ask the same question more than once. Let's keep all discussion of this issue in that one thread.
You also asked the question on stackoverflow (https://stackoverflow.com/questions/76947350/rename-multiples-columns-startswith-and-end-with-in-sas ) and I responded with a question asking for your to run PROC CONTENTS to check the variable names. It's fair to ask the same question on different sites (once per site). When people respond with questions, you're more likely to get help if you respond in turn.
Lets see if we can get some clarity. In most circumstances . is not part of a variable name. So are you claiming that your data set contains a . as part of the variable name? If so, while possible the syntax gets a bit uglier as it means you have created variables with the system option VALIDVARNAME=any. Part of a better solution may involve changing options and bringing data into SAS again without the .
Run proc contents on your data set and show us the results with the variable names and properties to make sure we know what your actual variable names may be.
The basic tool is a RENAME statement, either in a data step or using Proc Contents. But we want to see actual names to make sure what we understand what you have.
To change the name 'Td_ha.t_test'n to t_test you will need a RENAME statement
rename 'Td_ha.t_test'n = t_test;
Or perhaps the RENAME= dataset option.
set have (rename=( 'Td_ha.t_test'n = t_test));
To convert the string 'Td_ha.t_test' to the string 't_test' you can use the SUBSTR() function.
new_name = substr(name,7) ;
If your input dataset is named HAVE you can get the list of names from it by using PROC CONTENTS.
proc contents data=have out=contents noprint;
run;
You can then query that list of names and find the ones that start with that string so you can generate the old=new pairs needed for either the RENAME statement or the RENAME= dataset option.
proc sql noprint;
select catx('=',nliteral(name),nliteral(substr(name,7)))
into :renames separated by ' '
from contents
where upcase(name) eqt 'TD_HA.'
;
quit;
You can then use that macro variable to generate the rename statement.
data want;
set have;
rename &renames;
run;
I combined your questions into one thread.
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!
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.