<?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: Renaming multiple variables with special characters using macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442698#M282750</link>
    <description>&lt;P&gt;Not only do you need to remove the semicolon as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;noted, but your proc sql does not generate a single variable named &amp;amp;VARS with a list of name literals.&amp;nbsp; Instead it generated a SET of macrovars named VARS1 VARS2 VARS3, etc, each with a single name literal.&amp;nbsp; As a result, you can drop one of the qscans in the loop and convert the other to scan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;num_vars.;
%qscan(&amp;amp;vars,&amp;amp;i) = %qscan(&amp;amp;newname,&amp;amp;i," ")
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you can use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;num_vars.;
  &amp;amp;&amp;amp;vars&amp;amp;i = %scan(&amp;amp;newname,&amp;amp;i," ") 
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the double &amp;amp;&amp;amp;?&amp;nbsp;&amp;nbsp;&amp;nbsp; Because macro recursively parses, the&amp;nbsp;&amp;amp;&amp;amp; become &amp;amp;, and &amp;amp;I become 1,2,3, etc., generating &amp;amp;VARS1 &amp;amp;VARS2 &amp;amp;VARS3.&amp;nbsp; Upon reparsing they become the name literals generated by proc sql.&lt;/P&gt;</description>
    <pubDate>Tue, 06 Mar 2018 01:33:41 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-03-06T01:33:41Z</dc:date>
    <item>
      <title>Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442437#M282746</link>
      <description>&lt;P&gt;&amp;nbsp;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a novice to SAS programming, but I am trying to rename a large amount of variables existing in multiple files using macro. Following is my current code. I have put the old name of variables in a list variable num_vars and newname is also a list containing the new names. The old list of variables contain names with different special characters (such as #, ', ", etc). I have gone through some posts here but they did not answered my question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the following code I have right now, I am getting an issue whenever SAS reads the statement:&amp;nbsp;&lt;SPAN&gt;%qscan(&amp;amp;vars,&amp;amp;i) = %qscan(&amp;amp;newname,&amp;amp;i," ");&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I used qscan() in an attempt to contain the special characters, but it didn't work well. Any suggestions on how I can fix the below code would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro rename(LIB,DSN,newname);
proc sql noprint;
select nliteral(name) into:vars1 -
from dictionary.columns
where libname=%upcase("&amp;amp;LIB") and memname=%upcase("&amp;amp;DSN")
order by VARNUM;
%let num_vars=&amp;amp;sqlobs;
run;

proc datasets library = &amp;amp;LIB;
modify &amp;amp;DSN;
rename
%do i = 1 %to &amp;amp;num_vars.;
%qscan(&amp;amp;vars,&amp;amp;i) = %qscan(&amp;amp;newname,&amp;amp;i," ");
%end;
run;
quit;

%mend rename;&lt;/PRE&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 17:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442437#M282746</guid>
      <dc:creator>betabr2005</dc:creator>
      <dc:date>2018-03-05T17:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442438#M282747</link>
      <description>&lt;P&gt;Have you considered setting&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Option validvarname=V7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will limit the types of names that will be created. This assumes you're importing the data set.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 17:23:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442438#M282747</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-05T17:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442441#M282748</link>
      <description>&lt;P&gt;Reeza, thank you for your reply. We actually need to those variables in order to rename them. If we limit or strip them out before we import, we lose the information we need in order to rename. And also, it is over 200 of those.&lt;/P&gt;&lt;P&gt;Betabr2005&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 17:35:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442441#M282748</guid>
      <dc:creator>betabr2005</dc:creator>
      <dc:date>2018-03-05T17:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442446#M282749</link>
      <description>&lt;P&gt;I don't like macro language as the approach here, mainly because as someone new to SAS you need something that forces you to picture what your program looks like once macro language has done its duty.&amp;nbsp; However, back to the problem at hand, the semicolon needs to be moved:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;rename&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;%do i = 1 %to &amp;amp;num_vars.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %qscan(&amp;amp;vars,&amp;amp;i) = %qscan(&amp;amp;newname,&amp;amp;i," ")&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;%end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0000" face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Mar 2018 17:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442446#M282749</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-05T17:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442698#M282750</link>
      <description>&lt;P&gt;Not only do you need to remove the semicolon as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;noted, but your proc sql does not generate a single variable named &amp;amp;VARS with a list of name literals.&amp;nbsp; Instead it generated a SET of macrovars named VARS1 VARS2 VARS3, etc, each with a single name literal.&amp;nbsp; As a result, you can drop one of the qscans in the loop and convert the other to scan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;num_vars.;
%qscan(&amp;amp;vars,&amp;amp;i) = %qscan(&amp;amp;newname,&amp;amp;i," ")
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you can use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to &amp;amp;num_vars.;
  &amp;amp;&amp;amp;vars&amp;amp;i = %scan(&amp;amp;newname,&amp;amp;i," ") 
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the double &amp;amp;&amp;amp;?&amp;nbsp;&amp;nbsp;&amp;nbsp; Because macro recursively parses, the&amp;nbsp;&amp;amp;&amp;amp; become &amp;amp;, and &amp;amp;I become 1,2,3, etc., generating &amp;amp;VARS1 &amp;amp;VARS2 &amp;amp;VARS3.&amp;nbsp; Upon reparsing they become the name literals generated by proc sql.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 01:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442698#M282750</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-06T01:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442705#M282751</link>
      <description>&lt;P&gt;Using macros to handle special characters will just cause confusion if you have no experience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stick to the data step. Something like this should be easy to adapt:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data T;
  'kjklj.nb bn'n = 'A';
  '14&amp;amp;a%ba"dsf'n = 'B';
run;
 
data _null_;
  set T (obs=1);
  length VAR_NAME $32;
  call execute('proc datasets noprint; modify T; rename');
  do while(1);
    call vnext(VAR_NAME);
    if VAR_NAME='VAR_NAME' then leave;
    call execute(catt(quote(trim(VAR_NAME),"'"),'n=_',translate(trim(VAR_NAME),'__________________________','`~!@#$%^&amp;amp;*()_-=+''".&amp;gt;,&amp;lt;;: ')));
  end;
  call execute('; quit;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;NOTE: Renaming variable 'kjklj.nb bn'n to _kjklj_nb_bn.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;NOTE: Renaming variable '14&amp;amp;a%ba"dsf'n to _14_a_ba_dsf.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;NOTE: MODIFY was successful for WORK.T.DATA.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 01:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442705#M282751</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-03-06T01:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442856#M282752</link>
      <description>&lt;P&gt;Mkeintz, thank you for the information. It removed the error I was getting but it did not rename the variables. The old names still remain. Before asking another question, I think I will go back to the books and investigate further what the issue is.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 12:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442856#M282752</guid>
      <dc:creator>betabr2005</dc:creator>
      <dc:date>2018-03-06T12:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442857#M282753</link>
      <description>&lt;P&gt;ChrisNZ, thank you for the different approach on the problem. I will try this one as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 12:44:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442857#M282753</guid>
      <dc:creator>betabr2005</dc:creator>
      <dc:date>2018-03-06T12:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442859#M282754</link>
      <description>&lt;P&gt;I want to thank you all for taking the time to respond to my question and also for the patience with a newbie while going through the learning curve.&lt;/P&gt;&lt;P&gt;Greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 12:48:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442859#M282754</guid>
      <dc:creator>betabr2005</dc:creator>
      <dc:date>2018-03-06T12:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442955#M282755</link>
      <description>&lt;P&gt;Don't introduce macro quoting where you don't need it.&amp;nbsp; It will confuse you and worse it can confuse SAS in some cases.&amp;nbsp; Change your use of %Qscan() to normal %scan().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not let PROC SQL handle generating the proper syntax for the RENAME statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(LIB,DSN,newname);
%local varlist ;
proc sql noprint;
select catx('=',nliteral(name),scan("&amp;amp;newname",varnum,' '))
  into :varlist separated by ' '
from dictionary.columns
where libname=%upcase("&amp;amp;LIB")
  and memname=%upcase("&amp;amp;DSN")
  and varnum &amp;lt;= countw("&amp;amp;newname",' ')
;
quit;

proc datasets library = &amp;amp;LIB nolist;
  modify &amp;amp;DSN;
  rename &amp;amp;varlist ;
  run;
quit;

%mend rename;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's try it out.&lt;/P&gt;
&lt;PRE&gt;1313  options validvarname=any mprint ;
1314  data class ;
1315   set sashelp.class ;
1316   rename name='First Name'n Sex='Student Gender'n ;
1317  run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.

1318  %rename(work,class,Name Sex);
MPRINT(RENAME):   proc sql noprint;
MPRINT(RENAME):   select catx('=',nliteral(name),scan("Name Sex",varnum,' ')) into :varlist separated by '
' from dictionary.columns where libname="WORK" and memname="CLASS" and varnum &amp;lt;= countw("Name Sex",' ') ;
MPRINT(RENAME):   quit;

MPRINT(RENAME):   proc datasets library = work nolist;
MPRINT(RENAME):   modify class;
MPRINT(RENAME):   rename "First Name"N=Name "Student Gender"N=Sex ;
NOTE: Renaming variable 'First Name'n to Name.
NOTE: Renaming variable 'Student Gender'n to Sex.
MPRINT(RENAME):   run;
NOTE: MODIFY was successful for WORK.CLASS.DATA.
MPRINT(RENAME):   quit;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Mar 2018 16:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/442955#M282755</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-06T16:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variables with special characters using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/444049#M282756</link>
      <description>&lt;P&gt;Thank you, Tom. It did the trick. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; This is great!&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 11:00:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variables-with-special-characters-using-macro/m-p/444049#M282756</guid>
      <dc:creator>betabr2005</dc:creator>
      <dc:date>2018-03-09T11:00:52Z</dc:date>
    </item>
  </channel>
</rss>

