<?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: Use %Index in Macro Do loop to rename variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372767#M276120</link>
    <description>&lt;P&gt;There is no need add quotes around string literals in macro code. &amp;nbsp;Everything is a string to macro code. &amp;nbsp;Also if you want to use %INDEX() to test if a string begins with specific characters then you need to worry both about the case of the string and the position that the characters are found. &amp;nbsp;So to find names that start with 'ID_' you could use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF %index(%upcase(&amp;amp;&amp;amp;var&amp;amp;i),ID_) = 1 %THEN  ....;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in reality you can take the testing of the variable name out of macro logic and put it into regular SAS logic where it is easier to code and to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is version of your macro that greatly simplies the metadata query to get the list of variables that begin with 'ID_'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(lib,dsn,prefix);
%local i nvar ;
proc sql noprint ;
select name into :var1-
from dictionary.columns
where libname=%upcase("&amp;amp;LIB")
  and memname=%upcase("&amp;amp;DSN")
  and upcase(name) like 'ID^_%' escape '^'
;
%let nvar=&amp;amp;sqlobs;
quit;

%if (&amp;amp;nvar) %then %do;
proc datasets lib=&amp;amp;lib nolist ;
  modify &amp;amp;dsn ;
  rename
%do i=1 %to &amp;amp;sqlobs ;
  &amp;amp;&amp;amp;var&amp;amp;i = &amp;amp;prefix._&amp;amp;&amp;amp;var&amp;amp;i
%end;
  ;
run; quit;
%end;
%mend rename ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 03 Jul 2017 15:39:28 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-07-03T15:39:28Z</dc:date>
    <item>
      <title>Use %Index in Macro Do loop to rename variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372646#M276116</link>
      <description>&lt;P&gt;Hello, SAS experts!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here is my code:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; one;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;A=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;ID_U=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;ID_V=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;Run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;/* Running the renaming macro */&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;options&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; macrogen &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;mprint&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;mlogic&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; rename(lib,dsn,prefixtarget);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;select nvar into :num_vars&lt;/P&gt;&lt;P&gt;from dictionary.tables&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;where libname=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"&amp;amp;LIB"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; and&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;memname=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"&amp;amp;DSN"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;select distinct(name) into :var1-&lt;/P&gt;&lt;P&gt;:var%&lt;STRONG&gt;&lt;I&gt;TRIM&lt;/I&gt;&lt;/STRONG&gt;(%&lt;STRONG&gt;&lt;I&gt;LEFT&lt;/I&gt;&lt;/STRONG&gt;(&amp;amp;num_vars))&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;where libname=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"&amp;amp;LIB"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; and&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;memname=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"&amp;amp;DSN"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;proc datasets library=&amp;amp;LIB;&lt;/P&gt;&lt;P&gt;modify &amp;amp;DSN;&lt;/P&gt;&lt;P&gt;rename&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%do&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; i=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%to&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;num_vars;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%PUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;&amp;amp;VAR&amp;amp;i; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%IF&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%index&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;(&amp;amp;&amp;amp;var&amp;amp;i,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'ID'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) GT &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%THEN&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; &amp;amp;&amp;amp;var&amp;amp;i=&amp;amp;prefixtarget._&amp;amp;&amp;amp;var&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; rename;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;rename&lt;/I&gt;&lt;/STRONG&gt;(WORK,ONE,CAL);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;So my goal is to rename the variables based on whether the variables name contains a specific text expression(in this case, "ID"). So the final result I expect is: &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;A&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;CAL_ID_U&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;CAL_ID_V&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;I have tried many different ways, but none of them work. Can you please tell me where goes wrong? &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;Thanks!&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 04:38:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372646#M276116</guid>
      <dc:creator>NANANANA</dc:creator>
      <dc:date>2017-07-03T04:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: Use %Index in Macro Do loop to rename variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372649#M276117</link>
      <description>&lt;P&gt;Your macro will work correctly with the following changes:&lt;/P&gt;
&lt;PRE&gt;Data one;
A=0;
ID_U=1;
ID_V=2;
Run;

/* Running the renaming macro */
options macrogen mprint mlogic;
%macro rename(lib,dsn,prefixtarget);
 
/*ADD*/ proc sql noprint;
select nvar into :num_vars
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"
;

select distinct(name) into :var1-
:var%TRIM(%LEFT(&amp;amp;num_vars))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"
;
quit;

proc datasets library=&amp;amp;LIB;
modify &amp;amp;DSN;
rename
%do i=1 %to &amp;amp;num_vars;

%PUT &amp;amp;&amp;amp;VAR&amp;amp;i;
/* remove quotes around ID */
%IF %index(&amp;amp;&amp;amp;var&amp;amp;i,ID) GT 0 %THEN &amp;amp;&amp;amp;var&amp;amp;i=&amp;amp;prefixtarget._&amp;amp;&amp;amp;var&amp;amp;i.;
%end;
;
run;
quit;
/*not needed*/  /*run;*/

%mend rename;
%rename(WORK,ONE,CAL);
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 05:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372649#M276117</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-03T05:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Use %Index in Macro Do loop to rename variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372650#M276118</link>
      <description>&lt;P&gt;Are you running this for datasets with a large number of variables? Over 1000?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not, consider creating one macro variable in the rename.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/82d9f2854edc01560e0f" target="_blank"&gt;https://gist.github.com/statgeek/82d9f2854edc01560e0f&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 05:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372650#M276118</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-03T05:29:45Z</dc:date>
    </item>
    <item>
      <title>Re: Use %Index in Macro Do loop to rename variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372669#M276119</link>
      <description>&lt;P&gt;A much simpler method:&lt;/P&gt;
&lt;PRE&gt;%let lib=work;
%let dsn=abc;
%let pf=cal;

data _null_;
  set sashelp.vcolumn (where=(libname="&amp;amp;LIB." and memname="&amp;amp;DSN." and index(name,"ID") &amp;gt; 0)) end=last;
  if _n_=1 then call execute('proc datasets lib=work; modify abc;');
  call execute(catx(' ','rename',name,"=&amp;amp;PF._",name,';'));
  if last then call execute('run; quit;');
run;&lt;/PRE&gt;
&lt;P&gt;Do note however that if you go over the given length of SAS names, or include characters not allowed etc. any code will not work. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 08:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372669#M276119</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-03T08:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: Use %Index in Macro Do loop to rename variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372767#M276120</link>
      <description>&lt;P&gt;There is no need add quotes around string literals in macro code. &amp;nbsp;Everything is a string to macro code. &amp;nbsp;Also if you want to use %INDEX() to test if a string begins with specific characters then you need to worry both about the case of the string and the position that the characters are found. &amp;nbsp;So to find names that start with 'ID_' you could use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF %index(%upcase(&amp;amp;&amp;amp;var&amp;amp;i),ID_) = 1 %THEN  ....;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in reality you can take the testing of the variable name out of macro logic and put it into regular SAS logic where it is easier to code and to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is version of your macro that greatly simplies the metadata query to get the list of variables that begin with 'ID_'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(lib,dsn,prefix);
%local i nvar ;
proc sql noprint ;
select name into :var1-
from dictionary.columns
where libname=%upcase("&amp;amp;LIB")
  and memname=%upcase("&amp;amp;DSN")
  and upcase(name) like 'ID^_%' escape '^'
;
%let nvar=&amp;amp;sqlobs;
quit;

%if (&amp;amp;nvar) %then %do;
proc datasets lib=&amp;amp;lib nolist ;
  modify &amp;amp;dsn ;
  rename
%do i=1 %to &amp;amp;sqlobs ;
  &amp;amp;&amp;amp;var&amp;amp;i = &amp;amp;prefix._&amp;amp;&amp;amp;var&amp;amp;i
%end;
  ;
run; quit;
%end;
%mend rename ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jul 2017 15:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Index-in-Macro-Do-loop-to-rename-variable/m-p/372767#M276120</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-03T15:39:28Z</dc:date>
    </item>
  </channel>
</rss>

