<?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: Prefix for all variables with macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/578242#M163954</link>
    <description>&lt;P&gt;Thanks Kurt!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What a little change to make all the difference. The uppercase is also a good tip.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ballard, thanks for the reminder. The test_ is just an example, and I have checked that none of my datasets have large variable names. I'll work in some checks for the full product, I was just trying to give the simplest example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jul 2019 23:32:20 GMT</pubDate>
    <dc:creator>jamesf</dc:creator>
    <dc:date>2019-07-31T23:32:20Z</dc:date>
    <item>
      <title>Prefix for all variables with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/577970#M163860</link>
      <description>&lt;P&gt;I need to add a prefix to all variables in several data sets, with each data set using a different prefix. I have followed examples for doing this one data set at a time. The code below works exactly as intended.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.demographics;
run;

proc sql noprint;
	select catx('=',name,catt('test_',name))
	into :rename_list
	separated by ' '
	from sashelp.vcolumn
	where libname = 'WORK' and memname = 'HAVE';
quit;

proc datasets library= work nolist;
	modify have;
	rename &amp;amp;rename_list;
quit;

proc contents data=have; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am looking to generalize this code with a macro, but the rename_list2 isnt being populated (see the put statement), and I'm not sure why. I have given the log output below the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro name_prefix(library, dataset, prefix);

	proc sql noprint;
		select catx('=',name,catt(&amp;amp;prefix,name))
		into :rename_list2
		separated by ' '
		from sashelp.vcolumn
		where libname = '&amp;amp;library' and memname = '&amp;amp;dataset';
	quit;

	%put &amp;amp;rename_list2;

	proc datasets library= &amp;amp;library nolist;
		modify &amp;amp;dataset;
		rename &amp;amp;rename_list2;
	quit;
%mend name_prefix;

data have2;
set sashelp.demographics;
run;

%name_prefix(WORK,HAVE2,'test');&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LOG MESSAGE

rename_list2
WARNING: Apparent symbolic reference RENAME_LIST2 not resolved.
NOTE: Line generated by the invoked macro "NAME_PREFIX".
26                                                                         rename &amp;amp;rename_list2;  quit;
                                                                                  _
                                                                                  22
                                                                                  76
NOTE: Enter RUN; to continue or QUIT; to end the procedure.

ERROR 22-322: Expecting a name.  

ERROR 76-322: Syntax error, statement will be ignored.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 Jul 2019 07:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/577970#M163860</guid>
      <dc:creator>jamesf</dc:creator>
      <dc:date>2019-07-31T07:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Prefix for all variables with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/577973#M163862</link>
      <description>&lt;P&gt;This is your main culprit:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where libname = '&amp;amp;library' and memname = '&amp;amp;dataset';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Macro variables will not be resolved when enclosed in single quotes. Use double quotes instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also suggest another modification by moving the quotes away from the macro call into the macro itself:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro name_prefix(library, dataset, prefix);

	proc sql noprint;
		select catx('=',name,catt("&amp;amp;prefix",name))
		into :rename_list2
		separated by ' '
		from sashelp.vcolumn
		where libname = upcase("&amp;amp;library") and memname = upcase("&amp;amp;dataset");
	quit;

	%put &amp;amp;rename_list2;

	proc datasets library= &amp;amp;library nolist;
		modify &amp;amp;dataset;
		rename &amp;amp;rename_list2;
	quit;
%mend name_prefix;

%name_prefix(WORK,HAVE2,test);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I hardened the code against lowercase library and dataset names.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 07:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/577973#M163862</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-31T07:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Prefix for all variables with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/578143#M163927</link>
      <description>&lt;P&gt;With an automated process like this you &lt;STRONG&gt;really&lt;/STRONG&gt; need to consider the lengths of your variables when combined with the prefix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are adding 4 letters with a prefix of "Test" then any variable that may have 29 or more characters will create a name that exceeds SAS name rules and will cause an error in the rename.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 16:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/578143#M163927</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-07-31T16:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Prefix for all variables with macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/578242#M163954</link>
      <description>&lt;P&gt;Thanks Kurt!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What a little change to make all the difference. The uppercase is also a good tip.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ballard, thanks for the reminder. The test_ is just an example, and I have checked that none of my datasets have large variable names. I'll work in some checks for the full product, I was just trying to give the simplest example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 23:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Prefix-for-all-variables-with-macro/m-p/578242#M163954</guid>
      <dc:creator>jamesf</dc:creator>
      <dc:date>2019-07-31T23:32:20Z</dc:date>
    </item>
  </channel>
</rss>

