<?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: Duplicate columns with a different name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354184#M82818</link>
    <description>&lt;P&gt;The libname and memname, when using dictionary.columns, must be in UPPERCASE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus, you would have to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
  select catt(name,'_base=',name,';')
    into : creates separated by ' '
      from dictionary.columns
        where libname= "E" and
              memname='COMB_FILE_VTE_F'
  ;
quit;

data E.Comb_File_VTE_F1;
  set E.Comb_File_VTE_F;
  &amp;amp;creates.
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2017 16:21:01 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-04-27T16:21:01Z</dc:date>
    <item>
      <title>Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354152#M82800</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help with this small thing.&lt;/P&gt;&lt;P&gt;I've 25 columns which I want to duplicate each one to make 50 columns by adding "&lt;EM&gt;columnname&lt;/EM&gt;_Base" &amp;nbsp;to the duplicate column, is there way to do automated instead of doing manually?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ex: Have dataset Columns: &amp;nbsp;X &amp;nbsp; &amp;nbsp;Y &amp;nbsp; &amp;nbsp;Z &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Want dataset Columns: &amp;nbsp;X &amp;nbsp; &amp;nbsp;Y &amp;nbsp; &amp;nbsp;Z &amp;nbsp; &amp;nbsp;X_Base &amp;nbsp; &amp;nbsp;Y_Base &amp;nbsp; Z_Base&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354152#M82800</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T15:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354156#M82802</link>
      <description>&lt;P&gt;You just want to create those variables, or create and also copy their values? And are they all character, numeric or a mixture of both?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354156#M82802</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-27T15:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354158#M82803</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create and copy values, its a mixture of char and num variable types.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354158#M82803</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T15:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354160#M82804</link>
      <description>&lt;P&gt;You can extract the variable specifications from dictionary.columns (in SQL) or sashelp.vcolumn (data step).&lt;/P&gt;
&lt;P&gt;Then you can create a data step that adds variables, and sets attributes and values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table columns as
select *
from dictionary.columns
where libname = 'SASHELP' and memname = 'CLASS';
quit;

data _null_;
set columns end=done;
if _n_ = 1 then call execute("data class; set sashelp.class; ");
call execute("attrib " !! strip(name) !! "_base length=");
if type = "char" then call execute("$");
call execute(strip(put(length,best.)));
if format ne " " then call execute(" format=" !! strip(format));
call execute("; " !! strip(name) !! "_base=" !! strip(name) !! ";");
if done then call execute("run;");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354160#M82804</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-27T15:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354161#M82805</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input x y $ z;
  cards;
1 a 3
2 b 1
;

proc sql noprint;
  select catt(name,'_base=',name,';')
    into : creates separated by ' '
      from dictionary.columns
        where libname='WORK' and
              memname='HAVE'
  ;
quit;

data want;
  set have;
  &amp;amp;creates.
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354161#M82805</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-27T15:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354162#M82806</link>
      <description>&lt;P&gt;Well, you can:&lt;/P&gt;
&lt;PRE&gt;data have;
  x=1; y=2; z=5;
run;

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and memname="HAVE")) end=last;
  if _n_=1 then call execute('data want; set have;');
  call execute(cats('base_',name,'=',name,';'));
  if last then call execute('run;');
run;&lt;/PRE&gt;
&lt;P&gt;I would really advise against going down this route however. &amp;nbsp;You lose the inbuilt functionality of lists and arrays which will make you life so much easier. &amp;nbsp;For instance:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array vals{3} x y z;
  array base_{3};
  do i=1 to dim(vals);
    base_{i}=vals{i};
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This will create Base_1, Base_2, etc. &amp;nbsp; You can always now refer to that range in code as array base_{*}; and loop over it. &amp;nbsp;Makes your coding simpler. &amp;nbsp;Same with your variables x y z. &amp;nbsp;If you make these a list, then you can simply all your code:&lt;/P&gt;
&lt;PRE&gt;data have;
  value1=1; value2=2; value3=5;
run;
data want;
  set have;
  array values value:;
  array base{3};
  do i=1 to dim(values);
    base{i}=values{i};
  end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354162#M82806</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-27T15:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354164#M82807</link>
      <description>&lt;P&gt;Do they all share the same datatype?, if yes, would something like this help:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;select catx('_',name,'Base') into :name separated by ' '&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname='YOUR_LIBREF' and memname='YOUR_DATASET_NAME';&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;set thave;&lt;/P&gt;&lt;P&gt;retain &amp;amp;name ' ';&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354164#M82807</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-27T15:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354168#M82809</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;If it's possible can you explain what is the code doing? It would be a great learning.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;--Sujith&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354168#M82809</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T15:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354173#M82811</link>
      <description>&lt;P&gt;Array method is not working because all the variables aren't of the same data type. Can you explain the first method, I'm new to SAS and don't know what's doing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354173#M82811</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T15:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354174#M82812</link>
      <description>&lt;P&gt;If you were asking about my code, it's fairly simple. Dictionary.columns contains all of the metadata for a file, one of which is NAME, namely the variable names. Thus the call to proc sql&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
  select catt(name,'_base=',name,';')
    into : creates separated by ' '
      from dictionary.columns
        where libname='WORK' and
              memname='HAVE'
  ;
quit;
&lt;/PRE&gt;
&lt;P&gt;simply creates a macro variable using the catt function to create, in this case, the following string (as if you were manually typing it):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x_base=x; y_base=y; z_base=z;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thus when you then submit:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  &amp;amp;creates.&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;SAS will actually run:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;&lt;BR /&gt;&lt;SPAN&gt;  x_base=x;&lt;BR /&gt;  y_base=y;&lt;BR /&gt;  z_base=z;&lt;BR /&gt;&lt;/SPAN&gt;run;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:52:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354174#M82812</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-27T15:52:13Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354179#M82814</link>
      <description>&lt;P&gt;Much like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;'s solution, I use the SAS metadata columns, in a datastep rather than a macro variable, so on the first row, the intial data line is created, then for each other row the copy is done, then on the final is is finished. &amp;nbsp;If you run it you will see it creates the code:&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; base_x=x;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354179#M82814</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-27T16:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354183#M82817</link>
      <description>&lt;P&gt;I used your same code. In place of libname is used "E" instead of "WORK", for "HAVE" i used my file name "Comb_File_VTE_F".&lt;/P&gt;&lt;P&gt;Looks like I made a mistake, when i ran the code I got the log output as "No rows selected". Can you point my mistake?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select catt(name,'_base=',name,';')
    into : creates separated by ' '
      from dictionary.columns
        where libname= "E" and
              memname='Comb_File_VTE_F'
  ;
quit;

data E.Comb_File_VTE_F1;
  set E.Comb_File_VTE_F;
  &amp;amp;creates.
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354183#M82817</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T16:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354184#M82818</link>
      <description>&lt;P&gt;The libname and memname, when using dictionary.columns, must be in UPPERCASE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus, you would have to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
  select catt(name,'_base=',name,';')
    into : creates separated by ' '
      from dictionary.columns
        where libname= "E" and
              memname='COMB_FILE_VTE_F'
  ;
quit;

data E.Comb_File_VTE_F1;
  set E.Comb_File_VTE_F;
  &amp;amp;creates.
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:21:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354184#M82818</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-27T16:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354187#M82819</link>
      <description>&lt;P&gt;It worked!&lt;/P&gt;&lt;P&gt;Thanks a ton!!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354187#M82819</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T16:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate columns with a different name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354188#M82820</link>
      <description>&lt;P&gt;Thanks for the help guys!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-columns-with-a-different-name/m-p/354188#M82820</guid>
      <dc:creator>Sujithpeta</dc:creator>
      <dc:date>2017-04-27T16:27:39Z</dc:date>
    </item>
  </channel>
</rss>

