<?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: Create new variables with the same suffix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/400002#M96926</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist = varA varB varC ;
%let want=%sysfunc(prxchange(s/(\w+)/$1_new=$1/,-1,&amp;amp;varlist));

%put &amp;amp;want;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 30 Sep 2017 15:21:43 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2017-09-30T15:21:43Z</dc:date>
    <item>
      <title>Create new variables with the same suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399850#M96868</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to iterate over a list of variables in a data step, and for each one I want to create a new variable that is identical to the original but has the suffix "_new". Is this possible to do without resorting to using a macro (i.e. just in a data step)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, what I'm trying to achieve is something along the lines of (I know the below syntax is wrong):&lt;/P&gt;&lt;P&gt;%let varlist =&amp;nbsp;varA varB varC ... varZ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for each var in &amp;amp;varlist do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;var_new = var;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Giving me a new set of varaibles varA_new etc. taat are identical to the originals. I've worked out a way to do this using&amp;nbsp;%scan(&amp;amp;varlist, &amp;amp;i) in a macro, but this is a small bit of a much larger data step and it's not easy to create new macros in the project I'm working on.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 17:10:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399850#M96868</guid>
      <dc:creator>wb85</dc:creator>
      <dc:date>2017-09-29T17:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with the same suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399853#M96870</link>
      <description>&lt;P&gt;SAS doesn't support that.&amp;nbsp; It does support a few cases where the intent is to rename, rather than create new variables.&amp;nbsp; So you will need a new approach, possibly this.&amp;nbsp; Copy the entire data set to a new data set without change.&amp;nbsp; Then make your calculations based on the new data set, using the original variable names.&amp;nbsp; If necessary, find a way to combine the results of your calculations with the original data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, how did you want to handle this if your original data set contains variable names that are 32 characters long?&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 17:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399853#M96870</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-29T17:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with the same suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399895#M96885</link>
      <description>&lt;P&gt;You may want to consider making a common Prefix if you think you are going to want to do the same things or use them in a list.&lt;/P&gt;
&lt;P&gt;If I variables abc cde and dfe and I create new varirable with a common suffix such as _pdq so that I have abc_pdq cde_pdq and dfe_pdq every time I want to address all of those variables I likely have to use the full variable name every time. However if I create variables&lt;/P&gt;
&lt;P&gt;pdq_abc pdq_cde and pdq_dfe I can use a variable list such as pdq_:&amp;nbsp;&amp;nbsp; , the colon being one of the SAS list builders, and that will reference all varaibles that beging with pdq_ .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand the comment about: it's not easy to create new macros in the project&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can add to the code you place a macro definition before it is needed then use as needed. Or place the macro in an autocall library and don't worry about including it in your code.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 19:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399895#M96885</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-29T19:16:25Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with the same suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399900#M96888</link>
      <description>&lt;P&gt;You could do this with the %include statement - not exactly macro, but not exactly regular sas statement either.&amp;nbsp; This program produces these statments, the second of which is generated by the %INCLUDEd code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; new;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color="#0000ff"&gt;&amp;nbsp; set&lt;/FONT&gt; sashelp.class;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color="#0000ff"&gt;&amp;nbsp; if&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff"&gt;set&lt;/FONT&gt; sashelp.class (&lt;FONT color="#0000ff"&gt;rename&lt;/FONT&gt;=(Name=Name_new Sex=Sex_new&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Age=Age_new Height=Height_new Weight=Weight_new));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color="#000080"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's how, using the VNEXT function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tmp temp ;

data _null_;
  set sashelp.class (obs=1);

  length vnam $32 ;
  file tmp;

  put 'if 0 then set sashelp.class (rename=(';
  do I=1 to 6;
    call vnext(vnam);
    if vnam='vnam' then leave;
    put vnam +(-1) '=' vnam +(-1) '_new';
  end;
  put '));';
run;

data new;
  set sashelp.class;
  %include tmp /source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Sep 2017 19:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/399900#M96888</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-29T19:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variables with the same suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/400002#M96926</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist = varA varB varC ;
%let want=%sysfunc(prxchange(s/(\w+)/$1_new=$1/,-1,&amp;amp;varlist));

%put &amp;amp;want;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Sep 2017 15:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variables-with-the-same-suffix/m-p/400002#M96926</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-30T15:21:43Z</dc:date>
    </item>
  </channel>
</rss>

