BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sateh
Fluorite | Level 6

I have a table that has many columns and I need to remove the word P_ld from those columns and the word that follows after the last _ with the number, I have tried to do it but it has not been possible, attached image of my table.

 

sateh_0-1669252905911.png

The idea is to be able to remove what I have highlighted in yellow for all the variables, there are many variables.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Make this change

 

select catx (' = ', name, substr(scan(name, 2, "_"),3))
--
Paige Miller

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

What are you having trouble with?

Is it getting the list of variable names?

Spoiler
proc contents data=HAVE noprint out=contents; run;

Is it generating the new name?

Spoiler
data contents;
  set contents ;
  if upcase(name)=:'P_' and substrn(name,length(name)-2)='842');
  newname = substr(name,3,length(name)-5);
run;

Generating the pairs?

Spoiler
proc sql noprint;
select catx('=',name,newname)
  into :renames separated by ' '
  from contents
;
quit;

Or applying the renames?

Spoiler
data want;
  set have;
  rename &renames;
run;
Reeza
Super User

A slightly different approach than Tom's that uses the sashelp.vcolumn table and macro variables. 

 

data class;
	set sashelp.class;
	rename weight=p_random1_3343 height=p_random2_343 age=p_random3_34343;
run;

*create renaming list;

proc sql noprint;
	select catx (' = ', name, scan(name, 2, "_")) into :rename_list separated by 
		" " from sashelp.vcolumn where libname='WORK' and memname='CLASS' and 
		upcase(name) like 'P_%';
quit;

*rename variables;

proc datasets lib=work nodetails nolist;
	modify class;
	rename &rename_list;
quit;

*check that rename worked;

proc contents data=class;
run;
sateh
Fluorite | Level 6
This approach has been useful for me, however, I still can't remove a certain part of the column name, if you look at the image, the column names have a prefix called P_ld and then the name follows, so with that approach that you shared with me, I can remove the P_ and _ with numbers, but the idea is that it should look like this, for example:

P_ldACCESOALAADMINDEJUSTIC_842 = ACCESOALAADMINDEJUSTIC
PaigeMiller
Diamond | Level 26

@sateh wrote:
This approach has been useful for me, however, I still can't remove a certain part of the column name

Please give details when something doesn't work. Please show us the code you used. If there are errors in the LOG, also show us the log. If the end result is not what you expect, show us the end result.

 

IMPORTANT: when you say something doesn't work, and you give no other details about what you did, we really can't help.

--
Paige Miller
sateh
Fluorite | Level 6

this is my input table


The idea is to rename the columns so that the columns remain without the word P_ld and without _ with the number

sateh_1-1669302188011.png

 

Here I provide the code of how I am doing it as indicated @Reeza 

 

proc sql noprint;
	select catx (' = ', name, scan(name, 2, "_")) into :rename_list separated by 
		" " from sashelp.vcolumn where libname='WORK' and memname='RESULTADOS_VAR_CLEAN' and 
		upcase(name) like 'P_%';
quit;


*rename variables;

proc datasets lib=work nodetails nolist;
	modify resultados_var_clean;
	rename &rename_list;
quit;

and it works, this is the result, but if you look at the name of the variables I need to also remove what is highlighted in yellow, that is, the word Id

 

sateh_0-1669302051292.png

 

PaigeMiller
Diamond | Level 26

Make this change

 

select catx (' = ', name, substr(scan(name, 2, "_"),3))
--
Paige Miller
sateh
Fluorite | Level 6

Thank you very much this really worked for me
Tom
Super User Tom
Super User

Reminder: Posting photographs of text (in this case variable names) makes it hard for someone to help you.  Please post data as text so that it can be used to help you.

sateh
Fluorite | Level 6

I am presenting the following problem, it is bringing me only a part of the columns

 

This is how the columns of my table are, I need to eliminate what is highlighted in yellow, that is, P_ld and _with the number

 

sateh_0-1669385967561.png

I am using this code suggested by @Reeza but it is giving me this result.

proc sql noprint;
	select catx (' = ', name, substr(scan(name, 2, "_"),3)) into :rename_list separated by 
		" " from sashelp.vcolumn where libname='WORK' and memname='RESULTADOS_VAR_CLEAN' and 
		upcase(name) like 'P_%';
quit;

sateh_1-1669386086133.png

but what I really need is for the entire word to remain example:

ACCESO_ADMIN_JUSTICIA
AYUDA_HUMANITARIA
DEBIDO_PROCESO
DERECHO_DE_PETICION
.
.
.
.

 

Reeza
Super User
data class;
	set sashelp.class;
	rename weight=p_random1_3343 height=p_random2_343 age=p_random3_34343;
run;

*create renaming list;

proc sql noprint;
	select catx (' = ', name, substr(name,3,length(name)-5)) into :rename_list separated by 
		" " from sashelp.vcolumn where libname='WORK' and memname='CLASS' and 
		upcase(name) like 'P_%';
quit;

*rename variables;

proc datasets lib=work nodetails nolist;
	modify class;
	rename &rename_list;
quit;

*check that rename worked;

proc contents data=class;
run;

The SCAN won't work because you have multiple _ in the variable name, unlike the picture you posted. 

 

Use this instead for the second part of the name, see code above.

substr(name,3,length(name)-5)

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2045 views
  • 2 likes
  • 4 in conversation