<?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 macro to rename variables based on iteration in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670911#M201441</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have 3 datasets named S1 S2 S3 in the default work directory[i.e. produced from the last executed step], which should be used in a merge statement and the variable names should be renamed based on the numeric they are carrying.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create a macro for this process. These input datasets(S1 S2 S3) can change to anything(eg:outk1 outk2 outk3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without macro i've wrote the below code, would like to do it with the help of macro so that later based on the inputs(like s1 s2 s3 ) it will do my job&amp;nbsp; accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;finalS;&lt;/P&gt;
&lt;P&gt;merge s1(rename=(var_a=a1 var_b=b1 var_c=c1)&lt;/P&gt;
&lt;P&gt;s2(rename=(var_a=a2 var_b=b2 var_c=c2))&lt;/P&gt;
&lt;P&gt;s3(rename=(var_a=a3 var_b=b3 var_c=c3))&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any thoughts, comments?&lt;/P&gt;</description>
    <pubDate>Tue, 21 Jul 2020 09:57:41 GMT</pubDate>
    <dc:creator>sahoositaram555</dc:creator>
    <dc:date>2020-07-21T09:57:41Z</dc:date>
    <item>
      <title>macro to rename variables based on iteration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670911#M201441</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have 3 datasets named S1 S2 S3 in the default work directory[i.e. produced from the last executed step], which should be used in a merge statement and the variable names should be renamed based on the numeric they are carrying.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create a macro for this process. These input datasets(S1 S2 S3) can change to anything(eg:outk1 outk2 outk3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without macro i've wrote the below code, would like to do it with the help of macro so that later based on the inputs(like s1 s2 s3 ) it will do my job&amp;nbsp; accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;finalS;&lt;/P&gt;
&lt;P&gt;merge s1(rename=(var_a=a1 var_b=b1 var_c=c1)&lt;/P&gt;
&lt;P&gt;s2(rename=(var_a=a2 var_b=b2 var_c=c2))&lt;/P&gt;
&lt;P&gt;s3(rename=(var_a=a3 var_b=b3 var_c=c3))&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any thoughts, comments?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 09:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670911#M201441</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-21T09:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: macro to rename variables based on iteration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670913#M201442</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If i understand you correctly :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro merge(dsname);

    data final&amp;amp;dsname.;
        merge %do i=1 %to 3;
            &amp;amp;dsname.&amp;amp;i. (rename=(var_a=a&amp;amp;i. var_b=b&amp;amp;i. var_c=c&amp;amp;i.))
        %end;
        ;
    run;

%mend;

%merge(S)

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note : untested.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 10:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670913#M201442</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-07-21T10:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: macro to rename variables based on iteration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670919#M201444</link>
      <description>&lt;P&gt;This macro accepts parameters that specify data set name prefixes and variable name prefixes and number of each thereof.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want a more abstract macro, say one that examines the members of a library and discovers the common names of the variables therein, you will need a more complex process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;%macro enwidening_rename(data=, n=, var=, m=);

  %local i j letter;

  %do i = 1 %to &amp;amp;n;

    &amp;amp;data.&amp;amp;i ( rename = (

    %do j = 1 %to &amp;amp;m;

      %let letter = %sysfunc(byte(64+&amp;amp;j));

      &amp;amp;var._&amp;amp;letter = &amp;amp;letter.&amp;amp;i

    %end;

    ))

  %end;

%mend;


data s1 s2 s3;
  var_a = 010; var_b = 020; var_c = 030; output s1;
  var_a = 110; var_b = 120; var_c = 130; output s2;
  var_a = 210; var_b = 220; var_c = 230; output s3;
run;


options mprint;

data all;
  merge
    %enwidening_rename(data=s, n=3, var=var, m=3)
  ;

  * no by statement ?, be careful and certain;
run;
&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RichardADeVenezia_0-1595329516615.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47389i8B2C57FF067DCC31/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RichardADeVenezia_0-1595329516615.png" alt="RichardADeVenezia_0-1595329516615.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 11:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-rename-variables-based-on-iteration/m-p/670919#M201444</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-07-21T11:06:28Z</dc:date>
    </item>
  </channel>
</rss>

