<?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 changing variables' names in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805912#M81712</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;i have 42 variables with long names in my data. I want to change their names to V1, V2 etc. Instead of writing each of their names is there a quick way to rename them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
    <pubDate>Mon, 04 Apr 2022 19:00:56 GMT</pubDate>
    <dc:creator>dustychair</dc:creator>
    <dc:date>2022-04-04T19:00:56Z</dc:date>
    <item>
      <title>changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805912#M81712</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;i have 42 variables with long names in my data. I want to change their names to V1, V2 etc. Instead of writing each of their names is there a quick way to rename them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 19:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805912#M81712</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2022-04-04T19:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805914#M81713</link>
      <description>&lt;P&gt;Instead of making meaningless name like V1-V42 that no one will understand ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most if not all SAS procedures (exception: SQL) allow variable lists. So for example in PROC MEANS, you indicate the first and last variable name which are sequential in the data set separated by a double-dash, to include all the variables between the first variable name and the last variable name (inclusive), as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have;
    var verylongvariablenamenumber1--superduperlongvariablename;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And of course you can use any other options and statements in PROC MEANS as you would like.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 19:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805914#M81713</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-04T19:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805916#M81714</link>
      <description>Thank you for your response &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;. My variables names are not sequential. I will use them for another purpose in a different program. I am just organizing them in SAS. so I don't need to run proc means.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Mon, 04 Apr 2022 19:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805916#M81714</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2022-04-04T19:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805917#M81715</link>
      <description>&lt;P&gt;When I say sequential, I don't mean &lt;FONT color="#FF0000"&gt;variable names&lt;/FONT&gt; such as v1 v2 v3 v4, I mean the &lt;FONT color="#FF0000"&gt;position in the data&lt;/FONT&gt; set is sequential, left to right. If the columns are adjacent then you can do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you mean you are just organizing them in SAS? For what purpose? What happens next after you organize??&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I did mention PROC MEANS is an &lt;EM&gt;example&lt;/EM&gt;. The method works in almost any PROC (except SQL) and it works in DATA steps.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 19:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805917#M81715</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-04T19:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805918#M81716</link>
      <description>&lt;P&gt;You could hardcode a RENAME statement in a PROC DATASETS, as per this example for a dataset named WORK.CLASS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
  set sashelp.class;
run;
proc datasets library=work nolist;
  modify class;
    rename name=v1 sex=v2 age=v3  height=v4 weight=v5;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also dynamically discover the old variable names and generate the corresponding RENAME statement, which could then the %INCLUDEd in the PROC DATASETS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
  set sashelp.class;
run;
filename tmp temp;
data _null_;
  if 0 then set class;
  length _vname $20;
  file tmp;
  put 'rename ' @;
  do i=1 by 1 until (upcase(_vname)='_VNAME');
   call vnext(_vname);
   if upcase(_vname)='_VNAME' then leave;
   put _vname +(-1) '=V' i  @;
  end;
  put ';';
run;
proc datasets library=work  nolist;
  modify class;
    %include tmp / source2;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "IF 0 then SET statement" reads no data, but it doesn't have to.&amp;nbsp; It does force the SAS compiler to generate the program data vector (i.e. the list of variables), That can be sequentially examined via the CALL VNEXT statement, which allows for generation of expressions like&amp;nbsp; "name=v1", "sex=v2", etc.&amp;nbsp; &amp;nbsp;Write those expressions to a temporary text file, and %INCLUDE the result having all the renames in the subsequent PROC DATASETS.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 19:21:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805918#M81716</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-04T19:21:25Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805919#M81717</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When not use &lt;FONT face="courier new,courier"&gt;name--weight&lt;/FONT&gt; and skip the renaming? Much less typing.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 19:35:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805919#M81717</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-04T19:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805924#M81718</link>
      <description>Thank you!</description>
      <pubDate>Mon, 04 Apr 2022 19:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805924#M81718</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2022-04-04T19:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805952#M81721</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When not use &lt;FONT face="courier new,courier"&gt;name--weight&lt;/FONT&gt; and skip the renaming? Much less typing.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To be honest, I just accepted the OP's request for renaming, without any further thought.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But as I think about it a little more, using &lt;FONT face="courier new,courier"&gt;name--weight&lt;/FONT&gt; requires knowledge of variable names in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 22:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805952#M81721</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-04T22:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805953#M81722</link>
      <description>&lt;P&gt;Easy to do, but WHY?&lt;/P&gt;
&lt;P&gt;So assuming your dataset is named HAVE and it is in the WORK library then this code will generate a series of old=new name pairs into a macro variable which can then be used in a RENAME statement or RENAME= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',nliteral(name),cats('v',varnum))
  into :rename separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE'
    and lowcase(name) ne cats('v',varnum)
;
quit;
proc dataset nolist lib=WORK;
  modify HAVE ;
  rename &amp;amp;rename;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Apr 2022 22:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/805953#M81722</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-04T22:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/806011#M81723</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When not use &lt;FONT face="courier new,courier"&gt;name--weight&lt;/FONT&gt; and skip the renaming? Much less typing.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To be honest, I just accepted the OP's request for renaming, without any further thought.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But as I think about it a little more, using &lt;FONT face="courier new,courier"&gt;name--weight&lt;/FONT&gt; requires knowledge of variable names in advance.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes it does require the names of two variables in advance, and I assume the user already knows that since he/she was going to type out all 42 variable names and then decided to ask the question about renaming to V1 V2 V3 ... .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I find this another example of the &lt;A href="https://xyproblem.info/" target="_self"&gt;XY problem&lt;/A&gt;, where the user&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22174"&gt;@dustychair&lt;/a&gt; is extremely fixated on a specific task that is unnecessary (and in this case, I believe harmful, to convert meaningful variable names in to meaningless V1 V2 V3 ...), rather than asking us about the broader problem in which case perhaps making meaningless variable names or renaming isn't really needed and much easier solutions are available.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 09:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/806011#M81723</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-05T09:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/806012#M81724</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Why is PROC DATASETS needed here? Just extract the variable names using PROC SQL into a macro variable without renaming, and use the macro variable.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 10:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/806012#M81724</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-05T10:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: changing variables' names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/806063#M81725</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Why is PROC DATASETS needed here? Just extract the variable names using PROC SQL into a macro variable without renaming, and use the macro variable.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a question for the OP.&amp;nbsp; It is hard to perform a rename without actually performing a rename.&amp;nbsp; You could also use the OLD=NEW pairs in a RENAME= dataset option.&amp;nbsp; For example IF the reason for the renaming is the need to deliver a dataset using those new names you might just do something like one of these data steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have(rename=(&amp;amp;rename));
run;

data want;
  set have ;
  rename &amp;amp;rename;
run;

data want(rename=(&amp;amp;rename));
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2022 14:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/changing-variables-names/m-p/806063#M81725</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-05T14:10:43Z</dc:date>
    </item>
  </channel>
</rss>

