<?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: how to sum numbers from selected phrases? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817484#M81864</link>
    <description>&lt;P&gt;Do you have a SAS data set?&lt;/P&gt;
&lt;P&gt;If so show us the results of Proc Contents on the data set.&lt;/P&gt;
&lt;P&gt;If you do not have a SAS data set the first thing will be to create one and should show use some of the source file so we can help create one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Almost certainly you have to do something to separate the text into meaningful variables possibly such as Model, Kwota and Cena (what ever those are). Then the sum would be easy.&lt;/P&gt;</description>
    <pubDate>Fri, 10 Jun 2022 15:05:32 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-06-10T15:05:32Z</dc:date>
    <item>
      <title>how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817406#M81863</link>
      <description>&lt;P&gt;Hello All&lt;/P&gt;&lt;P&gt;I would like to sum up the numbers from the text below:&lt;/P&gt;&lt;P&gt;Kwota=237 + Kwota=30.87 + Kwota=292.74 + Kwota=292.74 + Kwota=300.87 that is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Kwota=1154,22&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;In the example there are 5 phrases but their number may vary.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;text:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;[Forma sprzedazy=leasing, auto: Audi A 6, Kwota=237, Cena =123],[Forma sprzedazy=leasing, auto: Volvo, Kwota=30.87, Cena =10],[Forma sprzedazy=leasing, auto: VW Golf, Kwota=292.74, Cena =134],[Forma sprzedazy=leasing, auto: VW Golf, Kwota=292.74, Cena =134],[Forma sprzedazy=leasing, auto: Porche, Kwota=300.87, Cena =152]&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 06:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817406#M81863</guid>
      <dc:creator>Jacek2</dc:creator>
      <dc:date>2022-06-10T06:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817484#M81864</link>
      <description>&lt;P&gt;Do you have a SAS data set?&lt;/P&gt;
&lt;P&gt;If so show us the results of Proc Contents on the data set.&lt;/P&gt;
&lt;P&gt;If you do not have a SAS data set the first thing will be to create one and should show use some of the source file so we can help create one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Almost certainly you have to do something to separate the text into meaningful variables possibly such as Model, Kwota and Cena (what ever those are). Then the sum would be easy.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 15:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817484#M81864</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-10T15:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817503#M81865</link>
      <description>&lt;P&gt;If the text is pretty simple it is not hard to parse it in SAS code.&lt;/P&gt;
&lt;P&gt;Looks like you have values in brackets (square brackets if you speak British) separated by commas.&amp;nbsp; Within that you have name value pairs separated by commas.&amp;nbsp; Strangely some of the name value pairs are separated by = and others by ':'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could build a regular expression to parse, but for your simple example you can just use SCAN(), especially if all you want is the KWOTA (?quota?) values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's start by creating a dataset from the information in your post so we have something to code with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  text='[Forma sprzedazy=leasing, auto: Audi A 6, Kwota=237, Cena =123]'
    ||',[Forma sprzedazy=leasing, auto: Volvo, Kwota=30.87, Cena =10]'
    ||',[Forma sprzedazy=leasing, auto: VW Golf, Kwota=292.74, Cena =134]'
    ||',[Forma sprzedazy=leasing, auto: VW Golf, Kwota=292.74, Cena =134]'
    ||',[Forma sprzedazy=leasing, auto: Porche, Kwota=300.87, Cena =152]'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can loop over the name/value pairs in TEXT and when the name is KWOTA convert the value to a number and accumulate the amount.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  dlm='[],=:';
  do word = 1 to countw(text,dlm) by 2;
    length name value $50 ;
    name=left(scan(text,word,dlm));
    value=left(scan(text,word+1,dlm));
    if name='Kwota' then kwota=sum(kwota,input(value,??32.));
  end;
  drop word name value dlm  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs     kwota

 1     1154.22
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 16:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817503#M81865</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-10T16:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817504#M81866</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/427324"&gt;@Jacek2&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This should get you started:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length text $1000;
text=cat('[Forma sprzedazy=leasing, auto: Audi A 6, Kwota=237, Cena =123],',
         '[Forma sprzedazy=leasing, auto: Volvo, Kwota=30.87, Cena =10],',
         '[Forma sprzedazy=leasing, auto: VW Golf, Kwota=292.74, Cena =134],',
         '[Forma sprzedazy=leasing, auto: VW Golf, Kwota=292.74, Cena =134],',
         '[Forma sprzedazy=leasing, auto: Porche, Kwota=300.87, Cena =152]');
output;
text=cat('[Forma sprzedazy=leasing, auto: Audi A 6, Kwota = 237, Cena =123],',
         '[Forma sprzedazy=leasing, auto: Volvo, Kwota 30.87, Cena =10],',
         '[Forma sprzedazy=leasing, auto: VW Golf, Kwota:292.74, Cena =134],',
         '[Forma sprzedazy=leasing, auto: VW Golf  Kwota=292.74 Cena =134],',
         '[Forma sprzedazy=leasing, auto: Porsche, kwota=2,300.87, Cena =852],',
         '[Forma sprzedazy=leasing, auto: Horch, Kwota=60,000],',
         '[Forma sprzedazy=leasing, auto: N/A, Kwota=N/A, Cena =N/A],',
         '[Forma sprzedazy=leasing, auto: N/A, Kwota=, Cena =]');
output;
run;

data want(keep=text total);
set have;
if ~id then id+prxparse('/Kwota[=: ]*(\d+,?\d*\.?\d*)/i');
start=1;
stop=length(text);
link add;
do while(pos);
  link add;
end;
add:
  call prxnext(id, start, stop, text, pos, len);
  total=sum(total,input(prxposn(id, 1, text),?? comma32.));
return;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have added a second observation with eight "phrases" involving various deviations from the standard layout to demonstrate the robustness of the algorithm:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;blanks around the equal sign after "Kwota"&lt;/LI&gt;
&lt;LI&gt;blank or colon instead of equal sign after "Kwota"&lt;/LI&gt;
&lt;LI&gt;missing commas around the "Kwota" portion of the phrase&lt;/LI&gt;
&lt;LI&gt;lowercase "kwota"&lt;/LI&gt;
&lt;LI&gt;thousands separator&lt;/LI&gt;
&lt;LI&gt;missing "Cena" portion of the phrase&lt;/LI&gt;
&lt;LI&gt;invalid numeric data ("N/A") after "Kwota="&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;no data&amp;nbsp;after "Kwota="&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The Perl regular expression &lt;FONT face="courier new,courier"&gt;'/Kwota[=: ]*(\d+,?\d*\.?\d*)/i'&lt;/FONT&gt; describes a pattern consisting of the word "Kwota" (case-insensitive), followed by any number of equal signs, colons or blanks, at least one digit and possibly: a comma, more digts, a decimal point and decimals. The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1obc9u7z3225mn1npwnassehff0.htm" target="_blank" rel="noopener"&gt;CALL PRXNEXT routine&lt;/A&gt; performs the search repeatedly as long as the pattern is found (condition &lt;FONT face="courier new,courier"&gt;pos&amp;gt;0&lt;/FONT&gt;). The relevant part of the string TEXT matching the pattern (i.e., the digits with thousands separator and decimal point, if any) is extracted by the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1lru1b4uoogqvn1ig446q4c6muu.htm" target="_blank" rel="noopener"&gt;PRXPOSN function&lt;/A&gt;, converted to a&amp;nbsp; numeric value (if possible) by the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p19en16vskd2vhn1vwmxpxnglxxs.htm" target="_blank" rel="noopener"&gt;INPUT function&lt;/A&gt; and finally added to variable TOTAL by the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0zxive1z1ctqin12w06c85jfigd.htm" target="_blank" rel="noopener"&gt;SUM function&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that a number format like 1&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/FONT&gt;234&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;,&lt;/FONT&gt;&lt;/STRONG&gt;56 in the data would require the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n0aggga4tdr094n14ecw1smkhva7.htm" target="_blank" rel="noopener"&gt;COMMAX. informat&lt;/A&gt; instead of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n1dvsmv8t9o60gn1dk1gkev91nan.htm" target="_blank" rel="noopener"&gt;COMMA. informat&lt;/A&gt;. A mixture of different formats (e.g., 123.45 in some places and 123,45 in others) is not supported by the suggested code. Numbers with two thousands separators (like 1,199,000) are not supported either.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 17:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817504#M81866</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-06-10T17:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817716#M81867</link>
      <description>&lt;P&gt;Thank you for the answer, the problem has already been solved by colleagues below.&lt;/P&gt;&lt;P&gt;Alternatively:&lt;/P&gt;&lt;PRE&gt;data want;
  length kwota_total 8;
  set have;
  rxid = prxparse ('/kwota=(\d+\.?\d+)/i');
  start = 1;
  stop = length(text);
  do while (_n_);
    call prxnext (rxid, start, stop, text, position, length);
    if position &amp;lt; 1 then leave;
    kwota_total = sum(kwota_total, input (prxposn(rxid,1,text), best32.));
  end;
  drop rxid start stop position length;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jun 2022 07:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817716#M81867</guid>
      <dc:creator>Jacek2</dc:creator>
      <dc:date>2022-06-13T07:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817717#M81868</link>
      <description>Thank you Tom, it works perfectly!!</description>
      <pubDate>Mon, 13 Jun 2022 07:12:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817717#M81868</guid>
      <dc:creator>Jacek2</dc:creator>
      <dc:date>2022-06-13T07:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to sum numbers from selected phrases?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817718#M81869</link>
      <description>Thank you for your comprehensive answer, it works perfectly!!!</description>
      <pubDate>Mon, 13 Jun 2022 07:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-sum-numbers-from-selected-phrases/m-p/817718#M81869</guid>
      <dc:creator>Jacek2</dc:creator>
      <dc:date>2022-06-13T07:16:56Z</dc:date>
    </item>
  </channel>
</rss>

