<?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: Dynamically rename a bunch of vriables into new variables: Y1, Y2.... in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591333#M15198</link>
    <description>There's no mention here of any input or output data set.  So if you can get the program to work, what would its results be?  Would it be a one-time application or would it need to be used with different values for &amp;amp;varlist?</description>
    <pubDate>Tue, 24 Sep 2019 23:29:13 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-09-24T23:29:13Z</dc:date>
    <item>
      <title>Dynamically rename a bunch of vriables into new variables: Y1, Y2....</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591328#M15195</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;&lt;P&gt;I am trying to rename a bunch of old variables&amp;nbsp; as Y1, Y2, Y3, …. but running into trouble. See code below. Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Harrison:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%let varlist=&lt;BR /&gt;A0A075B6K4 A0A075B6K5 A2NJV5 O14562 O96009 P00390 P01742 P01834 P01892 P02655 P02751 P02765 P02786 P02787 P02790 P09467 P11686 P16152 P18065 P20039 P30405 P30838 P35247 P43652 P49006 P50238 P61326 P63151 P02768_1 P06681_1 P14678_1 P22897_1 P23246_1 P29279_1 P51159_1 P61201_1 Q02790 Q02818 Q03252 Q14956 Q16401 Q99758 Q7Z2W4 Q8IWL2_1 Q8N9N7 Q96AG4 Q96IU4 Q96PD5 Q96S96 Q9BPX5 Q9BQ61 Q9BUL8 Q9BWS9_1 Q9H1Z4 Q9NQ79 Q9P2T1_1 Q9UGT4 Q9Y3E1;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;length old $32 rename $4;&lt;BR /&gt;do i=1 to countw("&amp;amp;varlist");&lt;BR /&gt;old=scan("&amp;amp;varlist",i);&lt;BR /&gt;rename &amp;amp;old = Y&amp;amp;i;&lt;BR /&gt;&lt;BR /&gt;end;&lt;BR /&gt;call symputx('rename',rename);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2019 22:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591328#M15195</guid>
      <dc:creator>htn1</dc:creator>
      <dc:date>2019-09-24T22:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically rename a bunch of vriables into new variables: Y1, Y2....</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591332#M15197</link>
      <description>&lt;P&gt;Not sure why you have that RENAME statement in the middle of the data step where you are attempting to generate the information that you will need to make an actual RENAME statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length old $32 rename $32767;
  do i=1 to countw("&amp;amp;varlist");
    old=scan("&amp;amp;varlist",i);
    rename=catx(' ',rename,catx('=',&amp;amp;old,cats('Y',i)));
  end;
  call symputx('rename',rename);
run;

proc datasets nolist lib=mylib ;
  modify mydata ;
  rename &amp;amp;rename ;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Sep 2019 23:24:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591332#M15197</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-24T23:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically rename a bunch of vriables into new variables: Y1, Y2....</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591333#M15198</link>
      <description>There's no mention here of any input or output data set.  So if you can get the program to work, what would its results be?  Would it be a one-time application or would it need to be used with different values for &amp;amp;varlist?</description>
      <pubDate>Tue, 24 Sep 2019 23:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591333#M15198</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-09-24T23:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically rename a bunch of vriables into new variables: Y1, Y2....</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591678#M15245</link>
      <description>&lt;P&gt;A space separated list of value can be parsed into items and an expression can be performed to transform each item.&amp;nbsp; This process is known as &lt;STRONG&gt;map&lt;/STRONG&gt; or &lt;STRONG&gt;apply &lt;/STRONG&gt;in some programming circles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Construct a map macro for parsing items from a list and resolving a passed in expression for each item.&amp;nbsp; The macro provides 'automatic' macro variable&amp;nbsp;&lt;STRONG&gt;item&lt;/STRONG&gt; and&amp;nbsp;&lt;STRONG&gt; _n_&lt;/STRONG&gt; for use in the expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each item in varlist is mapped to &amp;lt;item&amp;gt;=Y&amp;lt;_n_&amp;gt; which can be used in a rename statement or option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro map(items, expr);
  %local item _N_;
  %do _n_ = 1 %to %sysfunc(countw(&amp;amp;items));
    %let item = %scan(&amp;amp;items,&amp;amp;_n_);
    %unquote(&amp;amp;expr)
  %end;
%mend;

%let varlist = &lt;BR /&gt;  A0A075B6K4 A0A075B6K5 A2NJV5 O14562 O96009 P00390 P01742 P01834 P01892 P02655 P02751 &lt;BR /&gt;  P02765 P02786 P02787 P02790 P09467 P11686 P16152 P18065 P20039 P30405 P30838 P35247&lt;BR /&gt;  P43652 P49006 P50238 P61326 P63151 P02768_1 P06681_1 P14678_1 P22897_1 P23246_1 &lt;BR /&gt;  P29279_1 P51159_1 P61201_1 Q02790 Q02818 Q03252 Q14956 Q16401 Q99758 Q7Z2W4 Q8IWL2_1 Q8N9N7 &lt;BR /&gt;  Q96AG4 Q96IU4 Q96PD5 Q96S96 Q9BPX5 Q9BQ61 Q9BUL8 Q9BWS9_1 Q9H1Z4 Q9NQ79 Q9P2T1_1 Q9UGT4 Q9Y3E1&lt;BR /&gt;  ;

data have;
  retain &amp;amp;varlist 1.;
run;

data want;
  set have;
  rename %map(&amp;amp;varlist,%nrstr(&amp;amp;item.=y&amp;amp;_N_.));
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The same idea, but mapping to &amp;lt;item&amp;gt; as Y&amp;lt;_n_&amp;gt; for use in a SQL select&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want2(drop=sentinel) as
  select 
    %map(&amp;amp;varlist,%nrstr(&amp;amp;item. as y&amp;amp;_N_.,))
    . as sentinel
  from have;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 19:37:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamically-rename-a-bunch-of-vriables-into-new-variables-Y1-Y2/m-p/591678#M15245</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2019-09-25T19:37:31Z</dc:date>
    </item>
  </channel>
</rss>

