<?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: Help to rename in data infile in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560576#M156789</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example that uses PROC DATASETS - which is faster than recreating your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example, the name change is dynamic and assumes you have a data set with the old and new names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a lot of variables (thousands) this may not work because there's a limit of 64K characters within a macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data sample;
do i=10000 to 12000;
	start_date=i;
	middle_date=i+3;
	end_date=i+5;
	date_no_change=start_date;
	output;
end;
format start_date end_date middle_date date9.;
run;

*data set with old and new names;
data rename_list;
infile cards dlm=',' truncover;
length name new_name $32.;
input name $ new_name $;
cards;
start_date, my_start_date
middle_date, my_mid_date
end_date, my_end_date
;;;;


proc sql noprint;
select catx("=", name, new_name) 
into :rename_list
separated by " "
from renameTable;
quit;


%put &amp;amp;rename_list;

proc datasets library=work nodetails nolist;
modify sample;
rename &amp;amp;rename_list;
run; quit;

proc print data=sample noobs;
run;d&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/251275"&gt;@Sk1_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;every day i receive a .text file with different variable, so i need to identify the variables and then rename, i use this code to discover the variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA COLUMN(KEEP=COL NAME)&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;INFILE .txtfile DSD DLM=';' COLUMN=CC TERMSTR=CRLF LENGTH=LL lrecl=999999 TRUNCOVER;&lt;/P&gt;
&lt;P&gt;IF _N_=1 THEN DO ;&lt;/P&gt;
&lt;P&gt;INPUT @;&lt;/P&gt;
&lt;P&gt;DO COL=1 BY 1 WHILE (CC &amp;lt;= LL) ;&lt;/P&gt;
&lt;P&gt;LENGTH NAME$350 ;&lt;/P&gt;
&lt;P&gt;INPUT NAME @;&lt;/P&gt;
&lt;P&gt;OUTPUT COLUMN;&lt;/P&gt;
&lt;P&gt;END;&lt;/P&gt;
&lt;P&gt;INPUT;&lt;/P&gt;
&lt;P&gt;END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;ROW+1;&lt;/P&gt;
&lt;P&gt;RUN;&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;after that, i got the name of the all variables that i identified, because i have a table with the new names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;/P&gt;
&lt;P&gt;CREATE TABLE COLUMN_V2&lt;/P&gt;
&lt;P&gt;AS SELECT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A.NAME,&lt;/P&gt;
&lt;P&gt;B.NEW_NAME&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;FROM COLUMN AS A&lt;/P&gt;
&lt;P&gt;LEFT JOIN TABLE_NEW_NAMES AS B&lt;/P&gt;
&lt;P&gt;ON A.NAME=B.NAME;&lt;/P&gt;
&lt;P&gt;QUIT;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now i need to import the file renaming the fields using a macro, do you know how i can do that? im asking for a macro, because this process needs to be automatic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 May 2019 16:51:22 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-05-21T16:51:22Z</dc:date>
    <item>
      <title>Help to rename in data infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560566#M156786</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;every day i receive a .text file with different variable, so i need to identify the variables and then rename, i use this code to discover the variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA COLUMN(KEEP=COL NAME)&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;INFILE .txtfile DSD DLM=';' COLUMN=CC TERMSTR=CRLF LENGTH=LL lrecl=999999 TRUNCOVER;&lt;/P&gt;&lt;P&gt;IF _N_=1 THEN DO ;&lt;/P&gt;&lt;P&gt;INPUT @;&lt;/P&gt;&lt;P&gt;DO COL=1 BY 1 WHILE (CC &amp;lt;= LL) ;&lt;/P&gt;&lt;P&gt;LENGTH NAME$350 ;&lt;/P&gt;&lt;P&gt;INPUT NAME @;&lt;/P&gt;&lt;P&gt;OUTPUT COLUMN;&lt;/P&gt;&lt;P&gt;END;&lt;/P&gt;&lt;P&gt;INPUT;&lt;/P&gt;&lt;P&gt;END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;ROW+1;&lt;/P&gt;&lt;P&gt;RUN;&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;after that, i got the name of the all variables that i identified, because i have a table with the new names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;CREATE TABLE COLUMN_V2&lt;/P&gt;&lt;P&gt;AS SELECT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A.NAME,&lt;/P&gt;&lt;P&gt;B.NEW_NAME&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;FROM COLUMN AS A&lt;/P&gt;&lt;P&gt;LEFT JOIN TABLE_NEW_NAMES AS B&lt;/P&gt;&lt;P&gt;ON A.NAME=B.NAME;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now i need to import the file renaming the fields using a macro, do you know how i can do that? im asking for a macro, because this process needs to be automatic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!!&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 16:17:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560566#M156786</guid>
      <dc:creator>Sk1_SAS</dc:creator>
      <dc:date>2019-05-21T16:17:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help to rename in data infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560576#M156789</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example that uses PROC DATASETS - which is faster than recreating your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example, the name change is dynamic and assumes you have a data set with the old and new names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a lot of variables (thousands) this may not work because there's a limit of 64K characters within a macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data sample;
do i=10000 to 12000;
	start_date=i;
	middle_date=i+3;
	end_date=i+5;
	date_no_change=start_date;
	output;
end;
format start_date end_date middle_date date9.;
run;

*data set with old and new names;
data rename_list;
infile cards dlm=',' truncover;
length name new_name $32.;
input name $ new_name $;
cards;
start_date, my_start_date
middle_date, my_mid_date
end_date, my_end_date
;;;;


proc sql noprint;
select catx("=", name, new_name) 
into :rename_list
separated by " "
from renameTable;
quit;


%put &amp;amp;rename_list;

proc datasets library=work nodetails nolist;
modify sample;
rename &amp;amp;rename_list;
run; quit;

proc print data=sample noobs;
run;d&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/251275"&gt;@Sk1_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;every day i receive a .text file with different variable, so i need to identify the variables and then rename, i use this code to discover the variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA COLUMN(KEEP=COL NAME)&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;INFILE .txtfile DSD DLM=';' COLUMN=CC TERMSTR=CRLF LENGTH=LL lrecl=999999 TRUNCOVER;&lt;/P&gt;
&lt;P&gt;IF _N_=1 THEN DO ;&lt;/P&gt;
&lt;P&gt;INPUT @;&lt;/P&gt;
&lt;P&gt;DO COL=1 BY 1 WHILE (CC &amp;lt;= LL) ;&lt;/P&gt;
&lt;P&gt;LENGTH NAME$350 ;&lt;/P&gt;
&lt;P&gt;INPUT NAME @;&lt;/P&gt;
&lt;P&gt;OUTPUT COLUMN;&lt;/P&gt;
&lt;P&gt;END;&lt;/P&gt;
&lt;P&gt;INPUT;&lt;/P&gt;
&lt;P&gt;END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;ROW+1;&lt;/P&gt;
&lt;P&gt;RUN;&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;after that, i got the name of the all variables that i identified, because i have a table with the new names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;/P&gt;
&lt;P&gt;CREATE TABLE COLUMN_V2&lt;/P&gt;
&lt;P&gt;AS SELECT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A.NAME,&lt;/P&gt;
&lt;P&gt;B.NEW_NAME&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;FROM COLUMN AS A&lt;/P&gt;
&lt;P&gt;LEFT JOIN TABLE_NEW_NAMES AS B&lt;/P&gt;
&lt;P&gt;ON A.NAME=B.NAME;&lt;/P&gt;
&lt;P&gt;QUIT;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now i need to import the file renaming the fields using a macro, do you know how i can do that? im asking for a macro, because this process needs to be automatic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 16:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560576#M156789</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-21T16:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help to rename in data infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560585#M156792</link>
      <description>&lt;P&gt;How many columns do your files have?&amp;nbsp; If it is fewer then about 1000 you can probably get away with just using a macro variable to contain the RENAME list.&lt;/P&gt;
&lt;P&gt;First get the names from the file.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename newfile "newfile_2019_05_21.txt" termstr=crlf ;

data names ;
  infile newfile  dsd dlm=';' obs=1 ;
  varnum + 1;
  input name :$350. @@;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can join that list to your master list and find out which ones need to be renamed and put the list of OLD=NEW pairs into a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
%let renames=;
select catx(a.name,b.new_name) into :renames separated by ' '
  from names a inner join table_new_names b
  on a.name = b.name
  where a.name ne b.new_name
    and not missing(b.new_name)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use that list in a RENAME= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import file=newfile replace dbms=dlm 
   out=want(rename=(&amp;amp;renames)) 
;
  delimiter=';';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Are your names really up to 350 characters long?&amp;nbsp; &amp;nbsp;Are you using PROC IMPORT to read the file?&amp;nbsp; If both then perhaps you should skip the headers and just let PROC IMPORT create the variables using generic names VAR1,VAR2,.... and rename those.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
%let renames=;
select catx(cats('VAR',a.varnum),coalesce(b.new_name,substr(a.name,1,32)))
   into :renames separated by ' '
  from names a left join table_new_names b
  on a.name = b.name
;
quit;
proc import file=newfile replace dbms=dlm 
   out=want(rename=(&amp;amp;renames)) 
;
  delimiter=';';
  datarow=2;
  getnames=no;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 17:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560585#M156792</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-21T17:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: Help to rename in data infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560618#M156815</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tks a lot for yout help, i dont know the number of columns, some days&amp;nbsp;are&amp;nbsp; 300 other days are 1200, its not a fixed base.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I execute your code "proc import file=newfile replace dbms=dlm&lt;BR /&gt;&amp;nbsp;&amp;nbsp; out=want(rename=(&amp;amp;renames))&lt;BR /&gt;; delimiter=';';&lt;BR /&gt;run;"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and this error appears&lt;/P&gt;&lt;P&gt;ERROR 79-322: Expecting a =.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tks!!!&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 18:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560618#M156815</guid>
      <dc:creator>Sk1_SAS</dc:creator>
      <dc:date>2019-05-21T18:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help to rename in data infile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560620#M156817</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/251275"&gt;@Sk1_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tks a lot for yout help, i dont know the number of columns, some days&amp;nbsp;are&amp;nbsp; 300 other days are 1200, its not a fixed base.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I execute your code "proc import file=newfile replace dbms=dlm&lt;BR /&gt;&amp;nbsp;&amp;nbsp; out=want(rename=(&amp;amp;renames))&lt;BR /&gt;; delimiter=';';&lt;BR /&gt;run;"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and this error appears&lt;/P&gt;
&lt;P&gt;ERROR 79-322: Expecting a =.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tks!!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show the value of the RENAMES macro variable that got created. It is possible your column headers in your source file are not valid SAS variable names.&amp;nbsp; You might get away with using the NLITERAL() function to convert them if your sessions is using VALIDVARNAME=ANY option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the names are too long then perhaps the value got truncated?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to generate more rename pairs than can fit into a macro variable then generate the code another way.&lt;/P&gt;
&lt;P&gt;For example you could create a file with a RENAME statement.&amp;nbsp; Say you a dataset named RENAMES with the NAME and NEW_NAME pairs you could generate a temporary file with the rename statement and then use %INCLUDE to add that statement to a PROC DATASETS step to change the names in the file you created using PROC IMPORT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename rename temp;
data _null_;
  set renames end=eof;
  file rename lrecl=80;
  if _n_= then put 'rename ' @;
  put name '=' new_name @;
  if eof then put / ';' ;
run;
proc datasets nolist lib=work;
modify want ;
%include rename ;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 19:07:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-rename-in-data-infile/m-p/560620#M156817</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-21T19:07:41Z</dc:date>
    </item>
  </channel>
</rss>

