<?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 Appending a string to all variable names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298395#M62753</link>
    <description>&lt;P&gt;Hi. Is there a way to preface every variable name in my dataset with a fixed string? &amp;nbsp;For instance I would like to append 'a_' in front of all variable in my dataset regardless of how many variable make up the dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the end I would need the variable names in the dataset to be a_name and a_dept.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data person;
   infile datalines delimiter=','; 
   input name $ dept $;
   datalines;                      
John,Sales
Mary,Acctng
;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Sep 2016 18:16:30 GMT</pubDate>
    <dc:creator>buechler66</dc:creator>
    <dc:date>2016-09-14T18:16:30Z</dc:date>
    <item>
      <title>Appending a string to all variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298395#M62753</link>
      <description>&lt;P&gt;Hi. Is there a way to preface every variable name in my dataset with a fixed string? &amp;nbsp;For instance I would like to append 'a_' in front of all variable in my dataset regardless of how many variable make up the dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the end I would need the variable names in the dataset to be a_name and a_dept.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data person;
   infile datalines delimiter=','; 
   input name $ dept $;
   datalines;                      
John,Sales
Mary,Acctng
;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298395#M62753</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-09-14T18:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a string to all variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298397#M62755</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person;
   infile datalines delimiter=','; 
   input name $ dept $;
   datalines;                      
John,Sales
Mary,Acctng
;;;;
   run;
*Make a list of names;
proc transpose data=person(obs=0) out=varnames;
   var _all_;
   run;
*Gen the name=newname value pairs;
proc sql noprint;
   select catx('=',nliteral(_name_),nliteral(cats('A_',_name_))) 
      into :renamelist separated by ' '
      from varnames;
   quit;
   run;
%put NOTE: &amp;amp;=renamelist;
*Modify person;
proc datasets;
   modify person;
   rename &amp;amp;renamelist;
   run;
   contents data=person;
   run;
   quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298397#M62755</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-09-14T18:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a string to all variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298399#M62757</link>
      <description>&lt;P&gt;Do any of your existing variables have names of 31 characters or longer length? If so you may have to decide what to do with the prefixing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose you have two variables named:&lt;/P&gt;
&lt;P&gt;A_Somewhat_longwinded_variableA&lt;/P&gt;
&lt;P&gt;A_Somewhat_longwinded_variableB&lt;/P&gt;
&lt;P&gt;These are&amp;nbsp; 31 characters long. If you prefix them with A_ the length would become 33 characters and exceed the length of a valid variable name by 1. ALSO they would be identical for the 32 characters allowed. SAS datasets will not allow two or more variables with the same name. So you may have to provide some additional coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you never have variables that exceed 30 characters you are okay but if you start using longer prefixes the likelihood of encountering this name length/ duplication issue increases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298399#M62757</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-14T18:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a string to all variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298400#M62758</link>
      <description>&lt;P&gt;Awesome help! &amp;nbsp;Ty! &amp;nbsp;Any thoughts on handling a variable name that is already 31 or 32 chars long? &amp;nbsp;In this case I'm getting the error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1018     rename &amp;amp;renamelist;
ERROR: "A_A_PC_SCN_BEF_UNLD_SCAN_EXCL_IND" contains more than 32 characters.
ERROR: "A_A_BMEU_PC_SCAN_BEF_AET_EXCL_IND" contains more than 32 characters.
ERROR: "A_A_INVLD_CTR_LVL_ENT_FAC_EXCL_IND" contains more than 32 characters.
ERROR: "A_A_DSTNC_ORGN_FAC_DESTN_PLNT_ZIP" contains more than 32 characters.
ERROR: "A_A_DSTNC_ORGN_ZIP_DESTN_PLNT_ZIP" contains more than 32 characters.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Would there be anyway to just have it truncate the last to variable characters to make room for the 'a_'?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298400#M62758</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-09-14T18:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a string to all variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298401#M62759</link>
      <description>Yes!!! You anticipated my very next problem!</description>
      <pubDate>Wed, 14 Sep 2016 18:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298401#M62759</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-09-14T18:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a string to all variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298694#M62853</link>
      <description>&lt;P&gt;This will TRUNCATE the rename to 32 characters&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select (catx('=',nliteral(_name_),nliteral(substr(cats('A_',_name_),1,32)))) 
      into :renamelist separated by ' '
      from varnames;
   quit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However if you have any names where the last few characters are critical then you will likely want to pass the VARNAMES data set through a data step. I would sort by the Name to get similar names in order an selectively replace some text to abbreviate but you would have to make those decisions based on what you know of your data. Possibly something like&amp;nbsp;further abbreviating EXCL to EX or ZIP to ZP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2016 15:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-string-to-all-variable-names/m-p/298694#M62853</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-15T15:39:02Z</dc:date>
    </item>
  </channel>
</rss>

