<?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: rename multiple variables with a series of numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611548#M178246</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
    set have;
    length _NAME_ $32;

    call execute('data want; set have; rename');

    do while(_NAME_ ne "_NAME_");
        call vnext(_NAME_);
        if substr(_NAME_,1,3)="ID_" then do;
            i+1;
            call execute(cats(_NAME_,'=ID_',i));
        end;
    end;
    call execute('; run;');
    stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 13 Dec 2019 10:59:05 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2019-12-13T10:59:05Z</dc:date>
    <item>
      <title>rename multiple variables with a series of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611510#M178230</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with dummy variables created for different IDs and Time (using proc glmmod). I have more than 1000 IDs. I want to rename all the columns of ID dummy variables from "&lt;CODE class=" language-sas"&gt;ID_10001 ID_10002 ID_10006 ID_10010 ID_11114&lt;/CODE&gt;"&amp;nbsp; to&amp;nbsp; "&lt;CODE class=" language-sas"&gt;ID1 ID2 ID3 ID4 ID5&lt;/CODE&gt;" and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking for a code that can help me achieving this purpose.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input  Y X1 X2 ID_10001 ID_10002 ID_10006 ID_10010 ID_11114  Year_2005 Year_2006 Year_2007 ;
datalines;

1010 100 0 1 0 0 0 0 1 0 0
1022 250 0 1 0 0 0 0 0 0 1
1134 180 0 0 1 0 0 0 0 0 1
1014 450 1 0 0 1 0 0 1 0 0
2526 450 1 0 0 1 0 0 0 1 0
1014 300 0 0 0 1 0 0 0 0 1
4012 999 0 0 0 0 1 0 1 0 0
3179 850 1 0 0 0 1 0 0 1 0
1012 720 0 0 0 0 1 0 0 0 1
3175 800 0 0 0 0 0 1 1 0 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 04:22:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611510#M178230</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2019-12-13T04:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: rename multiple variables with a series of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611512#M178232</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi, Saba1&lt;/P&gt;&lt;P&gt;try this code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  dsid=open('work.have','I');
  if dsid&amp;gt;0 then do;
    vars=attrn(dsid,'nvars');
    do i=1 to vars;
      if find(varname(dsid,i),'ID')=1 then do;
        j+1; 
        call symputx(cats('vnm_old',j),varname(dsid,i));
        call symputx(cats('vnm_new',j),cats('ID',j));
      end;
    end;
    call symputx('renamevars',j);
    rc=close(dsid);
  end;
run;

%macro Mrename;
  data renamed;
    set have;
    %do i=1 %to &amp;amp;renamevars.;
      rename &amp;amp;&amp;amp;vnm_old&amp;amp;i. = &amp;amp;&amp;amp;vnm_new&amp;amp;i.;
    %end;
  run;
%Mend Mrename;

%Mrename;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 04:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611512#M178232</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2019-12-13T04:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: rename multiple variables with a series of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611516#M178234</link>
      <description>&lt;P&gt;I modified to rename multiple variables.&lt;/P&gt;&lt;P&gt;Check below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro Mrename(ds,prefix);
  data _null_;
    call symputx('vcnt',count("&amp;amp;prefix",' ')+1);
  run;
  %do i=1 %to &amp;amp;vcnt;
    %let prefix&amp;amp;i=%scan(&amp;amp;prefix,&amp;amp;i,' ');
  %end;

  data _null_;
    dsid=open("&amp;amp;ds.",'I');
    array varcnt{&amp;amp;vcnt};
    if dsid&amp;gt;0 then do;
      vars=attrn(dsid,'nvars');
      do i=1 to vars;
        %do k=1 %to &amp;amp;vcnt;
          if find(upcase(varname(dsid,i)),upcase("&amp;amp;&amp;amp;prefix&amp;amp;k"))=1 then do;
            j+1;
            varcnt{&amp;amp;k}+1;
            call symputx(cats('vnm_old',j),varname(dsid,i));
            call symputx(cats('vnm_new',j),cats("&amp;amp;&amp;amp;prefix&amp;amp;k",varcnt{&amp;amp;k}));
          end;
        %end;
      end;
      call symputx('renamevars',j);
      rc=close(dsid);
    end;
  run;

  data &amp;amp;ds._renamed;
    set &amp;amp;ds.;
    %do i=1 %to &amp;amp;renamevars.;
      rename &amp;amp;&amp;amp;vnm_old&amp;amp;i. = &amp;amp;&amp;amp;vnm_new&amp;amp;i.;
    %end;
  run;
%Mend Mrename;

/* Specify words to rename separated by spaces. */
%Mrename(work.have,ID YEAR);
&lt;BR /&gt;/* You can control prefix to uppercase or lowercase. */
/* Both are the same...  =)                          */
/*%Mrename(work.have,id year);*/

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 06:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611516#M178234</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2019-12-13T06:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: rename multiple variables with a series of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611526#M178237</link>
      <description>&lt;P&gt;Can be solved by using sashelp.vcolumn and call execute:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set sashelp.vcolumn(where=(libname = 'WORK' and memname = 'HAVE' and name like 'ID%')) end=jobDone;
   
   if _n_ = 1 then do;
      call execute('proc datasets library=work nolist; modify have; rename ');
   end;

   number + 1;
   call execute(cats(Name, '=', cats('ID', number)));

   if jobDone then do;
      call execute(';quit;');
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 07:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611526#M178237</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-12-13T07:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: rename multiple variables with a series of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611548#M178246</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
    set have;
    length _NAME_ $32;

    call execute('data want; set have; rename');

    do while(_NAME_ ne "_NAME_");
        call vnext(_NAME_);
        if substr(_NAME_,1,3)="ID_" then do;
            i+1;
            call execute(cats(_NAME_,'=ID_',i));
        end;
    end;
    call execute('; run;');
    stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 10:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611548#M178246</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-12-13T10:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: rename multiple variables with a series of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611960#M178475</link>
      <description>Thank you. It is really helpful.</description>
      <pubDate>Mon, 16 Dec 2019 06:22:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-multiple-variables-with-a-series-of-numbers/m-p/611960#M178475</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2019-12-16T06:22:03Z</dc:date>
    </item>
  </channel>
</rss>

