<?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: Converting Character columns to numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/589191#M168479</link>
    <description>&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; I&amp;nbsp;have a bunch of ##Div0 in excel in the first 30-40 rows across multiple columns across multiple spreadsheets so the input() option is quicker this time.&lt;/P&gt;&lt;P&gt;But I have noted the guessingrows=max option for future use with csv files.&lt;/P&gt;</description>
    <pubDate>Mon, 16 Sep 2019 20:30:52 GMT</pubDate>
    <dc:creator>ubshams</dc:creator>
    <dc:date>2019-09-16T20:30:52Z</dc:date>
    <item>
      <title>Converting Character columns to numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/588922#M168369</link>
      <description>&lt;P&gt;My dataset came from Excel and some columns which should be numbers got converted as characters in SAS.&lt;/P&gt;&lt;P&gt;Apart from one column I want to convert all other character columns into number format. How do I do that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lets say variables &amp;nbsp;"x", "y", "z","b", and "a"&amp;nbsp;came in as character variables (Type= Char and format = $7.). Only variable x is okay to be a charcter. How do I keep variable&amp;nbsp; x as is and conert y, z, and b to Type = Num and format =&amp;nbsp;BEST.&amp;nbsp;in a simple way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Was trying to do something like this, but not working:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; new;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; old;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; variable &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;not&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'x'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; format = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'$7.'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; format &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;best.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 01:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/588922#M168369</guid>
      <dc:creator>ubshams</dc:creator>
      <dc:date>2019-09-16T01:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character columns to numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/588927#M168371</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;While converting variables from character to numeric make sure all the available data under character variable should be number.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x $  y $ z $;
datalines;
9 1 2
;
run;
data want;
	set have; 
	array all_n[*] y z ;
	array t(2)  _y _z;
	do i = 1 to dim(all_n); 
		t[i] = input(all_n[i], best.);  
	end;
run; 
proc contents data = want; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Sep 2019 01:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/588927#M168371</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2019-09-16T01:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character columns to numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/588929#M168372</link>
      <description>&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We may do it without using array. Borrowing from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153275"&gt;@singhsahab&lt;/a&gt;&amp;nbsp;, we do it as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have(rename=(_y = y _z=z));
input x $  y $ z $;
_y = input(y, best12.); drop y;
_z = input(z, best12.); drop z;
datalines;
9 1 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 03:21:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/588929#M168372</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-09-16T03:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character columns to numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/589154#M168455</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/185449"&gt;@ubshams&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;My dataset came from Excel and some columns which should be numbers got converted as characters in SAS.&lt;/P&gt;
&lt;P&gt;Apart from one column I want to convert all other character columns into number format. How do I do that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lets say variables &amp;nbsp;"x", "y", "z","b", and "a"&amp;nbsp;came in as character variables (Type= Char and format = $7.). Only variable x is okay to be a charcter. How do I keep variable&amp;nbsp; x as is and conert y, z, and b to Type = Num and format =&amp;nbsp;BEST.&amp;nbsp;in a simple way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Was trying to do something like this, but not working:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; new;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; old;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; variable &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;not&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'x'&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; format = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'$7.'&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; format &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;best.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This sounds like using Proc Import to bring in the data. Have you verified that all of your actual character variables are correct? Import does this when reading spreadsheets because it only uses a default of the first 20 rows to determine variable type and length. If there are more than one row of headers or blank values in the column you will end up with character values. Or if the values sometimes have non-numeric characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Better is to learn to control reading the data sets to begin with so you don't have to spend time fixing this. Especially if you will be reading multiple sheets with the same structure. You might get different columns in the next file or sheet to be of the wrong type or length.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either save the sheets as CSV files and write a proper data step to read them (Hint: Proc import with the guessingrows=max option will generate a basic data step to read the file that you can copy from the log to the editor, clean up and reuse by changing infile and data set).&lt;/P&gt;
&lt;P&gt;Or use Libname Excel and read the data more directly.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 18:10:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/589154#M168455</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-16T18:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character columns to numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/589191#M168479</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; I&amp;nbsp;have a bunch of ##Div0 in excel in the first 30-40 rows across multiple columns across multiple spreadsheets so the input() option is quicker this time.&lt;/P&gt;&lt;P&gt;But I have noted the guessingrows=max option for future use with csv files.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 20:30:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-columns-to-numbers/m-p/589191#M168479</guid>
      <dc:creator>ubshams</dc:creator>
      <dc:date>2019-09-16T20:30:52Z</dc:date>
    </item>
  </channel>
</rss>

