如何从SAS中的两列中提取不匹配的字符

时间:2019-06-14 18:50:32

标签: sql sas

我在SAS企业指南中有两列,其中有一个字符列表。例如,第一列可能有7个字符,第二列也可能有7个字符,因此它们是匹配项。但是,有时列不匹配,我想将不匹配的字符放入新列中。 我已经尝试过查找功能。

   proc sql;
    create table Z
    as
    select *,
    case when (act_route_path<>designed_route_path) then 'Non Match'
    else 'Match' end as Off_at_Location_1
    from G;
    run;

上面的代码运行没有错误,但是我不希望布尔真实结果为“不匹配”。我希望它告诉我不匹配的字符。在案件陈述中这样做可行吗?

我确实有两列,我的数据用(-)分隔,并且可以将这两列用于输出。

SEA-MDW-EFH-STL ----------- SEA-POR-MDW-EFH-STL

在上一行中,我希望SAS放入新列POR。谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您是否正在寻找类似Red的东西?

data g; 
    length act_route_path $ 2 designed_route_path $ 2;`
    input act_route_path $ designed_route_path $;
datalines;
    xx xy
    xx xx
    yy yy
    yx yy
;
run;

proc sql; create table z as
    select act_route_path, designed_route_path, 
        case when Off_at_Location_1 = 0 and substr2 = 0 then designed_route2 
            else 'Match' end as result
    from(select *
        ,case when act_route_path = designed_route_path then 1 else 0 end as Off_at_Location_1

        ,substr(act_route_path,1,1) as act_route1
        ,substr(act_route_path,1,2) as act_route2
        ,substr(designed_route_path,1,1) as designed_route1
        ,substr(designed_route_path,2,1) as designed_route2

        ,index(substr(act_route_path,1,1),substr(designed_route_path,1,1)) as substr1
        ,index(substr(act_route_path,2,1),substr(designed_route_path,2,1)) as substr2
    from g
)a
;
quit;

如果您发布数据,可以使代码更整洁。

答案 1 :(得分:0)

您可以使用compress对两个路由值进行反联接比较

data have;
  input route1 $8. route2 $8.; datalines;
ABCDEFGH CDEFGHI
ACEGI    BCDEFGHI
ABCD     EFGH
1234     1234
run;

proc sql;
  create table want as
  select route1, route2
  , case 
      when route1=route2 then 'Same route'
      else 
        'diff:' 
        || '-' || trim(compress(route1, route2))  /* chars in 1 not in 2 */
        || '+' || trim(compress(route2, route1))  /* chars in 2 not in 1 */
    end  as route_compare length=25
  from have;