<?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: Change column names to abide by naming conventions with code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612728#M178839</link>
    <description>&lt;P&gt;Can you show us what you have tried?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suggestion, query the dataset using Proc sql re contents as below and search for the patterns you need and then do appropriate rename&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents
     data = sashelp.class
          noprint
          out = var_list
               (keep = name );
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 18 Dec 2019 16:22:43 GMT</pubDate>
    <dc:creator>ghosh</dc:creator>
    <dc:date>2019-12-18T16:22:43Z</dc:date>
    <item>
      <title>Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612726#M178838</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem that I think can be solved with code. I have to go through each variable in a dataset and rename them according to a couple rules. They are as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Issue: &lt;/STRONG&gt;More than 3 underscores “_”&lt;/LI&gt;&lt;/UL&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Resolution: &lt;/STRONG&gt;Rename the variable with only 3 underscores.&lt;/LI&gt;&lt;/UL&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Issue: &lt;/STRONG&gt;Time period location on a variable name&lt;/LI&gt;&lt;/UL&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Resolution:&lt;/STRONG&gt; Rename the variables where PER or OBS is at the end of the variable/ suffix.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;For example, if a variable is named CNT_FIRST_SECOND_THIRD_FOURTH it would have to be changed to CNT_FIRSTSECOND_THIRD_FOURTH. We're not picky on which underscore has to go as long as the number of underscores is 3 or less. An example of the second rule would be if we had a variable named CNT_FIRST_OBS_SECOND it would have to be rearranged where OBS is placed at the end of the variable name as in CNT_FIRST_SECOND_OBS. Let me know if there's a way to solve this with code. I'll be watching closely. Thanks ahead of time for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 16:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612726#M178838</guid>
      <dc:creator>Alexander2</dc:creator>
      <dc:date>2019-12-18T16:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612728#M178839</link>
      <description>&lt;P&gt;Can you show us what you have tried?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suggestion, query the dataset using Proc sql re contents as below and search for the patterns you need and then do appropriate rename&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents
     data = sashelp.class
          noprint
          out = var_list
               (keep = name );
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2019 16:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612728#M178839</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2019-12-18T16:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612734#M178843</link>
      <description>&lt;P&gt;i think countw() and scan() will solve both problems.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the countw() function allows you to determine if issue 1 is a problem for a given variable. in a loop, i'd use something like if ge 4 (with delimitter = "_"), then [define new variable name using concatenation of a few scan() instances]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for issue 2, i'd use some form of if scan() = "OBS" then [define new variable name]&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 16:34:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612734#M178843</guid>
      <dc:creator>seeff</dc:creator>
      <dc:date>2019-12-18T16:34:50Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612735#M178844</link>
      <description>&lt;P&gt;sorry, I meant to say first that I'd output the variable names to a dataset to work with them separately from the data&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 16:37:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612735#M178844</guid>
      <dc:creator>seeff</dc:creator>
      <dc:date>2019-12-18T16:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612740#M178847</link>
      <description>&lt;P&gt;Use&amp;nbsp;SASHELP.VCOLUMN table to scan your variable names&lt;/P&gt;
&lt;P&gt;Subset the desired variables and create the new names according to the rules&lt;/P&gt;
&lt;P&gt;Submit proc datasets with modify option and do the rename&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
 set SASHELP.VCOLUMN 
       (where=(libname=&amp;lt;library&amp;gt; and memname=&amp;lt;dataset name&amp;gt;));
        N_ =  countw(varname,'_') ;
        pos1 = index(varname,'OBS');
        pos2 = index(varname.'PER');

         if N_ &amp;gt; 3 then do;
             do i= 1 to (N_ - 3);
                 ix = index(varname,'_',-1); /* seek from the end */
                 varname = substr(varname,1,ix-1) || substr(varname,ix+1);
             end;
            newname = varname;
            flagout=1;
        end;

       if pos1 &amp;gt;0 or pos2&amp;gt;0 then do;
          pos = max(pos1 pos2);
          suffix = substr(varname,pos,3);
          newname = trim(substr(varname,1,pos-1) ||
                              substr(varname,pos+3)) || suffix;
          flagout =1;
      end;
      if flagout = 1 then output;
      keep varname newname;
run;

filename program temp 'generated_program.sas';
data _null_;
   set vars (end=eof);
         file program;
         if _N_= 1 then do;
            put 'proc datasets lib=&amp;lt;library&amp;gt;  nolist; ';
            put 'modify &amp;lt;dataset-name&amp;gt;; ';
            put 'rename';
         end;
         put varname ' = ' nename ';' ;
       
         if eof then put '; run;' ;
run;

%include program;

/* in case of issues just repair the program before running */
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2019 16:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612740#M178847</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-12-18T16:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612741#M178848</link>
      <description>&lt;P&gt;Hey thanks! When I fill in the libname in your 3rd line it says "error: Variable yourlibrary is not on file SASHELP.VCOLUMN. How can I fix this?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 17:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612741#M178848</guid>
      <dc:creator>Alexander2</dc:creator>
      <dc:date>2019-12-18T17:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612744#M178849</link>
      <description>&lt;P&gt;The value of LIBRARY is the name of the libref defined that points to where the dataset lives.&amp;nbsp; So it cannot be longer than 8 characters and must be specified as a character string in all uppercase.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;library='MYLIB' &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Similarly the MEMNAME is the name of the dataset in uppercase.&amp;nbsp; So it also must be a string and cannot be longer then 32 bytes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
 set SASHELP.VCOLUMN ;
 where library='MYLIB' and memname='MYDATASET_NAME' ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2019 17:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612744#M178849</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-18T17:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612745#M178850</link>
      <description>&lt;P&gt;Library and member names have to be all uppercase when searching in the dictionary tables (and the respective sashelp views). Library names are also limited to 8 characters, so "yourlibrary" is impossible.&lt;/P&gt;
&lt;P&gt;And you need to place those literals in quotes, or the data step compiler will misunderstand them for variable names within the view.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 17:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/612745#M178850</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-18T17:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Change column names to abide by naming conventions with code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/615474#M180029</link>
      <description>&lt;P&gt;I ended up using the countw() function as my workhorse. Here is my final solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data temp;
set var_list;
if index(lowcase(name), "__c") &amp;gt; 0 then ix="true";
count = countw(name, "_");
run;
data temp2;
set temp;
newstring=tranwrd(name, "__C", '');
run;
data temp4;
set temp2;
count = countw(newstring, "_");
len = length(name);
IF index(newstring, "OBS") then obs="true";
if index(newstring, "PER") then per="true";
if (length(newstring) - index(newstring, "OBS")) &amp;gt; 2 then breakOBS="true";
if (length(newstring) - index(newstring, "PER")) &amp;gt; 2 then breakPER="true";
IF obs = "true" and breakobs="true" then make_list1 = "true";
if per = "true" and breakper="true" then make_list2 = "true";
if count &amp;gt; 4 then make_list3 = "true";
if make_list1 = "true" or make_list2 = "true" or make_list3 = "true" then made_list = "true";
run;
data list(keep=name);
set temp4;
if made_list = "true";
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Jan 2020 21:10:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-column-names-to-abide-by-naming-conventions-with-code/m-p/615474#M180029</guid>
      <dc:creator>Alexander2</dc:creator>
      <dc:date>2020-01-06T21:10:35Z</dc:date>
    </item>
  </channel>
</rss>

