BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Idi
Obsidian | Level 7 Idi
Obsidian | Level 7
TesSAS

I have multiples columns starts with td_tou_ha.t_sadi , td_tou_ha.t_haba
Etc.... All the columns start with td_tou_ha.

I want to keep only names After the point (.) For example : t_sadi , t_haba etc.
Using SAS studio. Macro or SQL any help please ?

Thanks you so for your help.

I try rename fonction but its to much i want to do with macro but never use or.t
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Idi
Obsidian | Level 7 Idi
Obsidian | Level 7
Hi ,

I want to change multiple column : All the columns start with : td_ha.
After the . I have the Real name of the columns like : t_test
Td_ha.t_test = i want only t_test
I have 156 columns start with : td_ha. I just want to be After the point.
Thank you.
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Quentin
Super User

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

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
PaigeMiller
Diamond | Level 26

Well then, we seem to all be saying the same thing to @Idi . So, @Idi please show us PROC CONTENTS output for this data set.

--
Paige Miller
ballardw
Super User

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.

Tom
Super User Tom
Super User

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;

 

Idi
Obsidian | Level 7 Idi
Obsidian | Level 7
Thank you so much for your help.

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 3215 views
  • 3 likes
  • 6 in conversation