<?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: Can I rename column 1 without knowing the name of column 1? in SAS Viya</title>
    <link>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945129#M2606</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 set sashelp.class;
run;



%let dsid=%sysfunc(open(have));
%let first_var=%sysfunc(varname(&amp;amp;dsid,1));
%let dsid=%sysfunc(close(&amp;amp;dsid.));

data want;
 set have(rename=(&amp;amp;first_var=new_name));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Sep 2024 01:09:18 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-09-25T01:09:18Z</dc:date>
    <item>
      <title>Can I rename column 1 without knowing the name of column 1?</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945122#M2604</link>
      <description>&lt;P&gt;I have a lot of datasets with a different name for column 1, but I'd like to rename column 1 using the same name so I can stack them.&amp;nbsp; I can think of ways to do this using macros but I'm curious to know if there's a simpler option.&amp;nbsp; For example, is there a way to rename column 1 (or any column for that matter) using the column order?&amp;nbsp; Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&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;rename=(_COLUMN1_=NEWNAME)&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 23:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945122#M2604</guid>
      <dc:creator>Ryanb2</dc:creator>
      <dc:date>2024-09-24T23:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Can I rename column 1 without knowing the name of column 1?</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945123#M2605</link>
      <description>&lt;P&gt;Figure out what the name is.&lt;/P&gt;
&lt;P&gt;Then you can use it to generate the RENAME=() dataset option.&lt;/P&gt;
&lt;P&gt;For example say you want to combine the observations from three datasets in the WORK libref .&amp;nbsp; So generate a SET statement that looks like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set a(rename=(a=column1))&amp;nbsp;b(rename=(b=column1)) c(rename=(c=column1))&amp;nbsp;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let libref=work;
%let dslist=a b c ;
proc sql noprint;
select cats(libname,'.',memname,'(rename=(',name,'=','column1','))')
  into :code separated by ' '
  from dictionary.columns
  where libname=%upcase("&amp;amp;libref")
    and findw("&amp;amp;dslist",memname,,'sir')
    and varnum=1
    and lowcase(name) ne 'column1'
;
quit;
data want;
  set &amp;amp;code ;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Can you explain how you got into this situation? Perhaps you can fix the problem earlier in the process and avoid the need to generate RENAME= dataset options.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2024 00:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945123#M2605</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-25T00:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: Can I rename column 1 without knowing the name of column 1?</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945129#M2606</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 set sashelp.class;
run;



%let dsid=%sysfunc(open(have));
%let first_var=%sysfunc(varname(&amp;amp;dsid,1));
%let dsid=%sysfunc(close(&amp;amp;dsid.));

data want;
 set have(rename=(&amp;amp;first_var=new_name));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Sep 2024 01:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945129#M2606</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-09-25T01:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: Can I rename column 1 without knowing the name of column 1?</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945159#M2607</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46197"&gt;@Ryanb2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the fact that the UNION operator of PROC SQL (without the CORRESPONDING option) aligns columns by position.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have1(rename=(name=abc))
     have2(rename=(name=def))
     have3(rename=(name=ghi));
set sashelp.class;
run;

/* Stack HAVE1 - HAVE3, renaming the first column to Firstname */

proc sql nowarn;
create table base (Firstname char(8)); /* define name and length of the first column */

create table want as
select * from base
union all select * from have1
union all select * from have2
union all select * from have3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Table BASE may contain more columns to be renamed. PROC SQL would automatically increase the length of character variables to avoid truncation (e.g., if variable DEF in dataset HAVE2 had length 11).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have an existing template dataset, you can use that instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select * from sashelp.class(obs=0)
union all select * from have1
union all select * from have2
union all select * from have3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously, you don't need an additional template dataset if the columns in question have the desired names in dataset HAVE1 .&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2024 07:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Can-I-rename-column-1-without-knowing-the-name-of-column-1/m-p/945159#M2607</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-09-25T07:57:04Z</dc:date>
    </item>
  </channel>
</rss>

