<?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 Problem to rename column names containing special characters in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742403#M38811</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ma trying to run a macro that replaces all spaces " " in my column names by a "_". Everything is going fine, except when column names contains some special characters such as :&amp;nbsp; "," ".", "#" , etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find attached a reproducible example. I hope you can help me to debug this issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Clement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro colrename_compress(table);
  %local rename_list sqlobs;
  proc sql noprint;
    select catx('=',nliteral(name),translate(trim(name),'_',' '))
      into :rename_list separated by ' '
    from sashelp.vcolumn
      where libname=%upcase("%scan(work.&amp;amp;table,-2,.)")
        and memname=%upcase("%scan(&amp;amp;table,-1,.)")
        and indexc(trim(name),' ')
    ;
  quit;
  %if &amp;amp;sqlobs %then %do ;
      proc datasets lib=%scan(WORK.&amp;amp;table,-2);
        modify %scan(&amp;amp;table,-1);
          rename &amp;amp;rename_list;
        run;
      quit;
  %end;
%mend colrename_compress;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data faminc;
	input 'famid 1'n 'famid .1'n faminc1-faminc11;
	cards;
1 3281 3413 3114 2500 2700 3500 3114 -999 3514 1282 2434 2818
2 4042 3084 3108 3150 -999 3100 1531 2914 3819 4124 4274 4471
3 6015 6123 6113 -999 6100 6200 6186 6132 -999 4231 6039 6215
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%colrename_compress(faminc);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 May 2021 14:11:13 GMT</pubDate>
    <dc:creator>ctisseuil</dc:creator>
    <dc:date>2021-05-19T14:11:13Z</dc:date>
    <item>
      <title>Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742403#M38811</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ma trying to run a macro that replaces all spaces " " in my column names by a "_". Everything is going fine, except when column names contains some special characters such as :&amp;nbsp; "," ".", "#" , etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find attached a reproducible example. I hope you can help me to debug this issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Clement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro colrename_compress(table);
  %local rename_list sqlobs;
  proc sql noprint;
    select catx('=',nliteral(name),translate(trim(name),'_',' '))
      into :rename_list separated by ' '
    from sashelp.vcolumn
      where libname=%upcase("%scan(work.&amp;amp;table,-2,.)")
        and memname=%upcase("%scan(&amp;amp;table,-1,.)")
        and indexc(trim(name),' ')
    ;
  quit;
  %if &amp;amp;sqlobs %then %do ;
      proc datasets lib=%scan(WORK.&amp;amp;table,-2);
        modify %scan(&amp;amp;table,-1);
          rename &amp;amp;rename_list;
        run;
      quit;
  %end;
%mend colrename_compress;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data faminc;
	input 'famid 1'n 'famid .1'n faminc1-faminc11;
	cards;
1 3281 3413 3114 2500 2700 3500 3114 -999 3514 1282 2434 2818
2 4042 3084 3108 3150 -999 3100 1531 2914 3819 4124 4274 4471
3 6015 6123 6113 -999 6100 6200 6186 6132 -999 4231 6039 6215
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%colrename_compress(faminc);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 May 2021 14:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742403#M38811</guid>
      <dc:creator>ctisseuil</dc:creator>
      <dc:date>2021-05-19T14:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742411#M38812</link>
      <description>&lt;P&gt;You are only changing the spaces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;translate(trim(name),'_',' ')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could add those other characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;translate(trim(name),'____',' #.,')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or modify it to replace all non digit non alpha characters.&amp;nbsp; Perhaps by using COMPRESS() to get the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;translate(trim(name),repeat('_',255),compress(name,,'ad'))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 May 2021 14:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742411#M38812</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-19T14:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742415#M38813</link>
      <description>&lt;P&gt;I added a compress function to keep the valid sas characters and a substr to only extract the variable name and left your translate . Seems to be working now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%macro colrename_compress(table);&lt;BR /&gt;%local rename_list sqlobs;&lt;BR /&gt;proc sql ;&lt;BR /&gt;select catx('=',nliteral(name),compress(translate(substr(name,1,length(name)),"_"," "),,'kn'))&lt;BR /&gt;into :rename_list separated by ' '&lt;BR /&gt;from sashelp.vcolumn&lt;BR /&gt;where libname=%upcase("%scan(work.&amp;amp;table,-2,.)")&lt;BR /&gt;and memname=%upcase("%scan(&amp;amp;table,-1,.)")&lt;BR /&gt;and indexc(trim(name),' ')&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;%if &amp;amp;sqlobs %then %do ;&lt;BR /&gt;proc datasets lib=%scan(WORK.&amp;amp;table,-2);&lt;BR /&gt;modify %scan(&amp;amp;table,-1);&lt;BR /&gt;rename &amp;amp;rename_list;&lt;BR /&gt;run;&lt;BR /&gt;quit;&lt;BR /&gt;%end;&lt;BR /&gt;%mend colrename_compress;&lt;/P&gt;
&lt;P&gt;data faminc;&lt;BR /&gt;input 'famid 1'n 'famid .2'n faminc1-faminc11;&lt;BR /&gt;cards;&lt;BR /&gt;1 3281 3413 3114 2500 2700 3500 3114 -999 3514 1282 2434 2818&lt;BR /&gt;2 4042 3084 3108 3150 -999 3100 1531 2914 3819 4124 4274 4471&lt;BR /&gt;3 6015 6123 6113 -999 6100 6200 6186 6132 -999 4231 6039 6215&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;options symbolgen mprint mlogic;&lt;BR /&gt;%colrename_compress(faminc);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 14:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742415#M38813</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2021-05-19T14:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742431#M38815</link>
      <description>Thanks in works perfectly</description>
      <pubDate>Wed, 19 May 2021 15:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742431#M38815</guid>
      <dc:creator>ctisseuil</dc:creator>
      <dc:date>2021-05-19T15:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742434#M38817</link>
      <description>&lt;P&gt;Oups...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your solution works fine. However, I would like to replace the space in columns names&amp;nbsp;by "_"&amp;nbsp;only, while keeping the special characters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example : replacing&amp;nbsp; 'famid 1'n and 'famid .[2'n to&amp;nbsp;'famid1'n and 'famid.[2'n.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you see what I mean ?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 15:08:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742434#M38817</guid>
      <dc:creator>ctisseuil</dc:creator>
      <dc:date>2021-05-19T15:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742439#M38819</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/360343"&gt;@ctisseuil&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Oups...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your solution works fine. However, I would like to replace the space in columns names&amp;nbsp;by "_"&amp;nbsp;only, while keeping the special characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example : replacing&amp;nbsp; 'famid 1'n and 'famid .[2'n to&amp;nbsp;'famid1'n and 'famid.[2'n.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you see what I mean ?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I can see what you think you want but do not understand any reason for it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any characters other than letter, digit or _ in a variable name means that you will have to use the name literal syntax, 'somestupid*inname'n for example, and have the system option Validvarname=any set.&lt;/P&gt;
&lt;P&gt;Personally I think you going for next to worst of two worlds, replacing one character that makes things readable and keeping characters that make things hard to read. I suspect that it won't take long to get tired of the code errors created when missing one of the quotes or the n if you have a lot of these names to use in code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are having special characters in the variable names desirable? I would suggest using standard names and assigning LABELS to the variables to make "nice" output when needed. You can make the labels much nicer text such as&lt;/P&gt;
&lt;P&gt;label famid1 ='Family ID 1'&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 15:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742439#M38819</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-19T15:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742442#M38820</link>
      <description>&lt;P&gt;Sounds like a silly idea, but in that case just include the extra NLITERAL() function call so that the string generated for the RENAME statement includes the extra quotes and letter n when the values are non standard names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   select catx('=',nliteral(name),nliteral(translate(trim(name),'_',' ')))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 May 2021 15:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742442#M38820</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-19T15:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to rename column names containing special characters</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742618#M38828</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Thanks for your answer,&lt;BR /&gt;&lt;BR /&gt;I have set the system option Validvarname=V7 and it solved my issue.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Thu, 20 May 2021 09:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Problem-to-rename-column-names-containing-special-characters/m-p/742618#M38828</guid>
      <dc:creator>ctisseuil</dc:creator>
      <dc:date>2021-05-20T09:35:41Z</dc:date>
    </item>
  </channel>
</rss>

