<?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: Do Loop to change variable names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503263#M134449</link>
    <description>&lt;P&gt;In a SAS data step, use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename q1-q32=base_q1-base_q32;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to have q1_base, then try this (assuming you have no other variable names that begin with Q): MODIFIED slightly as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have noprint out=_contents_;
run;

proc sql;
    select cats(name,'=',name,'_base') into :varnames separated by ' ' 
    from _contents_ where upcase(name) eqt 'Q';
quit;

proc datasets library=work nolist;
    modify have;
    rename &amp;amp;varnames;
run;
quit;&lt;/CODE&gt;&amp;nbsp; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Oct 2018 13:32:56 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-10-11T13:32:56Z</dc:date>
    <item>
      <title>Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503248#M134438</link>
      <description>&lt;P&gt;Hi, I have a list of 32 variables (Q1, Q2,....Q31, Q32) and I would like to rename them (Q1_base, Q2_base,...,Q31_base, Q32_base). I am trying to figure out to code for a do loop that would do this fairly easily. All the variables are located in the same dataset. Any help is much appreciated! Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 21:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503248#M134438</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-10T21:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503260#M134447</link>
      <description>&lt;P&gt;This is unbelievably easy if you want the new variables to be named Base_Q1 through Base_Q32, no loops necessary. But, if they must be q1_base through q32_base, then I think you could use PROC SQL and macro variables to get this done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So which is it? Can the variables be Base_Q1 through Base_Q32, or not?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 21:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503260#M134447</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-10T21:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503261#M134448</link>
      <description>&lt;P&gt;Having them be Base_Q1 would be fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you mind showing me how to do both ways though, just out of curiosity?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 21:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503261#M134448</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-10T21:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503263#M134449</link>
      <description>&lt;P&gt;In a SAS data step, use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename q1-q32=base_q1-base_q32;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to have q1_base, then try this (assuming you have no other variable names that begin with Q): MODIFIED slightly as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have noprint out=_contents_;
run;

proc sql;
    select cats(name,'=',name,'_base') into :varnames separated by ' ' 
    from _contents_ where upcase(name) eqt 'Q';
quit;

proc datasets library=work nolist;
    modify have;
    rename &amp;amp;varnames;
run;
quit;&lt;/CODE&gt;&amp;nbsp; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 13:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503263#M134449</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-11T13:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503267#M134451</link>
      <description>If you use BASE_XXX you can also use the colon notation in future references which you cannot do if you use your requested notation. In general, in SAS, prefixes are better than suffixes.</description>
      <pubDate>Wed, 10 Oct 2018 22:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503267#M134451</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-10T22:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503271#M134452</link>
      <description>&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 23:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503271#M134452</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-10T23:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503350#M134489</link>
      <description>&lt;P&gt;Just for completeness:&lt;/P&gt;
&lt;PRE&gt;... into :varnames &lt;FONT color="#FF0000"&gt;separated by ' '&lt;/FONT&gt; from _contents_ ...&lt;/PRE&gt;
&lt;P&gt;Otherwise, only the first variable would be renamed.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 09:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503350#M134489</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-11T09:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503372#M134495</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Just for completeness:&lt;/P&gt;
&lt;PRE&gt;... into :varnames &lt;FONT color="#FF0000"&gt;separated by ' '&lt;/FONT&gt; from _contents_ ...&lt;/PRE&gt;
&lt;P&gt;Otherwise, only the first variable would be renamed.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Oh yeah, thanks ... that's always a problem when you can't test your code on the actual situation&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 10:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503372#M134495</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-11T10:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503400#M134508</link>
      <description>&lt;P&gt;This is an example using this attached %expand_varlist macro.&amp;nbsp; It uses a similar technique to that shown by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;packed into a macro.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;183  data q;
184     length q1-q35 $1;
185     stop;
186     call missing(of _all_);
187     run;

NOTE: The data set WORK.Q has 0 observations and 35 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


188
189  %let rename=%expand_varlist(data=q,var=q:,expr=cats(_name_,'=',_name_,'_BASE'));
NOTE: M=EXPAND_VARLIST_3 is a unique symbol name
190  %put NOTE: &amp;amp;=rename;
NOTE: RENAME=q1=q1_BASE q2=q2_BASE q3=q3_BASE q4=q4_BASE q5=q5_BASE q6=q6_BASE q7=q7_BASE q8=q8_BASE q9=q9_BASE q10=q10_BASE
q11=q11_BASE q12=q12_BASE q13=q13_BASE q14=q14_BASE q15=q15_BASE q16=q16_BASE q17=q17_BASE q18=q18_BASE q19=q19_BASE
q20=q20_BASE q21=q21_BASE q22=q22_BASE q23=q23_BASE q24=q24_BASE q25=q25_BASE q26=q26_BASE q27=q27_BASE q28=q28_BASE
q29=q29_BASE q30=q30_BASE q31=q31_BASE q32=q32_BASE q33=q33_BASE q34=q34_BASE q35=q35_BASE
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Oct 2018 13:30:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503400#M134508</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2018-10-11T13:30:19Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503405#M134512</link>
      <description>&lt;P&gt;I'm going to steal your macro and add it to my AUTOCALL library. Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 13:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503405#M134512</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-11T13:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503417#M134515</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;I hope you find it useful.&amp;nbsp; I think it is a nice application of DOSUBL.&amp;nbsp; Using PROC TRANSPOSE to generate the variable list has a number of advantages including&amp;nbsp;keeping the expanded variable list&amp;nbsp;in the proper order.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 14:13:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variable-names/m-p/503417#M134515</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2018-10-11T14:13:39Z</dc:date>
    </item>
  </channel>
</rss>

