<?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 Replacing &amp;quot; &amp;quot; with a &amp;quot;_&amp;quot; for variable names in a given dataset in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504260#M15647</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dear Community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm refering to this topic:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/td-p/174192" target="_blank"&gt;https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/td-p/174192&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This topic is closed (locked), so I have to start a new thread for my question:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution presented in this topic only removes the spaces in the variable names, it doesn't replace them with an underscore.&lt;/P&gt;
&lt;P&gt;What should be adapted to the code in the solution so that a variablename "variable 1" will be replaced by "variable_1"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename_vars(table, remove_char);

  %let table=%upcase(&amp;amp;table);

  %let rename_list=;

  proc sql noprint;

    select cats("'",name,"'n =",compress(name, "&amp;amp;remove_char")) into :rename_list

      separated by " "

    from sashelp.vcolumn

      where upper(libname)="%scan(WORK.&amp;amp;table,-2)"

        and upper(memname)="%scan(&amp;amp;table,-1)"

        and findc(strip(name), "&amp;amp;remove_char")&amp;gt;0;

  quit;

  %put rename_list: %bquote(&amp;amp;rename_list);

  %if %bquote(&amp;amp;rename_list) ne %then

    %do;

      proc datasets lib=%scan(WORK.&amp;amp;table,-2);

        modify %scan(&amp;amp;table,-1);

          rename &amp;amp;rename_list;

        run;

      quit;

    %end;

%mend;

/*  call the macro. First parameter is the table name,

    second parameter the characters to be removed in variable names */

%rename_vars(sample,_ -);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Oct 2018 12:10:49 GMT</pubDate>
    <dc:creator>fre</dc:creator>
    <dc:date>2018-10-15T12:10:49Z</dc:date>
    <item>
      <title>Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504260#M15647</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dear Community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm refering to this topic:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/td-p/174192" target="_blank"&gt;https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/td-p/174192&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This topic is closed (locked), so I have to start a new thread for my question:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution presented in this topic only removes the spaces in the variable names, it doesn't replace them with an underscore.&lt;/P&gt;
&lt;P&gt;What should be adapted to the code in the solution so that a variablename "variable 1" will be replaced by "variable_1"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename_vars(table, remove_char);

  %let table=%upcase(&amp;amp;table);

  %let rename_list=;

  proc sql noprint;

    select cats("'",name,"'n =",compress(name, "&amp;amp;remove_char")) into :rename_list

      separated by " "

    from sashelp.vcolumn

      where upper(libname)="%scan(WORK.&amp;amp;table,-2)"

        and upper(memname)="%scan(&amp;amp;table,-1)"

        and findc(strip(name), "&amp;amp;remove_char")&amp;gt;0;

  quit;

  %put rename_list: %bquote(&amp;amp;rename_list);

  %if %bquote(&amp;amp;rename_list) ne %then

    %do;

      proc datasets lib=%scan(WORK.&amp;amp;table,-2);

        modify %scan(&amp;amp;table,-1);

          rename &amp;amp;rename_list;

        run;

      quit;

    %end;

%mend;

/*  call the macro. First parameter is the table name,

    second parameter the characters to be removed in variable names */

%rename_vars(sample,_ -);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 12:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504260#M15647</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2018-10-15T12:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504264#M15648</link>
      <description>&lt;P&gt;Use the translate() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select cats("'",name,"'n =",translate(name,"&amp;amp;replace_char","&amp;amp;remove_char")) into :rename_list&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and add the parameter to the macro definition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename_vars(table,remove_char,replace_char);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Oct 2018 12:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504264#M15648</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-15T12:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504271#M15650</link>
      <description>&lt;P&gt;Note that a slight modification is probably in order.&amp;nbsp; Variable names often contain trailing blanks, and those should probably be removed rather than replaced:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;cats&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"'"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;name&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"'n ="&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;translate&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;FONT color="#FF0000"&gt;strip(&lt;/FONT&gt;&lt;/SPAN&gt;name&lt;FONT color="#FF0000"&gt;)&lt;/FONT&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"&amp;amp;replace_char"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"&amp;amp;remove_char"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;into&lt;/SPAN&gt; :rename_list&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 12:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504271#M15650</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-15T12:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504282#M15652</link>
      <description>&lt;P&gt;Hi, thank you for your replies&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tested both of your answers, but the space keeps existing between the variable names.&lt;/P&gt;
&lt;P&gt;This is what I used to call the macro:&lt;/P&gt;
&lt;P&gt;%rename_vars(my_dataset, ,_);&lt;/P&gt;
&lt;P&gt;I've also tried to put the space between ' ' and " ", but that didn't help either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could it be that something is still missing in the code?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 13:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504282#M15652</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2018-10-15T13:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504287#M15653</link>
      <description>&lt;P&gt;A blank on its own is nothing for the macro preprocessor. Use the %str macro function to force creation of a blank:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%rename_vars(my_dataset,%str( ),_);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Oct 2018 13:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504287#M15653</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-15T13:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504310#M15655</link>
      <description>&lt;P&gt;What does the first statement now look like (the %macro statement)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does the %PUT statement display?&amp;nbsp; (Just a portion of the result is fine.)&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504310#M15655</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-15T14:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504318#M15656</link>
      <description>&lt;P&gt;Thank you Kurt, the %str( ) did the job.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504318#M15656</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2018-10-15T14:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing " " with a "_" for variable names in a given dataset</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504329#M15657</link>
      <description>&lt;P&gt;Use the strip() or trim() function to get rid of leading and trailing blanks, or trailing blanks only. See &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s previous post in correction of mine.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Replacing-quot-quot-with-a-quot-quot-for-variable-names-in-a/m-p/504329#M15657</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-15T14:26:35Z</dc:date>
    </item>
  </channel>
</rss>

