我有一个简短的向量,我希望能够在PROC SQL的case语句中使用。我没有从我的研究中找到任何东西。让我举个例子来解释一下。
我有一个手机品牌的载体:
phone_brand
nokia
samsung
apple
microsoft
sony
motorolla
我想在另一个包含更多数据的表上创建一个PROC SQL语句,但我想创建一个指示符,告诉我表中的phone_brand字段是否与矩阵匹配。所以代码的想法是这样的:
proc sql;
create table test as
select id
,date
,case when index(home.phone_brand,'vector values') > 0 then 1 else 0
end as vector_ind
from ods.home;
quit;
我需要这个,因为我拥有的矢量将是动态的,但没有任何关键来识别品牌。所以我必须使用索引函数来搜索匹配项。如果我找不到使用向量的方法,我想我唯一的其他选择是每次向量中的数据改变时手动更新代码:
,case when index(home.phone_brand,'nokia') > 0 then 1
when index(home.phone_brand,'samsung) > 0 then 1
when index(home.phone_brand,'apple) > 0 then 1
.......
else 0 end as vector_ind
如果向量中的品牌数量显着增加,这将是繁琐且难以扩展的。
答案 0 :(得分:2)
我认为你正在寻找IN运营商。
<#if portlet_display.getPortletDecoratorId() != "barebone">
<h2 class="portlet-title-text">${portlet_title}</h2>
</#if>
您还可以将列表存储在宏变量
中proc sql;
select name , sex , age
, name in ('Alfred','Carol') as vector_ind
from sashelp.class
;
然后在查询中使用宏变量。
%let vector = 'Alfred','Carol' ;
或将其存储在数据集
中select name , sex , age
, name in (&vector) as vector_ind
from sashelp.class
;
并使用子查询。
create table vector as
select name
from sashelp.class
where name in (&vector)
;
或者你可以结合最后两个。将值存储在数据集中,但使用它来构建要在查询中使用的宏变量。
select name , sex , age
, name in (select name from vector) as vector_ind
from sashelp.class
;