如何从选定的短语中汇总数字? (SAS,SQL)

发布于 2025-02-06 18:02:27 字数 577 浏览 1 评论 0原文

我想从下面的文本中总结数字:

Kwota=237 + Kwota=30.87 + Kwota=292.74 + Kwota=292.74 + Kwota=300.87

即:

kwota = 1154,22

在示例中有5个短语,但它们的数字可能会有所不同。

文字:

[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]

I would like to sum up the numbers from the text below:

Kwota=237 + Kwota=30.87 + Kwota=292.74 + Kwota=292.74 + Kwota=300.87

that is:

Kwota=1154,22

In the example there are 5 phrases but their number may vary.

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2025-02-13 18:02:27

您可以使用perl正则表达式查找kwota =< number>零件,使用匹配组提取数字文本,input> input将文本转换为数字可以以总和或总变量积累。

示例:

data have;
length text $2000;
text = catx(',',
  '[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 = 'Nothing here';
output;
run;

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 < 1 then leave;

    kwota_total = sum(kwota_total, input (prxposn(rxid,1,text), best32.));
  end;
  drop rxid start stop position length;
run;

在此处输入图像描述“

另外,如果您直接从文本文件中读取,则可以使用基于字符的列指针控制的输入语句,例如

  ... in a loop on a single line ...
    INPUT @'Kwota=' amount @;
    total = sum(total,amount);

You can use Perl regular expressions to find the kwota=<number> pieces, extract the number text using a matching group, and input function to convert the text to a number that can be accumulated in a sum or total variable.

Example:

data have;
length text $2000;
text = catx(',',
  '[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 = 'Nothing here';
output;
run;

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 < 1 then leave;

    kwota_total = sum(kwota_total, input (prxposn(rxid,1,text), best32.));
  end;
  drop rxid start stop position length;
run;

enter image description here

Alternatively, if you are reading from a text file directly, you could use an INPUT statement with character based column pointer control such as

  ... in a loop on a single line ...
    INPUT @'Kwota=' amount @;
    total = sum(total,amount);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文