<?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: Find special characters in a string, take them off and do a calculation, and put them back in in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/752244#M236932</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/387529"&gt;@anuajax&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally, it is best practice to create a new question with what you are now asking and you can supply a link back to this question for context.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each example you have given what result do you want to see and what did you get when you tried the existing solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, when supplying data, it is a lot easier for everyone to help you if you supply data using a data step with &lt;FONT face="courier new,courier"&gt;datalines&lt;/FONT&gt;, as can be seen in the solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
    <pubDate>Tue, 06 Jul 2021 09:51:59 GMT</pubDate>
    <dc:creator>Amir</dc:creator>
    <dc:date>2021-07-06T09:51:59Z</dc:date>
    <item>
      <title>Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619839#M182057</link>
      <description>&lt;P&gt;I&amp;nbsp; want to find special characters in a string, remove them from the string, do a calculation, and put that characters back in. Is there an elegant way to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if I have a variable with values:&amp;nbsp;&lt;/P&gt;&lt;P&gt;33.3%&lt;/P&gt;&lt;P&gt;16%&lt;/P&gt;&lt;P&gt;&amp;lt;10%&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to multiply 1.2 after removing either or both % and &amp;lt;&lt;/P&gt;&lt;P&gt;39.96&lt;/P&gt;&lt;P&gt;19.2&lt;/P&gt;&lt;P&gt;12&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then I need to put the special characters back&amp;nbsp;&lt;/P&gt;&lt;P&gt;39.96%&lt;/P&gt;&lt;P&gt;19.2%&lt;/P&gt;&lt;P&gt;&amp;lt;12%&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe we can use index and prxmatch functions..? index(lborres,'&amp;lt;') or index(lborres,'%') ;&amp;nbsp; if prxmatch('m/&amp;lt;|%', lborres) &amp;gt; 0 then do; I'm not sure how I should go about this (or other way)..&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 15:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619839#M182057</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2020-01-24T15:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619849#M182063</link>
      <description>&lt;P&gt;You won't be able to do this if just remove special characters but you would need to store things like the &amp;lt; somewhere. If all of the values are percentages then putting them back is relatively simple but you may need to address concerns about how many digits to maintain in your re-calculated values. So please specify rules for significant digits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that because you will be adding digits to a character value that you should also tell us what the defined length of the variable is. If the current defined length of a variable is 5 characters then you won't be able to fit 39.96% into the variable as that requires 6 character positions.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 16:05:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619849#M182063</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-01-24T16:05:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619851#M182065</link>
      <description>&lt;P&gt;Are you looking for something like the following? The answer should be in the variable called "result".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input text $char10.;
   datalines;
33.3%
16%
&amp;lt;10%
;


data want;
   set have;
   length result $ 10;
   retain factor 1.2;

   number  = compress(text,'.','kd');
   product = number * factor;
   result  = tranwrd(text,cats(number),cats(product));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 16:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619851#M182065</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2020-01-24T16:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619854#M182067</link>
      <description>&lt;P&gt;I would recommend a little different approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can eliminate from the string any character that is not a number and not a decimal point and not a negative sign via the COMPRESS() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have done that, you can use INPUT to turn these character strings into actual numbers, so you can do the multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I would leave them as numbers, and use some format to make the number appear with a % sign or with a &amp;lt; indicator.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 16:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619854#M182067</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-24T16:10:32Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619893#M182089</link>
      <description>&lt;P&gt;I meant to convert variable &lt;FONT face="courier new,courier"&gt;number&lt;/FONT&gt; to a number, which is why I used the &lt;FONT face="courier new,courier"&gt;cats()&lt;/FONT&gt;&amp;nbsp;function on it later which formats numerics with the &lt;FONT face="courier new,courier"&gt;best.&lt;/FONT&gt; format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a format did cross my mind, but some data had one symbol and others had two and it wasn't immediately clear to me what the rule was from what the OP had posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 17:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619893#M182089</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2020-01-24T17:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619915#M182095</link>
      <description>&lt;P&gt;I can set up rounding and character lengths in another part of the program; should they affect the code/solution? This solution worked. Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 19:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619915#M182095</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2020-01-24T19:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619919#M182097</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22588"&gt;@Amir&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I meant to convert variable &lt;FONT face="courier new,courier"&gt;number&lt;/FONT&gt; to a number, which is why I used the &lt;FONT face="courier new,courier"&gt;cats()&lt;/FONT&gt;&amp;nbsp;function on it later which formats numerics with the &lt;FONT face="courier new,courier"&gt;best.&lt;/FONT&gt; format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a format did cross my mind, but some data had one symbol and others had two and it wasn't immediately clear to me what the rule was from what the OP had posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I like your solution&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22588"&gt;@Amir&lt;/a&gt;&amp;nbsp;, it is very compact and efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With regard to my suggestion to use formats: my general feeling is that numbers ought to be numbers, and if you want a % sign on the end or a &amp;lt; in front, I would certainly use formats for that, although your solution makes it easy to turn it back into character variables with the special symbols. Obviously, if there were a huge set of possible special symbols, like for example if the original text string was '&lt;FONT face="courier new,courier"&gt;~2.2}'&lt;/FONT&gt;, you can't accomodate all these special symbols using formats.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2020 20:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/619919#M182097</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-24T20:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/620127#M182178</link>
      <description>&lt;P&gt;Thanks for marking a post as a solution (not everyone does).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For best practice, I would recommend assigning variable "number" a numeric value before using it in a calculation. That also means you don't have to assign variable "number" a character length (which I didn't do, because I meant to make it a numeric variable).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Further, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;also pointed out, if you have negative values then "-" can be added to the second argument of the &lt;FONT face="courier new,courier"&gt;compress()&lt;/FONT&gt; function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example implementation of both points is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   number  = input(compress(text,'.-','kd'),8.);
&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;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 09:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/620127#M182178</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2020-01-27T09:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/752226#M236927</link>
      <description>&lt;P&gt;What if special characters are present in between?&lt;/P&gt;&lt;P&gt;want to mask name with Officer12345 keeping special characters&lt;/P&gt;&lt;P&gt;like (Mr). Briane 'O' Conor to (Of).ficer1'2'345&lt;/P&gt;&lt;P&gt;one more example Han &amp;amp; Van to Off &amp;amp; icer12345&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 08:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/752226#M236927</guid>
      <dc:creator>anuajax</dc:creator>
      <dc:date>2021-07-06T08:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Find special characters in a string, take them off and do a calculation, and put them back in</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/752244#M236932</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/387529"&gt;@anuajax&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally, it is best practice to create a new question with what you are now asking and you can supply a link back to this question for context.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each example you have given what result do you want to see and what did you get when you tried the existing solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, when supplying data, it is a lot easier for everyone to help you if you supply data using a data step with &lt;FONT face="courier new,courier"&gt;datalines&lt;/FONT&gt;, as can be seen in the solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 09:51:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-special-characters-in-a-string-take-them-off-and-do-a/m-p/752244#M236932</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-07-06T09:51:59Z</dc:date>
    </item>
  </channel>
</rss>

