<?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: Convert Character to Numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536556#M147452</link>
    <description>&lt;P&gt;Hi &lt;SPAN class=""&gt;&lt;A id="link_8" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214996" target="_self"&gt;monona&lt;/A&gt;, my suggestion would be to k&lt;/SPAN&gt;eep it simple, just read the character values in, check if it's "NaN" then convert them if they are not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input ;
	input charValue $ ;
cards;
Nan
0
1
2
Nan
4
5
;
run ;

data output ;
	set input ;
	if charValue="Nan" then
	do ;
		numValue=0 ;
	end ;
	else do ;
		numValue=inputn(charValue,"8.") ;
	end ;
	put charValue= numValue= ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 18 Feb 2019 19:36:15 GMT</pubDate>
    <dc:creator>AMSAS</dc:creator>
    <dc:date>2019-02-18T19:36:15Z</dc:date>
    <item>
      <title>Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536552#M147449</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.cholesterol;
	%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
	infile 'path' delimiter='09'x MISSOVER DSD lrecl=32767 firstobs=2 ;
	format variant $70. ;
	format minor_allele $2. ;
	format minor_AF best12. ;
	format expected_case_minor_AC best12. ;
	format low_confidence_variant $5. ;
	format n_complete_samples best12. ;
	format AC best12. ;
	format ytx best12. ;
	format beta $12. ;
	format se $11. ;
	format tstat $12. ;
	format pval $11. ;

	input
		variant  $
		minor_allele  $
		minor_AF
		expected_case_minor_AC
		low_confidence_variant  $
		n_complete_samples
		AC
		ytx
		beta  $
		se  $
		tstat  $
		pval ;
	if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable*/ 
run;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I imported data from codes above.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27269i64944D59728A4482/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I changed 'NaN' to 0 so that I can convert these columns to numeric.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cholesterol;
	set cholesterol;
	array Var _character_;
		do over Var;
			if Var='NaN' then Var=0;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 785px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27268i30E3E58F0C8F8AF3/image-dimensions/785x144?v=v2" width="785" height="144" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I tried to convert these variables to numeric, but doesn't work.....&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cholesterol;
	set cholesterol;
	beta  = input(se, best32.);
	se    = input(se, best32.);
	tstat = input(tstat, best32.);
	pval  = input(pval, best32.);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27271iF1440B9E90E5B9BD/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate if you could give any solution for this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 19:49:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536552#M147449</guid>
      <dc:creator>monona</dc:creator>
      <dc:date>2019-02-18T19:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536554#M147450</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214996"&gt;@monona&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess your log says something about variable has alreadu be defined as numeric. You must assign the numeric values to&amp;nbsp; (new) numeric variables. If you want to preserve the original names, you could use rename:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data new (drop ctstat), set old (rename=(tstat=ctstat);&lt;/P&gt;
&lt;P&gt;tstat = input(ctstat,best32.);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 19:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536554#M147450</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-18T19:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536555#M147451</link>
      <description>- of course double pranthesis as the end: (rename=(tstat=ctstat xx=cxx));</description>
      <pubDate>Mon, 18 Feb 2019 19:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536555#M147451</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-18T19:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536556#M147452</link>
      <description>&lt;P&gt;Hi &lt;SPAN class=""&gt;&lt;A id="link_8" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214996" target="_self"&gt;monona&lt;/A&gt;, my suggestion would be to k&lt;/SPAN&gt;eep it simple, just read the character values in, check if it's "NaN" then convert them if they are not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input ;
	input charValue $ ;
cards;
Nan
0
1
2
Nan
4
5
;
run ;

data output ;
	set input ;
	if charValue="Nan" then
	do ;
		numValue=0 ;
	end ;
	else do ;
		numValue=inputn(charValue,"8.") ;
	end ;
	put charValue= numValue= ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Feb 2019 19:36:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536556#M147452</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2019-02-18T19:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536562#M147456</link>
      <description>&lt;P&gt;I intend to replace NaN to 0 over&lt;STRONG&gt; all variables&amp;nbsp;&lt;/STRONG&gt;not single variable. How can I do that?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 19:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536562#M147456</guid>
      <dc:creator>monona</dc:creator>
      <dc:date>2019-02-18T19:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536570#M147462</link>
      <description>&lt;P&gt;Why are you reading them in as character to begin with?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like you might have sub-contracted coding your data step to PROC IMPORT?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why does the file have character strings in numeric variables?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you generate that file from R perhaps?&amp;nbsp; Can you teach R to not do that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is easier to convert the NaN (and any other non numeric strings) to missing instead of zero.&amp;nbsp; Also probably more accurate.&lt;/P&gt;
&lt;P&gt;You can use the ?? informat modifier to suppress the error messages.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cholesterol;
  infile 'path' delimiter='09'x TRUNCOVER DSD lrecl=32767 firstobs=2 ;
  length variant $70 minor_allele $2 minor_AF 8
         expected_case_minor_AC 8 low_confidence_variant $5 
         n_complete_samples 8 AC 8 ytx 8
         beta 8 se 8 tstat 8 pval 8
  ;
  input variant--ytx ( beta se tstat pval ) (:??32.) ;
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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 20:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536570#M147462</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-18T20:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536575#M147465</link>
      <description>&lt;P&gt;Oh this works! Lastly, can you explain brief logic behind the last statement?&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; variant&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;ytx &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;beta&lt;/SPAN&gt; se tstat pval &lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;:??&lt;SPAN class="token number"&gt;32&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 punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Appreicate!!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 20:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536575#M147465</guid>
      <dc:creator>monona</dc:creator>
      <dc:date>2019-02-18T20:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Convert Character to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536585#M147467</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214996"&gt;@monona&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Oh this works! Lastly, can you explain brief logic behind the last statement?&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; variant&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;ytx &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;beta&lt;/SPAN&gt; se tstat pval &lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;:??&lt;SPAN class="token number"&gt;32&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 punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Appreicate!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It is just an INPUT statement. There are two variable lists. The first one by position.&amp;nbsp; The second one just space delimited, but it could have been shortened to beta--pval.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The (...) (...) syntax is for listing a set of informats to apply the preceding set of variables. If the informat list is too short they are recycled.&amp;nbsp; Using the (...)(...) means that I only have to type the informat specification once instead of repeating it for each variable in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The double ? says don't generate any warnings or errors if the text is not compatible with the informat.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The : says to continue to use list mode (only read as many characters as there are before the next delimiter) instead of formatted mode even though there is an explicit informat in the INPUT statement.&amp;nbsp; &amp;nbsp;The 32. is the informat to use. It means to read the value using the normal numeric informat.&amp;nbsp; The width of 32 is the max allowed, but not really needed since in list mode SAS will ignore the width that you set on your informat and instead just read the full set of characters found.&amp;nbsp; You probably do NOT need the :32. at all since SAS already knows how to read numbers.&amp;nbsp; But if your text values have dollar signs and/or commas then you would want to use the COMMA informat instead.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 21:25:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-Character-to-Numeric/m-p/536585#M147467</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-18T21:25:50Z</dc:date>
    </item>
  </channel>
</rss>

