PROC SQL条件条件

时间:2019-04-08 17:43:22

标签: sas

我正在尝试删除许多记录,其中REASON_FOR_VISIT变量字符串的范围从心理问题到哮喘问题以及KEEP患者的敏度为3。但是,当我运行此代码并执行REASON_FOR_VISIT的触发频率时,我仍然获得包含要删除的记录关键字的结果。

proc sql;
create table NoPysch as
Select *
from ED_TAT
Where 
    Pt_Acuity like '%3%'
    AND TRACK_Group like '%ED Tracking%'
    AND REASON_FOR_VISIT not like '%asth%' 
AND REASON_FOR_VISIT not like '%asthma%' 
AND REASON_FOR_VISIT not like '%asthma/%' 
    AND REASON_FOR_VISIT not like '%suicidal%' 
AND REASON_FOR_VISIT not like '%assualt%' 
AND REASON_FOR_VISIT not like '%assult%'
AND REASON_FOR_VISIT not like '%APA%' 
AND REASON_FOR_VISIT not like '%legal%'  
AND REASON_FOR_VISIT not like '%suicide%'
AND REASON_FOR_VISIT not like 'ASA'
AND REASON_FOR_VISIT not like '/ASA'
AND REASON_FOR_VISIT not like '%SI/%'
AND REASON_FOR_VISIT not like '%SI, attempt%'
AND REASON_FOR_VISIT not like '%SI w%'
AND REASON_FOR_VISIT not like 'SI'
AND REASON_FOR_VISIT not like 'HI'
AND REASON_FOR_VISIT not like '%homicide%'
AND REASON_FOR_VISIT not like '%psyc%'
AND REASON_FOR_VISIT not like '%psch%'
AND REASON_FOR_VISIT not like '%FD-12%'
AND REASON_FOR_VISIT not like '%behavior%'
AND REASON_FOR_VISIT not like '%behav%'
AND REASON_FOR_VISIT not like '%overdose%'
AND REASON_FOR_VISIT not like '%agitation%'
    AND REASON_FOR_VISIT not like '%aggression%'
AND REASON_FOR_VISIT not like '%psy eval%';
run; 

proc freq data=NoPysch;
table REASON_FOR_VISIT;
run;

1 个答案:

答案 0 :(得分:0)

考虑将相似的目标放在单独的查找表中,而不是使用过滤条件来污染代码。检查查找表中是否存在值的一种形式称为存在子选择。

大多数目标具有%<target>%所指定的“包含”性质,并且有些目标没有通配符,例如'SI''HI',这些通配符必须匹配完全是为了被过滤。

如果所有项目实际上都是“包含”匹配类型,只需在查找表中指定目标值,然后将括号%添加到子选择查询的代码中。如果您想拥有各种类似的匹配项(以<target>%开始,以%<target>结尾或包含%<target>%,则查找目标将需要包含通配符。

例如,由于 NOT EXISTS ,存在性查询检查所有原因过滤器。如果没有任何过滤器表达式与原因匹配,那么将选择记录。

data reason_like_filter;
input; 
expression = _infile_;
datalines4;
%asth%
%asthma%
%asthma/%
%suicidal%
%assualt%
%assult%
%APA% 
%legal%  
%suicide%
ASA
/ASA
%SI/%
%SI, attempt%
%SI w%
SI
HI
%homicide%
%psyc%
%psch%
%FD-12%
%behavior%
%behav%
%overdose%
%agitation%
%aggression%
%psy eval%
;;;;

data ed_tat;
  pt_acuity = 'AABB3DDEE';
  track_group = 'Regional ED Tracking base';
  length pid 8 reason_for_visit $50;
  input pid reason_for_visit;
  infile cards dsd dlm=',';
datalines;
1, "Suicide"
2, "Suicidal"
3, "Psy eval mm//dd/yy"
4, "Drop off cookies"
5, "Visit friend"
6, "Heart attack"
run;

proc sql;
  create table want as
  select * 
  , (
select count(*)
      from reason_like_filter FILTER
      where upcase(REASON_FOR_VISIT) LIKE upcase(FILTER.expression)
) as N

  from ED_TAT
  where 
   Pt_Acuity like '%3%'
   AND TRACK_Group like '%ED Tracking%'
   AND NOT EXISTS (
      select *
      from reason_like_filter FILTER
      where upcase(REASON_FOR_VISIT) LIKE trim(upcase(FILTER.expression))
   )
   ;
quit;