<?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 rename all variables by removing last 5 characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869473#M343415</link>
    <description>&lt;P&gt;this is my code&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want; set have;
 do i=9 to nvars;
    varname = vname(i);
    newname = substr(varname,1,length(varname)-5);
    rename varname = newname;
 end;
drop i varname newname;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the error:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;NOTE: Variable nvars is uninitialized.
WARNING: The variable varname in the DROP, KEEP, or RENAME list has never been referenced.
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero, or invalid.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note: the variables names from the 9th until the last one end with '_2017'. I want to remove that. There are 139 variables in total. 131 variables ending with '_2017'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Apr 2023 20:46:55 GMT</pubDate>
    <dc:creator>Satori</dc:creator>
    <dc:date>2023-04-12T20:46:55Z</dc:date>
    <item>
      <title>rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869473#M343415</link>
      <description>&lt;P&gt;this is my code&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want; set have;
 do i=9 to nvars;
    varname = vname(i);
    newname = substr(varname,1,length(varname)-5);
    rename varname = newname;
 end;
drop i varname newname;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the error:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;NOTE: Variable nvars is uninitialized.
WARNING: The variable varname in the DROP, KEEP, or RENAME list has never been referenced.
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero, or invalid.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note: the variables names from the 9th until the last one end with '_2017'. I want to remove that. There are 139 variables in total. 131 variables ending with '_2017'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 20:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869473#M343415</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-04-12T20:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869481#M343419</link>
      <description>&lt;P&gt;I think you can use a different approach here. The methodology used here is incorrect - rename names must be hardcoded it doesn't take variable values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql noprint;
select catx("=", name, substr(name, 1, length(name)-5)) 
into :rename_list
separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='HAVE'
and upper(trim(name)) like '%_2017';
quit;


%put &amp;amp;rename_list;


proc datasets library=work nodetails nolist;
modify have;
rename &amp;amp;rename_list;
run; quit;

proc contents data=have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;this is my code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data want; set have;
 do i=9 to nvars;
    varname = vname(i);
    newname = substr(varname,1,length(varname)-5);
    rename varname = newname;
 end;
drop i varname newname;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the error:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;NOTE: Variable nvars is uninitialized.
WARNING: The variable varname in the DROP, KEEP, or RENAME list has never been referenced.
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero, or invalid.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: the variables names from the 9th until the last one end with '_2017'. I want to remove that. There are 139 variables in total. 131 variables ending with '_2017'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 21:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869481#M343419</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-12T21:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869482#M343420</link>
      <description>&lt;P&gt;Are you sure it isn't easier to re-read the data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the file started as any form of text I might ask why you have characters you don't want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have several issues.&lt;/P&gt;
&lt;P&gt;First, nowhere do you define NVARS and apparently it is not a variable in your data set.&lt;/P&gt;
&lt;P&gt;Vname returns the name of the variable if you have a an array reference like Vname(array(i)) . If you use Vname(somevariable) it will return the literal text "somevariable" if that is the name of a variable in the set.&lt;/P&gt;
&lt;P&gt;So your Vname does not have any reference to anything related to variables to return those names.&lt;/P&gt;
&lt;P&gt;The Rename statement in a data step will not allow use of expressions. So Rename varname=newname would actually be renaming the variable Varname that you just created to newname.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use information to build a control data set to create rename statements, preferably for Proc Datasets that is intended to do things like change properties of datasets including variable names, formats, labels and informats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A small working example:&lt;/P&gt;
&lt;PRE&gt;/* create something to work with*/
data work.have;
 input a b_2017 c_2017 $;
datalines;
1  2  A
4  5  B
;
run;

proc sql;
   create table rename as
   select name, tranwrd(name,'_2017','') as newname
   from dictionary.columns
      where libname='WORK' and memname='HAVE' 
            and index(name,'_2017')&amp;gt;0
   ;
quit;

data _null_;
   set rename end=last;
   if _n_=1 then call execute('proc datasets library=work nodetails nolist; modify have; rename');
   call execute (name||'='||newname);
   if last then call execute('; quit;');
run;&lt;/PRE&gt;
&lt;P&gt;What this does: The Proc sql uses the metadata maintained by SAS to search in the data set indicated in the Libname and Memname (data set) that have variable names that have the text _2017 in the name. The Library and Memname values are stored in the metadata in upper case so you want to use that in the where clause. &lt;STRONG&gt;Caution:&lt;/STRONG&gt; I am not searching for the "last 5 characters" just the presence of the string '_2017'. If you have that as valid text as part of the variable names in other positions I would recommend going back and rereading the data from start. The SQL also creates a new variable with the modified name removing _2017. &lt;STRONG&gt;Extreme caution: &lt;/STRONG&gt;There is a chance, depending on YOUR data that removing that next from the name might result in the name of another variable that already exists in your data set. Have you checked for that? Example your data set as variable Measure and Measure_2017. If you attempt to rename Measure_2017 as Measure you will not get it as that variable already exists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data _null_ step uses the Call execute function to place statements in to the execution buffer. It starts with boilerplate to use proc datasets with your library , set some options to reduce output, and then select the data set to modify and then the instruction rename. the next call execute creates the rename instructions. The data set option END creates a temporary variable Last that is true when the observation the data step is processing is the last one. So we can provide the code to end the Proc data sets call.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For my example the log shows:&lt;/P&gt;
&lt;PRE&gt;NOTE: CALL EXECUTE generated line.
1   + proc datasets library=work nodetails nolist; modify have;
NOTE: Writing HTML Body file: sashtml1.htm
1   +                                                           rename
2   + b_2017                          =b
3   + c_2017                          =c
4   + ;
NOTE: Renaming variable b_2017 to b.
NOTE: Renaming variable c_2017 to c.
4   +   quit;

&lt;/PRE&gt;
&lt;P&gt;You should be able to compare those lines after the + with the Call execute statements.&lt;/P&gt;
&lt;P&gt;Note that Proc Datasets is one of a few procedures that does "run group processing" and could have multiple Run statements in one procedure call. This procedure uses QUIT; to end the procedure.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 21:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869482#M343420</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-12T21:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869571#M343458</link>
      <description>&lt;P&gt;I got a few errors:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;484        proc sql noprint;
485        select catx("=", name, substr(name, 1, length(name)-5))
486        into :rename_list
487        separated by " "
488        from sashelp.vcolumn
489        where libname='bvd'
490        and memname='brics2017'
491        and upper(trim(name)) like '%_2017';
NOTE: No rows were selected.
492        quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      

WARNING: Apparent symbolic reference RENAME_LIST not resolved.
493        
494        
495        %put &amp;amp;rename_list;
&amp;amp;rename_list
496        
497        
498        proc datasets library=bvd nodetails nolist;
499        modify brics2017;
WARNING: Apparent symbolic reference RENAME_LIST not resolved.
500        rename &amp;amp;rename_list;
                  _
                  22
                  76
NOTE: Enter RUN; to continue or QUIT; to end the procedure.
ERROR 22-322: Expecting a name.  
ERROR 76-322: Syntax error, statement will be ignored.
501        run;

NOTE: Statements not processed because of errors noted above.
501      !      quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Apr 2023 13:39:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869571#M343458</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-04-13T13:39:39Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869572#M343459</link>
      <description>&lt;P&gt;I got no errors, but something is not working:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;484        proc sql;
485           create table rename as
486           select name, tranwrd(name,'_2017','') as newname
487           from dictionary.columns
488              where libname='bvd' and memname='brics2017'
489                    and index(name,'_2017')&amp;gt;0
490           ;
NOTE: Table WORK.RENAME created, with 0 rows and 2 columns.

491        quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

492        
493        data _null_;
494           set rename end=last;
495           if _n_=1 then call execute('proc datasets library=bvd nodetails nolist; modify brics2017; rename');
496           call execute (name||'='||newname);
497           if last then call execute('; quit;');
498        run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Apr 2023 13:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869572#M343459</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-04-13T13:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869577#M343462</link>
      <description>&lt;PRE&gt;488              where libname='bvd' and memname='brics2017'
489                    and index(name,'_2017')&amp;gt;0
490           ;
NOTE: Table WORK.RENAME created, with 0 rows and 2 columns.&lt;/PRE&gt;
&lt;P&gt;LIBNAME is always upper case, it can't be lower case 'bvd'. Similarly, MEMNAME is always upper case, it can't be lower case 'brics2017'. NAME can be mixed case, you would need to check how it exists in the dictionary table (please run PROC CONTENTS to see the actual case of your variable names).&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 13:53:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869577#M343462</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-13T13:53:57Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869587#M343467</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I got no errors, but something is not working:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;484        proc sql;
485           create table rename as
486           select name, tranwrd(name,'_2017','') as newname
487           from dictionary.columns
488              where libname='bvd' and memname='brics2017'
489                    and index(name,'_2017')&amp;gt;0
490           ;
NOTE: Table WORK.RENAME created, with 0 rows and 2 columns.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And from my original post:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The Library and Memname values are stored in the metadata in upper case so you want to use that in the where clause.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 14:31:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869587#M343467</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-13T14:31:16Z</dc:date>
    </item>
    <item>
      <title>Re: rename all variables by removing last 5 characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869615#M343486</link>
      <description>&lt;P&gt;Names are stored in all upper case&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;489        where libname='bvd'
490        and memname='brics2017'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make the libname and memname upper cased and it should work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 16:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-all-variables-by-removing-last-5-characters/m-p/869615#M343486</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-13T16:20:55Z</dc:date>
    </item>
  </channel>
</rss>

