<?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: Renaming variables to be read in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660199#M197664</link>
    <description>&lt;P&gt;Close. Try:&lt;/P&gt;
&lt;PRE&gt;options validvarname=any;
data One;
   set Merlin.data;
   rename 'Dx status'n=Dx_status;
run;&lt;/PRE&gt;
&lt;P&gt;A name that needs the Validvarname option to be usable requires the quotes and n to indicate a "name literal". The quotes are needed to tell SAS where the unfriendly name starts and ends and the n that is a name. The new variable name should not be in quotes.&lt;/P&gt;</description>
    <pubDate>Tue, 16 Jun 2020 21:29:25 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-16T21:29:25Z</dc:date>
    <item>
      <title>Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660195#M197660</link>
      <description>&lt;P&gt;I am running SAS Studio 9.4 and am trying to rename variables that have spaces in them so that they can be read in SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example I have a variable Dx status but since it has a space in it SAS cannot read it. This is part of the original dataset.&lt;/P&gt;&lt;P&gt;Is there a way to rename it to Dx_status?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bernie&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 21:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660195#M197660</guid>
      <dc:creator>BernieK</dc:creator>
      <dc:date>2020-06-16T21:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660196#M197661</link>
      <description>&lt;P&gt;data One;&lt;BR /&gt;set Merlin.data;&lt;BR /&gt;options validvarname=any;&lt;BR /&gt;rename Dx status='Dx_status';&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 21:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660196#M197661</guid>
      <dc:creator>BernieK</dc:creator>
      <dc:date>2020-06-16T21:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660198#M197663</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
options validvarname=any;

data One;
set Merlin.data;

rename 'Dx status'n=Dx_status;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Jun 2020 21:26:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660198#M197663</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-06-16T21:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660199#M197664</link>
      <description>&lt;P&gt;Close. Try:&lt;/P&gt;
&lt;PRE&gt;options validvarname=any;
data One;
   set Merlin.data;
   rename 'Dx status'n=Dx_status;
run;&lt;/PRE&gt;
&lt;P&gt;A name that needs the Validvarname option to be usable requires the quotes and n to indicate a "name literal". The quotes are needed to tell SAS where the unfriendly name starts and ends and the n that is a name. The new variable name should not be in quotes.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 21:29:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660199#M197664</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-16T21:29:25Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660223#M197672</link>
      <description>&lt;P&gt;You can dynamically change the variable names by utilizing SAS Dictionary Tables. The code below pulls all the variables from the table that have spaces and creates a rename statement that can convert them in a downstream data step operation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* Data with spaces in variable names */
data work.in_data;
	'Dx Status'n = "a";
	'Another Status'n = 23;
	good_status = "yes";
run;

/* Create rename statement */
proc sql noprint;
	select cats("'",strip(name),"'n=",tranwrd(strip(name)," ","_")) into :rename_stmt separated by " "
	from dictionary.columns
		where find(strip(name),' ','i')
			and upcase(libname) eq 'WORK'
			and upcase(memname) eq 'IN_DATA'
	;
quit;

/* Apply rename statement */
data work.want;
	set work.in_data (rename=(&amp;amp;rename_stmt));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The&amp;nbsp;&lt;STRONG&gt;&amp;amp;rename_stmt&amp;nbsp;&lt;/STRONG&gt;variable will resolve to:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="agannon_1-1592350291796.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/43963iEDF0F25B26D5B8C6/image-size/large?v=v2&amp;amp;px=999" role="button" title="agannon_1-1592350291796.png" alt="agannon_1-1592350291796.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which, when applied, will generate the desired variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 23:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660223#M197672</guid>
      <dc:creator>agannon</dc:creator>
      <dc:date>2020-06-16T23:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660228#M197673</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option validvarname=v7;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now import your data and SAS will automatically fix your variable names to be valid SAS names without spaces.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 00:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660228#M197673</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-06-17T00:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables to be read in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660232#M197676</link>
      <description>&lt;P&gt;Note that the NLITERAL() function will generate a valid variable name from a string.&amp;nbsp; If the value is already a valid name then it does nothing. Otherwise it adds quotes and the N suffix.&amp;nbsp; It will pick the best quote character to use.&lt;/P&gt;
&lt;P&gt;Note that changing just the spaces to underscore does not guarantee a valid name.&amp;nbsp; Also removing leading spaces by using STRIP() instead of just removing trailing spaces with TRIM() might lead to duplicate names.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;catx('=',nliteral(name),nliteral(tranwrd(trim(name),' ','_')))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jun 2020 00:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables-to-be-read-in-SAS/m-p/660232#M197676</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-06-17T00:56:43Z</dc:date>
    </item>
  </channel>
</rss>

