<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: rename variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846057#M334475</link>
    <description>&lt;P&gt;What are you having trouble with?&lt;/P&gt;
&lt;P&gt;Is it getting the list of variable names?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=HAVE noprint out=contents; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Is it generating the new name?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data contents;
  set contents ;
  if upcase(name)=:'P_' and substrn(name,length(name)-2)='842');
  newname = substr(name,3,length(name)-5);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Generating the pairs?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',name,newname)
  into :renames separated by ' '
  from contents
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Or applying the renames?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  rename &amp;amp;renames;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
    <pubDate>Thu, 24 Nov 2022 01:48:11 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-11-24T01:48:11Z</dc:date>
    <item>
      <title>rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846055#M334474</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sateh_0-1669252905911.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77648i3490C3AB71499042/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sateh_0-1669252905911.png" alt="sateh_0-1669252905911.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The idea is to be able to remove what I have highlighted in yellow for all the variables, there are many variables.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 01:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846055#M334474</guid>
      <dc:creator>sateh</dc:creator>
      <dc:date>2022-11-24T01:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846057#M334475</link>
      <description>&lt;P&gt;What are you having trouble with?&lt;/P&gt;
&lt;P&gt;Is it getting the list of variable names?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=HAVE noprint out=contents; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Is it generating the new name?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data contents;
  set contents ;
  if upcase(name)=:'P_' and substrn(name,length(name)-2)='842');
  newname = substr(name,3,length(name)-5);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Generating the pairs?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',name,newname)
  into :renames separated by ' '
  from contents
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Or applying the renames?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  rename &amp;amp;renames;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Thu, 24 Nov 2022 01:48:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846057#M334475</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-24T01:48:11Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846066#M334478</link>
      <description>&lt;P&gt;A slightly different approach than Tom's that uses the sashelp.vcolumn table and macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;amp;rename_list;
quit;

*check that rename worked;

proc contents data=class;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Nov 2022 02:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846066#M334478</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-11-24T02:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846164#M334523</link>
      <description>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:&lt;BR /&gt;&lt;BR /&gt;P_ldACCESOALAADMINDEJUSTIC_842 = ACCESOALAADMINDEJUSTIC</description>
      <pubDate>Thu, 24 Nov 2022 14:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846164#M334523</guid>
      <dc:creator>sateh</dc:creator>
      <dc:date>2022-11-24T14:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846167#M334526</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/369041"&gt;@sateh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;This approach has been useful for me, however, I still can't remove a certain part of the column name&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IMPORTANT: when you say something doesn't work, and you give no other details about what you did, we really can't help.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 14:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846167#M334526</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-24T14:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846175#M334527</link>
      <description>&lt;P&gt;this is my input table&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The idea is to rename the columns so that the columns remain without the word&lt;U&gt;&lt;STRONG&gt; P_ld&lt;/STRONG&gt;&lt;/U&gt; and without &lt;U&gt;&lt;STRONG&gt;_ with the number&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sateh_1-1669302188011.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77674iCA2F4E6EADFEB82C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sateh_1-1669302188011.png" alt="sateh_1-1669302188011.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I provide the code of how I am doing it as indicated &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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 &amp;amp;rename_list;
quit;&lt;/PRE&gt;&lt;P&gt;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 &lt;U&gt;&lt;STRONG&gt;Id&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sateh_0-1669302051292.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77672i82D13B0CAFD52CBD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sateh_0-1669302051292.png" alt="sateh_0-1669302051292.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 15:05:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846175#M334527</guid>
      <dc:creator>sateh</dc:creator>
      <dc:date>2022-11-24T15:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846176#M334528</link>
      <description>&lt;P&gt;Make this change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select catx (' = ', name, substr(scan(name, 2, "_"),3))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Nov 2022 15:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846176#M334528</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-24T15:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846177#M334529</link>
      <description>&lt;BR /&gt;Thank you very much this really worked for me</description>
      <pubDate>Thu, 24 Nov 2022 15:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846177#M334529</guid>
      <dc:creator>sateh</dc:creator>
      <dc:date>2022-11-24T15:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846183#M334532</link>
      <description>&lt;P&gt;Reminder: Posting photographs of text (in this case variable names) makes it hard for someone to help you.&amp;nbsp; Please post data as text so that it can be used to help you.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 15:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846183#M334532</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-24T15:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846327#M334588</link>
      <description>&lt;P&gt;I am presenting the following problem, it is bringing me only a part of the columns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how the columns of my table are, I need to eliminate what is highlighted in yellow, that is, &lt;U&gt;&lt;STRONG&gt;P_ld&lt;/STRONG&gt;&lt;/U&gt; and &lt;U&gt;&lt;STRONG&gt;_with the number&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sateh_0-1669385967561.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77705iCF133D58557842FC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sateh_0-1669385967561.png" alt="sateh_0-1669385967561.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I am using this code suggested by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; but it is giving me this result.&lt;/P&gt;&lt;PRE&gt;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;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sateh_1-1669386086133.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77706i6CC6CE2CA976650A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sateh_1-1669386086133.png" alt="sateh_1-1669386086133.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;but what I really need is for the entire word to remain example:&lt;/P&gt;&lt;PRE&gt;ACCESO_ADMIN_JUSTICIA&lt;BR /&gt;&lt;SPAN&gt;AYUDA_HUMANITARIA&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;DEBIDO_PROCESO&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;DERECHO_DE_PETICION&lt;/SPAN&gt;&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Nov 2022 14:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846327#M334588</guid>
      <dc:creator>sateh</dc:creator>
      <dc:date>2022-11-25T14:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846351#M334595</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;amp;rename_list;
quit;

*check that rename worked;

proc contents data=class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SCAN won't work because you have multiple _ in the variable name, unlike the picture you posted.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use this instead for the second part of the name, see code above.&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;substr(name,3,length(name)-5)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Nov 2022 16:32:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-variables/m-p/846351#M334595</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-11-25T16:32:58Z</dc:date>
    </item>
  </channel>
</rss>

