<?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: Renaming variable with a substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561688#M157292</link>
    <description>&lt;P&gt;The variables are in two different&amp;nbsp;tables sorry i'm changing on the topic but they have to have the same name because i'm appending them after !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For understand what i'm doing this is an example :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ;
format namevar_1_1 1. namevar_1_2 1. namevar_1_3 1.;
input namevar_1_1 namevar_1_2 namevar_1_3;
cards;
1 3 2
2 . .
3 1 .
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Each line is a response make by a respondent (for a inverstigation).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is 3 variables but it the same question :&lt;/P&gt;&lt;P&gt;line 1 : The respondent put modality 1 at FIRST CHOICE&lt;/P&gt;&lt;P&gt;Modality 3 on SECOND CHOICE and modality 2 at last choice (he made 3 choices on this order 1 3 2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 2 : An other respondent just put the modality 2 at first choice and he doesn't put another choice&lt;/P&gt;</description>
    <pubDate>Mon, 27 May 2019 08:34:03 GMT</pubDate>
    <dc:creator>Onizuka</dc:creator>
    <dc:date>2019-05-27T08:34:03Z</dc:date>
    <item>
      <title>Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561686#M157290</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(sorry for my english)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want rename some variables and delete the last 2 characters (or one character, look at below) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example, I have a variable : namevar_1_1 and an other namevar_1_10.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to do is rename these variables to delete the caracters after the last underscore :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;namevar_1_1 becomes namevar_1_&lt;/P&gt;&lt;P&gt;namevar_1_10 becomes namevar_1_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can we do this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I dont have any idea.. I tryed by going through a rename using SUBSTR but doesn't work..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here a data set example :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ;
format namevar_1_1 1. ;
input namevar_1_1 ;
cards;
1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ; 
format namevar_1_10 1. ; 
input namevar_1_10 ; 
cards; 
1 
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank's in advance !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Onizuka&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 08:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561686#M157290</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-05-27T08:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561687#M157291</link>
      <description>&lt;P&gt;Two variables can not have the same name. You are asking to name both the variables&amp;nbsp;&lt;SPAN&gt;namevar_1_?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 08:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561687#M157291</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-27T08:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561688#M157292</link>
      <description>&lt;P&gt;The variables are in two different&amp;nbsp;tables sorry i'm changing on the topic but they have to have the same name because i'm appending them after !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For understand what i'm doing this is an example :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ;
format namevar_1_1 1. namevar_1_2 1. namevar_1_3 1.;
input namevar_1_1 namevar_1_2 namevar_1_3;
cards;
1 3 2
2 . .
3 1 .
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Each line is a response make by a respondent (for a inverstigation).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is 3 variables but it the same question :&lt;/P&gt;&lt;P&gt;line 1 : The respondent put modality 1 at FIRST CHOICE&lt;/P&gt;&lt;P&gt;Modality 3 on SECOND CHOICE and modality 2 at last choice (he made 3 choices on this order 1 3 2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;line 2 : An other respondent just put the modality 2 at first choice and he doesn't put another choice&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 08:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561688#M157292</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-05-27T08:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561691#M157294</link>
      <description>&lt;P&gt;But here, you still have all the variables in the same data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please be more specific about your requirements &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Show us a representation of what you &lt;STRONG&gt;have&amp;nbsp;&lt;/STRONG&gt;and what you &lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 08:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561691#M157294</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-27T08:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561694#M157297</link>
      <description>&lt;P&gt;Yes sorry I'm not explaining very well ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm treating the results question by question so my &lt;STRONG&gt;REAL QUESTION is : &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;How can we rename a variable for keep what is before the last "_" :&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;namevar_1_1 : namevar_1_ &lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let say that I have only one table with only this variable &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 08:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561694#M157297</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-05-27T08:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561709#M157304</link>
      <description>&lt;P&gt;It work doing like this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ;
format namevar_1_1 1. ;
input namevar_1_1  ;
cards;
1
;

Proc contents data = have out = c_have (keep = name) noprint ; run ;

Data want ;
set c_have ;
name1 = COMPRESS(SCAN(name, 1, '_')!!"_"!!SCAN(name,2,'_')!!"_");
run;

Proc sql noprint ; select name1 into: name1 from want ; quit ;

data have2 ;
set have ;
rename namevar_1_1 = &amp;amp;name1. ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 May 2019 09:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561709#M157304</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-05-27T09:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561758#M157312</link>
      <description>&lt;P&gt;You are moving in the right direction.&amp;nbsp; However, it's a little more complicated than what you have done:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You assume you already know the name of the original variable in the RENAME statement.&lt;/LI&gt;
&lt;LI&gt;You may be assuming that all variable names contain at least two underscores.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here is a more general version of what you are attempting:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=c_have (keep=name) noprint;
run;

file renames temp;

data _null_;
set c_have;
if countw(name, '_') &amp;gt; 2;
len_newname = length(name) - length(scan(name, -1, '_'));
newname = substr(name, 1, len_newname);
file renames noprint;
put 'rename ' name '=' newname ';' ;
run;

data want;
set have;
%include renames;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is untested code, so you will need to try it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, it assumes that you won't run into name conflicts, such as original variable names that include both var_1_1 and var_1_2 both being renamed to var_1.&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 14:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/561758#M157312</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-27T14:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562208#M157475</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;You are moving in the right direction.&amp;nbsp; However, it's a little more complicated than what you have done:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;You assume you already know the name of the original variable in the RENAME statement.&lt;/LI&gt;&lt;LI&gt;You may be assuming that all variable names contain at least two underscores.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Also, it assumes that you won't run into name conflicts, such as original variable names that include both var_1_1 and var_1_2 both being renamed to var_1.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your answer !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first step works well and it put this on the log :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: The file RENAMES is:
      Nom du fichier=C:\Users\XXXX\renames.dat, /*renames.dat */
      RECFM=V,LRECL=32767,
      Taille de fichier (octets)=0,
      Modifié(e) le=29 mai 2019 11 h 21,
      Heure de création=29 mai 2019 11 h 16&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but it doesn't work on the last step :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
%include renames;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here the log error :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;71   data want;
72   set have;
73   %include renames;
WARNING: Physical file does not exist, C:\Users\XXXX\renames.sas. /*renames.sas*/
ERROR: Cannot open %INCLUDE file RENAMES.
74   run;

&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:25:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562208#M157475</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-05-29T09:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562266#M157496</link>
      <description>&lt;P&gt;Looks like you skipped the line that defined RENAMES as a fileref.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename renames temp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the data step step took as 'renames' as the base name for an output data file and add '.dat' extension. And the %include figured that renames was the base name for the SAS program file and added '.sas' extension and so did not find the file the data step wrote.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 13:38:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562266#M157496</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-29T13:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562268#M157498</link>
      <description>&lt;P&gt;The surest, most flexible solution is to replace this statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename renames temp;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead, pick a location where you would like to store all the renaming code.&amp;nbsp; Select an existing folder, and a filename within that folder.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename renames 'path_to_folder\renm.sas';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just supply the name of the folder and file that you select within the quotes.&amp;nbsp; This also lets you examine the file later, to better understand how the program is working.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 13:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562268#M157498</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-29T13:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable with a substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562301#M157508</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;The surest, most flexible solution is to replace this statement:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename renames temp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Instead, pick a location where you would like to store all the renaming code.&amp;nbsp; Select an existing folder, and a filename within that folder.&amp;nbsp; For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename renames 'path_to_folder\renm.sas';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Just supply the name of the folder and file that you select within the quotes.&amp;nbsp; This also lets you examine the file later, to better understand how the program is working.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Thank you for the answer, i will try your solution using this, thank you very much &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 14:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-with-a-substr/m-p/562301#M157508</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-05-29T14:38:51Z</dc:date>
    </item>
  </channel>
</rss>

