在最后一个逗号SAS之后删除所有内容

时间:2019-01-03 13:55:02

标签: sas substr

我有一个地址字段,其地址类似于:

1 High Street, London, Uk, L1 6YR 
Flat 2, 35 Second Street, London,Greater London, L14 8FG

我想摆脱上一个逗号之后的所有内容,以便最终得到:

   1 High Street, London, Uk
   Flat 2, 35 Second Street, London,Greater London

我试图这样做:

DATA WANT;
SET HAVE;
ADDRESS_NEW = SCAN(ADDRESS, -1, ",");
RUN;

但这只是返回邮政编码:

L1 6YR
L14 8FG

2 个答案:

答案 0 :(得分:1)

您可以使用PRXCHANGE来修改符合所需条件的文本值。本示例使用正则表达式模式s/,[^,]*$//将查找到的末端部分替换为空白。

data have;
input address $80.;
datalines; 
1 High Street, London, Uk, L1 6YR 
Flat 2, 35 Second Street, London,Greater London, L14 8FG
Argy Bargy Lane
London, Uk
run;

data want;
  set have;
  address = prxchange("s/,[^,]*$//", 1, address);
run;

答案 1 :(得分:1)

具有B选项的FindC。

24   data _null_;
25      input address $80.;
26      put / address=;
27      l = findc(address,',','b');
28      if l then address = substrn(address,1,l-1);
29      put  (_all_)(=);
30      datalines;


address=1 High Street, London, Uk, L1 6YR
address=1 High Street, London, Uk l=26

address=Flat 2, 35 Second Street, London,Greater London, L14 8FG
address=Flat 2, 35 Second Street, London,Greater London l=48

address=Argy Bargy Lane
address=Argy Bargy Lane l=0

address=London, Uk
address=London l=7