<?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: Changing variable names across datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667073#M199697</link>
    <description>&lt;P&gt;You tell us you need a counter but the sample data you're posting indicates that you just want to maintain the already existing numbering on variables when renaming them.&lt;/P&gt;
&lt;P&gt;Here your &lt;EM&gt;have&lt;/EM&gt;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;txnmy_cd txnmy_cd2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here your &lt;EM&gt;want&lt;/EM&gt;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;Taxonomy Taxonomy2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If one would use a counter then why is there no &lt;EM&gt;Taxonomy1&lt;/EM&gt; in your &lt;EM&gt;want&lt;/EM&gt; data? What are the rules? Please don't just repeat what you've already told us but try to reformulate and further specify what you have and what you want - and the detailed logic in words how to get from have to want.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Jul 2020 00:41:30 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-07-06T00:41:30Z</dc:date>
    <item>
      <title>Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665943#M199189</link>
      <description>&lt;P&gt;I currently have upwards of 10+ datasets, each of which have a list of variables like 2 seen below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Variable Names
ID|BusName|PIN|Diag_cd1|Diag_cd2|Diag_cd3|Diag_cd4|Diag_cd5|Diag_cd6|Diag_cd7|Diag_cd8|Spec_cd1|Spec_cd2|Spec_cd3|Spec_cd4|Address1|Address2|City|State|Zip|Zip4
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Variable Names
ID|BusName|PIN|Diag_cd|Diag_cd1|Diag_cd2|Diag_cd4|Diag_cd5|Diag_cd6|Diag_cd7|Diag_cd8|Spec_cd|Spec_cd2|Spec_cd3|Spec_cd4|Address1|Address2|City|State|Zip|Zip4
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and so on...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was told there is a more efficient way to rename my variables than using proc format. Instead, they told me to&lt;/P&gt;
&lt;P&gt;1) use proc contents to get a list of variable names&lt;/P&gt;
&lt;P&gt;2) sort datasets by all variables&lt;/P&gt;
&lt;P&gt;2) create a counter variable&lt;/P&gt;
&lt;P&gt;3) create a rename variable using the counter&lt;/P&gt;
&lt;P&gt;4) Use conditional logic to rename the prefix&amp;nbsp; (Assigned [e.g. Assigned_ID]) on ID, BusName, PIN, address, city, state, zip, etc&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Ex)&amp;nbsp;if index(Add) then new_name=Assigned_Address1; if upcase(NAME) then new_name=cat("Assigned",name)&lt;/P&gt;
&lt;P&gt;5)Obtain 1 column with original variable names and another column for renamed variables&lt;/P&gt;
&lt;P&gt;6) Use proc sql to create a renaming structure to generate renamed variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=rawdata.raw_&amp;amp;file. out=contents_&amp;amp;file. (keep=NAME) noprint;
run;&lt;BR /&gt;
proc sort data=contents_&amp;amp;file.;  &lt;BR /&gt;     by _all_; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm a little lost on part 3 and on...specifically how to assign the counter using pieces of NAME to rename.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;End result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Variable Name|New Variable Name
ID|Assigned_ID
Diag_cd1|Assigned_DX1
Diag_cd2|Assigned_DX2
Diag_cd3|Assigned_DX3
...
Spec_cd1|Assigned_SPCD1
Spec_cd2|Assigned_SPCD2
Spec_cd3|Assigned_SPCD3
...
Address1|Assigned_Address1
Address2|Assigned_Address2
City|Assigned_City
State|Assigned_State
Zip|Assigned_Zip
Zip4|Assigned_Zip4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Variable Name|New Variable Name
ID|Assigned_ID
Diag_cd|Assigned_DX1
Diag_cd2|Assigned_DX2
Diag_cd3|Assigned_DX3
...
Spec_cd|Assigned_SPCD1
Spec_cd1|Assigned_SPCD2
Spec_cd2|Assigned_SPCD3
...
Address1|Assigned_Address1
Address2|Assigned_Address2
City|Assigned_City
State|Assigned_State
Zip|Assigned_Zip
Zip4|Assigned_Zip4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 22:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665943#M199189</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2020-06-29T22:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665948#M199192</link>
      <description>&lt;P&gt;If you are going to use SQL and recreate the tables, and if the columns are always in the right order, it much simpler to use a UNION.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table NEW as 
  select * from TABLE_WITH_GOOD_NAMES (obs=0)
  union
  select * from TABLE_WITH_BAD_NAMES;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 23:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665948#M199192</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-06-29T23:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665951#M199193</link>
      <description>&lt;P&gt;You can likely get all the information you need with a single step using either Proc SQL and querying Dictionary.columns or a data step using SASHELP.Volumn. This data view, the Vcolumn version has all of the variables and there properties in all of the data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data need;
   set sashelp.vcolumn;
   where libname='MAPS' and memname =: 'A';
run;&lt;/PRE&gt;
&lt;P&gt;will the details of all variables of al data sets (memname) that start with "A" in the library 'MAPS'. Libname and memname are stored in upper case so select appropriately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then something along these lines.&lt;/P&gt;
&lt;PRE&gt;&amp;lt;use what ever logic to create old name/new name pairs
from that Need data set&amp;gt;
&amp;lt;use that data set in a data _null_ step to create
 the following statments using Call Execute
&amp;gt;

Proc datasets library='MAPS';
   modify &amp;lt;dataset name goes here&amp;gt;;
   rename
      oldvar1=newvarname1
      oldvar2=newvarname2
      ....
   ;
   modify &amp;lt;next dataset name&amp;gt; ;
   rename
      oldvar1=newvarname1
      oldvar2=newvarname2
      ....
   ;
  &amp;lt;repeat until you run out data sets&amp;gt;
run;
quit;
&lt;/PRE&gt;
&lt;P&gt;Searching the forum for "Proc Datasets" rename you can find&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374935" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374935&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/column-rename-macro/m-p/493320" target="_blank"&gt;https://communities.sas.com/t5/SAS-Enterprise-Guide/column-rename-macro/m-p/493320&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-rename-all-the-column-names-of-a-dataset/m-p/496616" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-rename-all-the-column-names-of-a-dataset/m-p/496616&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;with bits and pieces similar to what you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data _null_ using a BY MEMNAME lets you have "if first.memname then do" to write the stuff that starts with the data set name change and an "if last.memname then do" to end the rename block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 23:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665951#M199193</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-29T23:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665952#M199194</link>
      <description>&lt;P&gt;You have already accepted a solution for dynamically rename a list of variable names&lt;/P&gt;
&lt;P&gt;in post:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Dynamically-change-the-name-of-variables/m-p/663795#M198229" target="_self"&gt;https://communities.sas.com/t5/SAS-Programming/Dynamically-change-the-name-of-variables/m-p/663795#M198229&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably can use same method, of creating an informat, for those new datasets.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 23:23:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665952#M199194</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-06-29T23:23:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665955#M199196</link>
      <description>&lt;P&gt;Building on the code shared &lt;A href="https://communities.sas.com/t5/SAS-Programming/Dynamically-change-the-name-of-variables/m-p/663795#M198229" target="_self"&gt;here&lt;/A&gt; you basically could just add a special type of informat which does the renaming for you using Regular Expressions.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
  array vars {*} 
    ID otherVar
    Diag_cd Diag_cd1 Diag_cd2 Diag_cd3 
    Spec_cd1 Spec_cd2 Spec_cd3 
    Address1 Address2 City State Zip Zip4;
  stop;
run;

data have2;
  array vars {*} ProvID NPI txnmy_cd;
  stop;
run;


%macro renameVars(tbl,infmt_stdz);
  %local lib;
  %let lib=%upcase(%scan(WORK.&amp;amp;tbl,-2,.));
  %let tbl=%upcase(%scan(&amp;amp;tbl,-1,.));

  %local rename_vars;
  %let rename_vars=;
  proc sql noprint;
    select cats(name,'=',input(name,$&amp;amp;infmt_stdz.32.)) 
      into :rename_vars separated by ' '
    from dictionary.columns
    where 
      libname="&amp;amp;lib" 
      and memname="&amp;amp;tbl"
      and not missing(input(name,$&amp;amp;infmt_stdz.32.))
    ;
  quit;
  
  %if %nrbquote(&amp;amp;rename_vars) ne %nrbquote() %then
    %do;
      proc datasets lib=&amp;amp;lib nolist;
        modify &amp;amp;tbl;
          rename &amp;amp;rename_vars;
        run;
      quit;
    %end;
%mend;

proc format;
  invalue $varname_stdz(default=32 upcase notsorted)
    'TXNMY_CD'  = 'Taxonomy'
    'TXNMY_CD1' = 'Taxonomy'
    'TXNMY_CD2' = 'Taxonomy2'
    other=' '
    ;  
  invalue $varname_prefix(default=32 notsorted)
    /* exclusions */
    's/ID|otherVar//i' (regexpe)      = _same_
    /* renames */
    's/^Diag_cd(.*)/Assigned_DX$1/i' (regexpe) = _same_
    's/^Spec_cd(.*)/Assigned_SPCD$1/i' (regexpe) = _same_
    's/^(.*)/Assigned_$1/i' (regexpe)    = _same_
    other=' '
    ;
run;

data _null_;
  infile datalines dsd dlm=' ' truncover;
  input tbls :$41.;
  rc=dosubl(cats('%renameVars(',tbls,',varname_stdz);'));
  rc=dosubl(cats('%renameVars(',tbls,',varname_prefix);'));
  datalines;
have1
work.have2
haveNot
;

proc contents data=have1 varnum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1593476110308.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46780iC94B3CEDABF2B96E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1593476110308.png" alt="Patrick_0-1593476110308.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 00:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/665955#M199196</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-30T00:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666646#M199495</link>
      <description>&lt;P&gt;While the solutions posted previously in my thread and suggested here by Patrick and Shmuel were great, I was advised from the person looking over my data that I should avoid manually creating a list of rename, manually creating an array listing out my variables, and/or creating a format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, I was advised to approach the renaming of my variables differently:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a counter for my variables where they start with either txnmy_cd/txnmy_cd1 and go on to n+1. Create a rename structure to provide a prefix to other variables (e.g. Address1 to ProvAddress1) and renaming other instances like Prov_type to Provtype. I think this can be accomplished with either a combination of proc contents to obtain a variable list and/or proc sql dictionary columns to obtain my list of variables. The issue I encounter is creating a counter then do loop after I run my proc contents or dictionary.columns approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 16:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666646#M199495</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2020-07-02T16:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666649#M199496</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;While the solutions posted previously in my thread and suggested here by Patrick and Shmuel were great, I was advised from the person looking over my data that I should avoid manually creating a list of rename, manually creating an array listing out my variables, and/or creating a format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, I was advised to approach the renaming of my variables differently:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a counter for my variables where they start with either txnmy_cd/txnmy_cd1 and go on to n+1. Create a rename structure to provide a prefix to other variables (e.g. Address1 to ProvAddress1) and renaming other instances like Prov_type to Provtype. I think this can be accomplished with either a combination of proc contents to obtain a variable list and/or proc sql dictionary columns to obtain my list of variables. The issue I encounter is creating a counter then do loop after I run my proc contents or dictionary.columns approach.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is there a pattern to the issues you are having with the names?&amp;nbsp; I am not sure I see a fixed pattern in your examples.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For some you seem to be saying that some variables that do not end in any numeric suffix should be renamed to add 1 to the end of the name.&amp;nbsp; Do you want to do that for every variable that does not end in a numeric suffix?&amp;nbsp; Or only when there are some other existing variables that do end a numeric suffix?&amp;nbsp; If the latter are you positive that there are not "basenames" that include a numeric suffix already?&amp;nbsp; For example do you ever see patterns like: q11, q12, q13, q2, q31,q32 q33,q34&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For others you seem to be saying that you should remove underscores from the variable name.&amp;nbsp; Do you want to remove all underscores?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the source of the inconsistency in the variables names?&amp;nbsp; Is this a one time fix for a finite set of existing datasets? Or do you expect to get new datasets in the future where similar types of renamings will be required?&lt;/P&gt;
&lt;P&gt;I suspect that any solution where the source data can change will require some type of review process to verify that your renaming rules actually work for the new datasets.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 17:23:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666649#M199496</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-02T17:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666660#M199503</link>
      <description>&lt;P&gt;There is some pattern with the names.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Taxonomy codes and spec_cd both either start with txnmy_cd or txnmy_cd1 and spec_cd or spec_cd1 and continue for n+1.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;underscores should be removed for everything&lt;/LI&gt;
&lt;LI&gt;Some variables are not changed at all (e.g. ID, PIN)&lt;/LI&gt;
&lt;LI&gt;other variables (address) have a prefix 'Prov'&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;There will be future iterations of the process which may include new datasets but they will follow similar naming conventions.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 18:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666660#M199503</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2020-07-02T18:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666921#M199633</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There is some pattern with the names.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Taxonomy codes and spec_cd both either start with txnmy_cd or txnmy_cd1 and spec_cd or spec_cd1 and continue for n+1.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;underscores should be removed for everything&lt;/LI&gt;
&lt;LI&gt;Some variables are not changed at all (e.g. ID, PIN)&lt;/LI&gt;
&lt;LI&gt;other variables (address) have a prefix 'Prov'&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;There will be future iterations of the process which may include new datasets but they will follow similar naming conventions.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You need to clearly define the patterns and the desired result. If you do that then we can use regular expressions to match such patterns.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Taxonomy codes and spec_cd both either start with txnmy_cd or txnmy_cd1 and spec_cd or spec_cd1 and continue for n+1&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;So if it starts with&amp;nbsp;&lt;EM&gt;txnmy_cd &lt;/EM&gt;do you now want the first variable to become&amp;nbsp;&lt;EM&gt;txnmy_cd1&amp;nbsp;&lt;/EM&gt; then then the 2nd variable -&amp;nbsp;&lt;EM&gt;txnmy_cd1&amp;nbsp;&lt;/EM&gt;- to become&amp;nbsp;&lt;EM&gt;txnmy_cd2?&lt;/EM&gt; Please provide representative sample data (via a SAS data step) and then show us the desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Create a rename structure to provide a prefix to other variables&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can either do this within code and if/then/else statements or you do it data driven so you don't have to change the code to add additional patterns. Using an Informat especially with the RegEx is a way to implement something data driven.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jul 2020 02:15:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/666921#M199633</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-04T02:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667038#M199685</link>
      <description>&lt;P&gt;Have:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ProvID NPI Txnmy_cd1 Txnmy_cd2 Spec_cd1 Spec_cd2 Address1 Address2 City State Zip Zip4 Prov_type&amp;nbsp;Pcp_flg&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dataset 2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ProvID NPI txnmy_cd txnmy_cd2 Spec_cd Spec_cd1 Address1 Address2 City State Zip Zip4 Prov_type&amp;nbsp;Pcp_flg&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and so on... (all datasets follow similar structure)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;P&gt;Dataset 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ProvID NPI Taxonomy Taxonomy2 ProvSpec1 ProvSpec2 ProvAddress1 ProvAddress2 ProvCity ProvState ProvZip ProvZip4 Provtype PCP_flg&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dataset 2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ProvID NPI Taxonomy Taxonomy2 ProvSpec1 ProvSpec2 ProvAddress1 ProvAddress2 ProvCity ProvState ProvZip ProvZip4 Provtype pcp_flg&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm attempting to make this data driven as much as possible but to avoid using informat, format, or manually renaming statements since future years may have more datasets and/or variables or the layout may change. From what I was told my the person overlooking the project is that I should consider a counter variable for the taxonomy and spec_cd, an index with a combined if/else statement for address variables, and then a rename structure (proc sql) to rename the variables.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 19:30:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667038#M199685</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2020-07-05T19:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667041#M199687</link>
      <description>&lt;P&gt;I suspect part of your problem with writing the code it not quite knowing how to describe the problem.&lt;/P&gt;
&lt;P&gt;Step 1 is getting the variable names into data.&amp;nbsp; If you start with actual datasets then use PROC CONTENTS or access the SAS metadata (via DICTIONARY.COLUMNS or SASHELP.VCOLUMN is not using SQL code).&amp;nbsp; If your source is the first row of a text file then just read the first line of the file using trailing&amp;nbsp;@@ on the input statement.&amp;nbsp; Whichever method you use create a dataset with one observation per variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 2 is locating the names that you want to rename.&amp;nbsp; To do that you need to classify each variable that ends with a numeric suffix to its corresponding basename.&amp;nbsp; One way to do that is using PRCHANGE() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;basename = prxchange('s/\d*$//',-1,trim(name));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Step 3 is figuring out whether you want to change the basename.&amp;nbsp; It looks like there are three variables where you don't want to change the base name.&amp;nbsp; And for the others you want to add Prov prefix to the names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if lowcase(basename) not in ('provID','npi','txnmy_cd') then basename='Prov'||basename;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Step 3 is add the numeric suffix to the variables in a consistent manner.&amp;nbsp;&amp;nbsp;It is still not clear to me if you &lt;STRONG&gt;only&lt;/STRONG&gt; want to generate the numeric suffix when there are more than one instance of the variable or not.&amp;nbsp; But since not adding the suffix is probably how you got into this mess to begin with then perhaps you should always add the suffix.&amp;nbsp; &amp;nbsp;Also it is not clear if you ever receive gaps in the series (like B1,B2,B6,B7).&amp;nbsp; Again I would just eliminate the gaps and number then consistently.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.basename then suffixnum=1;
nename=cats(basename,suffixnum);
suffixnum+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have all of the NAME,NEWNAME pairs then just use that information to rename the variables.&amp;nbsp; How to do that depends on what you are doing.&amp;nbsp; If you are trying to combine the datasets you could do it with RENAME=() dataset option.&amp;nbsp; If you are copying the datasets to new datasets you could do it with RENAME statement , or RENAME=() dataset option on either the input or the output dataset.&amp;nbsp; If you are trying to modify the names in an existing dataset then do it with the RENAME statement within a MODIFY group in PROC DATASETS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So see if you can generate a dataset that has the old NAME and generated NEWNAME.&amp;nbsp; Once you have that the rest is easy and there are many examples on this forum of ways to do that actual renaming.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 20:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667041#M199687</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-05T20:10:20Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667073#M199697</link>
      <description>&lt;P&gt;You tell us you need a counter but the sample data you're posting indicates that you just want to maintain the already existing numbering on variables when renaming them.&lt;/P&gt;
&lt;P&gt;Here your &lt;EM&gt;have&lt;/EM&gt;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;txnmy_cd txnmy_cd2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here your &lt;EM&gt;want&lt;/EM&gt;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;Taxonomy Taxonomy2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If one would use a counter then why is there no &lt;EM&gt;Taxonomy1&lt;/EM&gt; in your &lt;EM&gt;want&lt;/EM&gt; data? What are the rules? Please don't just repeat what you've already told us but try to reformulate and further specify what you have and what you want - and the detailed logic in words how to get from have to want.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 00:41:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667073#M199697</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-06T00:41:30Z</dc:date>
    </item>
    <item>
      <title>Re: Changing variable names across datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667537#M199913</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your response. You are right; I am having trouble communicating the problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 1 and 2 are no issues. I am able to generate a list of variable names, review the list, and identify the renaming that is required as seen below.&lt;/P&gt;
&lt;P&gt;Dataset 1:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="A_Swoosh_2-1594146550778.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46981iE7203BD5CFB02956/image-size/medium?v=v2&amp;amp;px=400" role="button" title="A_Swoosh_2-1594146550778.png" alt="A_Swoosh_2-1594146550778.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Dataset 2:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="A_Swoosh_0-1594146456018.png" style="width: 163px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46979iC4D8220F6BBEC20F/image-dimensions/163x379?v=v2" width="163" height="379" role="button" title="A_Swoosh_0-1594146456018.png" alt="A_Swoosh_0-1594146456018.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;data new; 
        set contents_&amp;amp;file.;
         if upcase(name) in ('ADDRESS1' 'ADDRESS2' 'BUSNAME' 'CITY' 
          'COUNTY' 'FNAME' 'LNAME' 'MI' 'STATE' 'ZIP' 'ZIP4') 
         then basename='Prov'||name; else basename=name;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;I want to rename txnmy_cd and spec_cd using a numeric suffix since they have more than one instance of the variable beginning with:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Taxonomy, Taxonomy2, Taxonomy3, etc.&lt;/LI&gt;
&lt;LI&gt;ProvSpec1, ProvSpec2, ProvSpec3, etc.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I also want to remove the underscore from prov_type also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition, I am trying to remove those weird naming conventions in dataset 2 (e.g. CountyA_B, PCP_flagD) to follow the format of dataset 1.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;I was considering the use of index function to do this. I'm not sure if there is a more efficient way.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Also, dataset 2 is slightly different so the code listed above won't work on that dataset. Using this function below, I don't know if this quite works for my address and zip variables.&lt;/P&gt;
&lt;PRE&gt;basename = prxchange('s/\d*$//',-1,trim(name));&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I think once I am able to produce a column right next to the original name column, I can use proc sql to rename the column variables as the new column variables.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jul 2020 18:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-variable-names-across-datasets/m-p/667537#M199913</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2020-07-07T18:46:05Z</dc:date>
    </item>
  </channel>
</rss>

