如何使用 SAS 或 PROC SQL 从列中删除字符串和标点符号?

问题描述 投票:0回答:1

我有一个专栏,里面有很多公司名称,没有任何清理,如下所示。但我想进行清理以从公司名称中删除字符串列表,以便我可以将它们分组在一起。

我有一个专栏,里面有很多公司名称,没有任何清理,如下所示。但我想进行清理以从公司名称中删除字符串列表,以便我可以将它们分组在一起。

字符串列表:com、llc、ltd、corp、stores

| Company |
| --- |
| Amazon |
| Amazon.com |
| Amazon.llc |
| Amazon.ltd |
| Amazon corp |
| Amazon stores |

我还想从公司名称中删除标点符号和空格,因此期望的结果可以是这样的:

| Company |
| --- |
| Amazon |
| Amazon |
| Amazon |
| Amazon |
| Amazon |
| Amazon |

我已经使用Python完成了我的项目,但我需要将其转换为SAS或PROC SQL,但我不熟悉这两种语言,所以感谢任何帮助。

sas proc-sql regex-replace
1个回答
0
投票

正则表达式应该可以让这变得简单:

([\. ])(com|ltd|llc|corp|stores)

https://regex101.com/r/FhnEwN/1

在 SAS 中,使用正则表达式查找和替换字符串是通过

prxchange
函数完成的,其工作原理如下:

string = prxchange('s/regex here/replacement string/', times, string);

其中

times
是您要进行的替换数量。 -1表示替换所有匹配项。如果将替换字符串保留为空值,如下所示:
's/regex here//'
,那么您将有效地删除匹配的值。

将其转换为 SAS:

data want;
    set have;
    company = prxchange('s/([\. ])(com|ltd|llc|corp|stores)//', -1, company);
run;
Company
Amazon
Amazon
Amazon
Amazon
Amazon
Amazon
© www.soinside.com 2019 - 2024. All rights reserved.