<?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 variables through a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742676#M232357</link>
    <description>Thank you! I had to add a line to rename the original variables whose names weren't too long, but then it worked perfectly. One further question - because it successfully renamed all the variables in the original dataset, the next time I ran the same code to work out other little bugs it read in the already renamed variables and was not happy. Is there a way to read in the original data and save the results with a new dataset name? Once all the bugs are clear this won't be a fatal issue, it just would be nice to have</description>
    <pubDate>Thu, 20 May 2021 15:05:44 GMT</pubDate>
    <dc:creator>dbjosiah</dc:creator>
    <dc:date>2021-05-20T15:05:44Z</dc:date>
    <item>
      <title>Renaming variables through a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742555#M232306</link>
      <description>&lt;P&gt;Hi, my project reads in variables from several datasets, and I need a macro that adds a prefix to each variable name identifying which dataset it came from. In this example the prefix is "CI_". A complication is that some of the original variable names are at the SAS limit of 32 characters. I've found several helpful previous posts, in particular Ksharp's response at&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Code-working-in-SAS-9-4-but-not-SAS-9-3-nvar-to/td-p/285174" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-Code-working-in-SAS-9-4-but-not-SAS-9-3-nvar-to/td-p/285174:&lt;/A&gt;&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);
 data _null_;
  set sashelp.vcolumn(where=(libname="%upcase(&amp;amp;lib)" and memname="%upcase(&amp;amp;dsn)")) end=last;
  if _n_=1 then call execute("proc datasets library=&amp;amp;lib nodetails nolist; modify &amp;amp;dsn; rename ");
  call execute(catt(name,'=&amp;amp;newname._',name));
  if last then call execute(';quit;');
 run;
%mend rename;
%rename(WORK,B,Try1)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is my version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(lib,dsn,prefix);
data _null_;
if _n_=1 then call execute("proc datasets library=&amp;amp;lib nodetails nolist; modify &amp;amp;dsn; rename ");
  set sashelp.vcolumn(where=(libname="%upcase(&amp;amp;lib)" and memname="%upcase(&amp;amp;dsn)")) end=last;
  vlength=length(name);
  if vlength&amp;gt;25 then do; /*Taking out underscores to shorten the vbl name*/
	put '1. Before: ' name= vlength=;
    name=compress(translate(trim(name),'','_'));
	put '1. After: ' name= vlength=;
  end;
   call execute(catt(name,'=&amp;amp;prefix._',name));
  if last then call execute(';quit;');
 run;
%mend rename;
%rename(WORK,case6,CI) &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This successfully writes to the log:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1. Before: _N_=1 name=_Diagnosis_Date_time__Associated vlength=32
1. After: _N_=1 name=DiagnosisDatetimeAssociated vlength=27&lt;BR /&gt;...&lt;BR /&gt;NOTE: CALL EXECUTE generated line.&lt;BR /&gt;1 + proc datasets library=WORK nodetails nolist; modify case6;&lt;BR /&gt;1 + rename&lt;BR /&gt;2 + DiagnosisDatetimeAssociated=CI_DiagnosisDatetimeAssociated&lt;BR /&gt;3 + VAR3=CI_VAR3&lt;BR /&gt;...&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but then gives an error message for the renamed variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ERROR: Variable DiagnosisDatetimeAssociated is not on file WORK.CASE6.&lt;BR /&gt;NOTE: Renaming variable VAR3 to CI_VAR3.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully it's something simple but I'm not good at manipulating Proc Datasets.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any suggestions!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 00:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742555#M232306</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-05-20T00:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables through a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742577#M232319</link>
      <description>&lt;P&gt;Works fine with a small modification. The before and after variable name should be different.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(lib,dsn,prefix);
data _null_;
if _n_=1 then call execute("proc datasets library=work nodetails nolist; modify &amp;amp;dsn; rename ");
  set sashelp.vcolumn(where=(libname="%upcase(&amp;amp;lib)" and memname="%upcase(&amp;amp;dsn)")) end=last;
  vlength=length(name);
  if vlength&amp;gt;25 then do; /*Taking out underscores to shorten the vbl name*/
	put '1. Before: ' name= vlength=;
    newname=compress(translate(trim(name),'','_'));
	put '1. After: ' newname= vlength=;
  end;
   call execute(catt(name,'=&amp;amp;prefix._',newname));
  if last then call execute(';quit;');
 run;
%mend rename;
%rename(WORK,case6,CI) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 1         + proc datasets library=work nodetails nolist; modify case6;
 1         +                                                            rename
 2         + _Diagnosis_Date_time__Associated=CI_DiagnosisDatetimeAssociated
 3         + ;
 NOTE: Renaming variable _Diagnosis_Date_time__Associated to CI_DiagnosisDatetimeAssociated.
 3         +  quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 01:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742577#M232319</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-05-20T01:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables through a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742676#M232357</link>
      <description>Thank you! I had to add a line to rename the original variables whose names weren't too long, but then it worked perfectly. One further question - because it successfully renamed all the variables in the original dataset, the next time I ran the same code to work out other little bugs it read in the already renamed variables and was not happy. Is there a way to read in the original data and save the results with a new dataset name? Once all the bugs are clear this won't be a fatal issue, it just would be nice to have</description>
      <pubDate>Thu, 20 May 2021 15:05:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742676#M232357</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-05-20T15:05:44Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables through a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742682#M232360</link>
      <description>&lt;P&gt;I would suggest that you to start&amp;nbsp; a new thread for the issues you are facing as a consequence of renaming the original variables in your existing code. Please provide some samples.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 15:37:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742682#M232360</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-05-20T15:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables through a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742686#M232361</link>
      <description>Since it's not critical, I'll do that when have a bit of free time. Thanks again for your solution!</description>
      <pubDate>Thu, 20 May 2021 15:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-through-a-macro/m-p/742686#M232361</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-05-20T15:43:03Z</dc:date>
    </item>
  </channel>
</rss>

