BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Idi
Fluorite | Level 6 Idi
Fluorite | Level 6
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
Fluorite | Level 6 Idi
Fluorite | Level 6
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
PROC Star

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

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
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
Fluorite | Level 6 Idi
Fluorite | Level 6
Thank you so much for your help.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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