<?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 Rename any variables in dataset with spaces to include underscore in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906416#M44027</link>
    <description>&lt;P&gt;Morning Folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm just wondering is this is possible. I have multiple historic datasets which have naming conventions such as follows&amp;nbsp;'Underlying Code 'Age Group'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to write some code to pull all the variable names out of my dataset and any which have a space in them to replace them with an underscore. So 'Underlying Code' would be renamed to 'Underlying_Code' etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be welcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sean&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Dec 2023 11:36:09 GMT</pubDate>
    <dc:creator>Sean_OConnor</dc:creator>
    <dc:date>2023-12-06T11:36:09Z</dc:date>
    <item>
      <title>Rename any variables in dataset with spaces to include underscore</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906416#M44027</link>
      <description>&lt;P&gt;Morning Folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm just wondering is this is possible. I have multiple historic datasets which have naming conventions such as follows&amp;nbsp;'Underlying Code 'Age Group'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to write some code to pull all the variable names out of my dataset and any which have a space in them to replace them with an underscore. So 'Underlying Code' would be renamed to 'Underlying_Code' etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be welcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sean&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 11:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906416#M44027</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2023-12-06T11:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Rename any variables in dataset with spaces to include underscore</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906421#M44028</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=any;
data fake;
    'Underlying Code'n=12;
    'Age Group'n=4;
    noblank=0;
run;

proc sql noprint;
    select cats('"',name,'"n=',translate(trim(name),'_',' ')) into :renames separated by ' ' 
    from dictionary.columns where libname='WORK' and memname='FAKE' and find(trim(name),' ')&amp;gt;0;
quit;
%put &amp;amp;=renames;

proc datasets library=work nolist;
modify fake;
rename &amp;amp;renames;
run; quit;
options validvarname=v7;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Dec 2023 12:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906421#M44028</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-06T12:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Rename any variables in dataset with spaces to include underscore</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906424#M44029</link>
      <description>&lt;P&gt;Here a variant for the same. Main difference besides the macro is the use of function nvalid() to test for any non-compliant character and prxchange() to then translate any non-compliant character to an underscore&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro renameVars(ds);
  %local lib tbl rename_list;

  %let lib=%upcase(%scan(work.&amp;amp;ds,-2,.));
  %let tbl=%upcase(%scan(work.&amp;amp;ds,-1,.));

  %let rename_list=;
  proc sql noprint;
    select cats("'",name,"'n =",prxchange('s/[^\w]/_/oi',-1,strip(name)))
      into :rename_list separated by ' '
  /*  select **/
    from dictionary.columns
    where 
      libname="&amp;amp;lib" 
      and memname="&amp;amp;tbl"
      and memtype='DATA' 
      and nvalid(name,'v7')=0
    ;
  quit;

  %if %nrbquote(&amp;amp;rename_list) ne %nrbquote() %then
    %do;
      proc datasets lib=&amp;amp;lib nolist nowarn;
        modify &amp;amp;tbl;
        rename &amp;amp;rename_list;
        run;
      quit;
    %end;
%mend;

data work.have;
  set sashelp.class;
  'var 2 x'n    =1;
  'var_other$5'n=20;
run;
%renameVars(have);

proc contents data=work.have;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Dec 2023 12:29:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-any-variables-in-dataset-with-spaces-to-include/m-p/906424#M44029</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-06T12:29:33Z</dc:date>
    </item>
  </channel>
</rss>

