<?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 a character variable with text/symbol/numeric values to a numeric value in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876249#M38876</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/440884"&gt;@moni2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The original character variable has numeric values, special symbols ":, *), numeric values with symbols "&amp;gt;3, -3, &amp;gt;=10", and text such as "see comment" and "&lt;STRONG&gt; _&lt;/STRONG&gt;___MARKED". Ultimately, I want to convert this to a numeric variable because I will need to do summary statistics on it later. However, I do need to keep the "-3" the other values such as "&amp;gt;3" can become "3".&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think we would need a complete set of rules about what to do with non-numeric characters that are found in the data. The above discusses just one such rule.&lt;/P&gt;</description>
    <pubDate>Wed, 17 May 2023 15:04:48 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-05-17T15:04:48Z</dc:date>
    <item>
      <title>Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876246#M38875</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am trying to convert a variable from a character to a numeric variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The original character variable has numeric values, special symbols ":, *), numeric values with symbols "&amp;gt;3, -3, &amp;gt;=10", and text such as "see comment" and "&lt;STRONG&gt; _&lt;/STRONG&gt;___MARKED". Ultimately, I want to convert this to a numeric variable because I will need to do summary statistics on it later. However, I do need to keep the "-3" the other values such as "&amp;gt;3" can become "3".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote the following program:&lt;/P&gt;&lt;P&gt;DATA L1;&lt;BR /&gt;LENGTH RV $ 100;&amp;nbsp;&lt;BR /&gt;SET Labs;&lt;BR /&gt;RESULT_VALUE = COMPRESS(RESULT_VALUE,'&amp;lt;&amp;gt; &amp;gt;=+');&amp;nbsp;&lt;BR /&gt;IF RESULT_VALUE IN('' ':') THEN RV = '';&amp;nbsp;&lt;BR /&gt;ELSE RV=RESULT_VALUE;&lt;BR /&gt;IF anyalpha(RV) THEN DELETE;&lt;BR /&gt;ELSE RV = INPUT(RV, 12.);&amp;nbsp;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;I was able to successfully remove the text values and symbols, but, "-3" got converted to "3" and the variable is still character. Additionally, on doing a subsequent&amp;nbsp; PROC FREQ it the original variable "result_value" is missing its original text and special values which I thought should not happen.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help me understand how to do this differently?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 14:52:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876246#M38875</guid>
      <dc:creator>moni2</dc:creator>
      <dc:date>2023-05-17T14:52:48Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876249#M38876</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/440884"&gt;@moni2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The original character variable has numeric values, special symbols ":, *), numeric values with symbols "&amp;gt;3, -3, &amp;gt;=10", and text such as "see comment" and "&lt;STRONG&gt; _&lt;/STRONG&gt;___MARKED". Ultimately, I want to convert this to a numeric variable because I will need to do summary statistics on it later. However, I do need to keep the "-3" the other values such as "&amp;gt;3" can become "3".&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think we would need a complete set of rules about what to do with non-numeric characters that are found in the data. The above discusses just one such rule.&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 15:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876249#M38876</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-17T15:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876251#M38877</link>
      <description>&lt;P&gt;Add some sample data in a working data step along with desired results&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 15:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876251#M38877</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2023-05-17T15:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876278#M38882</link>
      <description>&lt;P&gt;First, this makes no sense:&lt;/P&gt;
&lt;PRE&gt;ELSE RV = INPUT(RV, 12.); &lt;/PRE&gt;
&lt;P&gt;because you have defined RV to be a 100 length character variable. So you are going to input something, that may actually be numeric in appearance and then use internal default conversions to make it fit into a character value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your -3 disappearing is likely because of the COMPRESS you have chosen. You are removing the SPACE character. So if there is anything before the -3 (or similar) then&amp;nbsp; "a&amp;nbsp;&amp;nbsp;&amp;nbsp; -3" after compress becomes "a-3" which will not read as numeric with a 12. informat.&lt;/P&gt;
&lt;P&gt;So look at the full value of variable with the disappearing -3. Subtle issues might be the presence of TAB or NULL characters that appear to be spaces but are not actually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might consider, lacking anything resembling many examples&lt;/P&gt;
&lt;PRE&gt;RESULT_VALUE = COMPRESS(RESULT_VALUE,'0123456789-.','k'); &lt;/PRE&gt;
&lt;P&gt;to KEEP digits, the - and . (assumes some decimal values) and discard everything else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 16:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876278#M38882</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-17T16:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876467#M38915</link>
      <description>&lt;P&gt;thank you to everyone who offered suggestions;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adding the " RESULT_VALUE = COMPRESS(RESULT_VALUE,'0123456789-.','k'); " was helpful but then it created a value which was not there before "7.0000000000000007-2" in either then old or the new variable and the old and the text values from the old variable are missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Here is a sample of the values in the variable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&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;27.9&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;Again, I don't need the text values in my new variable, "-3" should be preserved, and "=&amp;gt;60" can be just 60. And ultimately, I want to create a new numeric variable free from text.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;thank you everyone for your patience.&lt;/DIV&gt;</description>
      <pubDate>Thu, 18 May 2023 14:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876467#M38915</guid>
      <dc:creator>moni2</dc:creator>
      <dc:date>2023-05-18T14:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876731#M38941</link>
      <description>&lt;P&gt;What is the numeric value for "Moderate"?&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 22:24:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876731#M38941</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2023-05-19T22:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Converting a character variable with text/symbol/numeric values to a numeric value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876735#M38942</link>
      <description>&lt;P&gt;I would use TRANSLATE() instead of COMPRESS() to get rid of the characters you don't want.&amp;nbsp; That will reduce the risk of forming a number from separate digit strings in the string.&amp;nbsp; But probably remove the commas so strings like 1,234,567 will be recognized.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input string $32. ;
cards;
2.8
25
Slight
&amp;gt;=60
0.3
4.56E2
Moderate
37
Marked
7.0000000000000007-2
1,230
+45
-3
100-200
34&amp;gt;56
;

data want;
  set have;
  clean=left(translate(compress(string,','),' ',compress(string,'+-. E','d')));
  number=input(clean,??32.);
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    string                  clean                    number

  1    2.8                     2.8                         2.8
  2    25                      25                         25.0
  3    Slight                                               .
  4    &amp;gt;=60                    60                         60.0
  5    0.3                     0.3                         0.3
  6    4.56E2                  4.56E2                    456.0
  7    Moderate                                             .
  8    37                      37                         37.0
  9    Marked                                               .
 10    7.0000000000000007-2    7.0000000000000007-2         .
 11    1,230                   1230                     1230.0
 12    +45                     +45                        45.0
 13    -3                      -3                         -3.0
 14    100-200                 100-200                      .
 15    34&amp;gt;56                   34 56                        .
&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>Sat, 20 May 2023 01:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-a-character-variable-with-text-symbol-numeric-values/m-p/876735#M38942</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-20T01:14:36Z</dc:date>
    </item>
  </channel>
</rss>

