SAS在字符串中搜索多个值

时间:2017-12-06 16:04:27

标签: sas

我有两个SAS数据集(假设为了简单)每个都有一个char变量。第一个数据集有一个带有公司描述的变量(有时包括城市,有时不包括;凌乱的字段),第二个数据集有一个变量,其中列出了所有城市。我需要在第一个数据集中创建变量,如果找到第二个数据集中的任何城市,结果不应该只包含0或1个答案,而是城市本身。 有没有循环INDEXW(或类似)函数的简单方法呢?

1 个答案:

答案 0 :(得分:3)

indexw有什么问题?使用proc sqlindexw可以提供非常直接的解决方案。

示例数据:

data have_messy;
  length messy $100;
  messy = 'this is a city name: brisbane' ; output;
  messy = 'this is a city name: sydney'   ; output;
  messy = 'this is a city name: melbourne'; output;
run;

data have_city;
  length city $20;
  city = 'sydney'  ; output;
  city = 'brisbane'; output;
run;

示例查询:

proc sql noprint;
  create table want as
  select a.*,
         b.city
  from have_messy a
  left join have_city  b on indexw(a.messy, b.city)
  ;
quit;

结果:

messy                               city 
===============================     =========
this is a city name: sydney         sydney 
this is a city name: brisbane       brisbane 
this is a city name: melbourne   

注意 - 如果找到多个城市名称,则上述查询可以在表a中每行返回多个结果。我建议您根据需要运行后续步骤来处理任何重复的行。