<?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: Making a variable in to numeric but it contains some characters also in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606821#M176282</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to convert a variable in to numeric , but I am getting a warning "Invalid argument to function Input"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have used &lt;STRONG&gt;var1=input(var,best.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variable contains data like below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;3&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;4&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;non-reactive&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;5&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;non-reactive&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;6&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;7&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;etc....&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the warning is coming for the non-reactive. how to handle this to make this in to numeric&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What number to you want to convert 'non-reactive' into?&lt;/P&gt;
&lt;P&gt;If you want any value that is not a number to just become missing without SAS complaining about it you can add the ?? prefix to the informat.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var1=input(var,??32.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that trying to use BEST as the name of an informat will just revert to using the normal numeric informat.&amp;nbsp; There is nothing better about using best, the only thing it adds is confusion. The maximum length string that the normal numeric informat can handle is 32 bytes.&lt;/P&gt;</description>
    <pubDate>Sun, 24 Nov 2019 15:49:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-11-24T15:49:58Z</dc:date>
    <item>
      <title>Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606774#M176258</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to convert a variable in to numeric , but I am getting a warning "Invalid argument to function Input"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used &lt;STRONG&gt;var1=input(var,best.);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variable contains data like below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;3&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;4&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;non-reactive&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;5&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;non-reactive&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;6&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;7&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;etc....&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the warning is coming for the non-reactive. how to handle this to make this in to numeric&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2019 05:59:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606774#M176258</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2019-11-24T05:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606777#M176261</link>
      <description>&lt;P&gt;A SAS numeric variable can only contain numbers or a missing value. Either switch to using a character variable that can handle both numbers and characters (like you have already) or record "non-responsive" in a separate character variable.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2019 06:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606777#M176261</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-11-24T06:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606783#M176262</link>
      <description>&lt;P&gt;You could always try compressing only the digits into a numeric variable as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length var $15.;
	input var $;
	cards;
3
4
non-reactive
5
non-reactive
6
7	
;
run;

data want;
	set have;
	
	char_digits = compress(var, '', 'kd');
	num_digits = input(char_digits, best.);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Be warned that values like "non-reactive1" will be translated to 1. Inspect your data to make sure you get the right results. You could work around that by using the ANYALPHA function and ignoring values that have characters in them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	
	any_digit = anydigit(var);
	any_char = anyalpha(var);
	
	if not (any_char) then do;
		char_digits = compress(var, '', 'kd');
		num_digits = input(char_digits, best.);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Nov 2019 07:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606783#M176262</guid>
      <dc:creator>jvdl</dc:creator>
      <dc:date>2019-11-24T07:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606798#M176269</link>
      <description>&lt;P&gt;A numeric variable can only store digits. Below how you could replace alphanumeric values with digits.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length var $15.;
	input var $;
	cards;
3
4
non-reactive
5
non-reactive
6
7	
;

proc format;
  invalue recode(upcase)
    'NON-REACTIVE' = 0
    other = _same_
    ;
quit;

data want;
  set have;
  var_recoded=input(var,recode.);
run;

proc print;
run;
&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2019 10:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606798#M176269</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-11-24T10:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606802#M176271</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there's only a small, fixed set of possible non-numeric values, you could use formatted special missing values to store everything in a numeric variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Prepare informat and format for possible non-numeric values */

data infmt;
retain fmtname 'labi' type 'I' default 20;
input start :$12. label :$2.;
cards;
non-reactive .N
reactive     .R
unknown      .U
;
run;

data fmt;
set infmt(rename=(label=start start=label) drop=type);
fmtname='labf';
run;

/* Create informat LABI and format LABF as defined above */

proc format cntlin=infmt;
proc format cntlin=fmt;
run;

/* Apply informat and format to values */

data have;
input value labi.;
format value labf14.;
cards;
3
4
non-reactive
5
non-reactive
6
7
unknown
reactive
;

proc print data=have noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;value&lt;/FONT&gt;&lt;/STRONG&gt;

             3
             4
non-reactive
             5
non-reactive
             6
             7
unknown
reactive&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Nov 2019 11:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606802#M176271</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-24T11:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606805#M176272</link>
      <description>&lt;P&gt;What do you want ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;var1=input(var, ?? best.);&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2019 11:16:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606805#M176272</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-24T11:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable in to numeric but it contains some characters also</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606821#M176282</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to convert a variable in to numeric , but I am getting a warning "Invalid argument to function Input"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have used &lt;STRONG&gt;var1=input(var,best.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variable contains data like below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;3&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;4&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;non-reactive&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;5&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;non-reactive&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;6&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;7&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;etc....&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the warning is coming for the non-reactive. how to handle this to make this in to numeric&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What number to you want to convert 'non-reactive' into?&lt;/P&gt;
&lt;P&gt;If you want any value that is not a number to just become missing without SAS complaining about it you can add the ?? prefix to the informat.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var1=input(var,??32.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that trying to use BEST as the name of an informat will just revert to using the normal numeric informat.&amp;nbsp; There is nothing better about using best, the only thing it adds is confusion. The maximum length string that the normal numeric informat can handle is 32 bytes.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Nov 2019 15:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-a-variable-in-to-numeric-but-it-contains-some-characters/m-p/606821#M176282</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-24T15:49:58Z</dc:date>
    </item>
  </channel>
</rss>

