<?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: extract number from string and calculate the mean if the value is in a range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823104#M325018</link>
    <description>&lt;P&gt;Use the COMPRESS function to get rid of anything but digits and hyphens, then count the "words", then calculate or just input:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x dsd truncover;
input ID $ hours :$30.;
datalines;
1	7-8 hours after play
2	0-1
3	10-12
4	11
;

data want;
set have;
string = compress(hours,"-","kd");
if countw(string,"-") = 2
then hours_m = (input(scan(string,2,"-"),best.) + input(scan(string,1,"-"),best.)) / 2;
else hours_m = input(string,best.);
drop string;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Jul 2022 14:01:47 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-07-13T14:01:47Z</dc:date>
    <item>
      <title>extract number from string and calculate the mean if the value is in a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823099#M325016</link>
      <description>&lt;P&gt;Dear Community,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to learn how to calculate the mean from string variables some of them contain comments and range.&lt;/P&gt;
&lt;P&gt;For example, the data I have is as&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="149"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;TD width="85"&gt;hours&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;7-8 hours after play&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;0-1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;10-12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;And the data wanted is&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="213"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="59.75px" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="78.9844px" height="30px"&gt;hours&lt;/TD&gt;
&lt;TD width="73.2656px" height="30px"&gt;hours_m&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="59.75px" height="57px"&gt;1&lt;/TD&gt;
&lt;TD width="78.9844px" height="57px"&gt;7-8 hours after play&lt;/TD&gt;
&lt;TD width="73.2656px" height="57px"&gt;7.5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="59.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="78.9844px" height="30px"&gt;0-1&lt;/TD&gt;
&lt;TD width="73.2656px" height="30px"&gt;0.5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="59.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="78.9844px" height="30px"&gt;10-12&lt;/TD&gt;
&lt;TD width="73.2656px" height="30px"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="59.75px" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="78.9844px" height="30px"&gt;11&lt;/TD&gt;
&lt;TD width="73.2656px" height="30px"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 13:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823099#M325016</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2022-07-13T13:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: extract number from string and calculate the mean if the value is in a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823104#M325018</link>
      <description>&lt;P&gt;Use the COMPRESS function to get rid of anything but digits and hyphens, then count the "words", then calculate or just input:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x dsd truncover;
input ID $ hours :$30.;
datalines;
1	7-8 hours after play
2	0-1
3	10-12
4	11
;

data want;
set have;
string = compress(hours,"-","kd");
if countw(string,"-") = 2
then hours_m = (input(scan(string,2,"-"),best.) + input(scan(string,1,"-"),best.)) / 2;
else hours_m = input(string,best.);
drop string;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2022 14:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823104#M325018</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-13T14:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: extract number from string and calculate the mean if the value is in a range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823106#M325020</link>
      <description>Thank you so much. WOW!</description>
      <pubDate>Wed, 13 Jul 2022 14:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-number-from-string-and-calculate-the-mean-if-the-value/m-p/823106#M325020</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2022-07-13T14:05:12Z</dc:date>
    </item>
  </channel>
</rss>

