我在sql中有如下代码(很多,而不是ins,但只想列出一些),我是sas新手,对proc sql有点了解,每天都在学习和探索,
Select * from table
Where date=‘20180112’
and type=‘apple’ and location=‘dc’ and not
(columnName)in(‘a’,’b’) And lat=‘ten’
我无法理解以下sql的sas等效项。有人可以解释一下如果部分的SAS代码,然后做
Data sample;
Set sourcetble;
If date=‘20180112’ and type=‘apple’
And location=‘dc’ then do;
Blah1=‘rain’
Blah2=‘something else’
If columnName in(‘a’, ‘b’) and lat=‘ten’ Then do;
答案 0 :(得分:0)
这只是基于WHERE语句中的值和变量的子集。
Data sample;
set table;
WHERE date='20180112' and type='apple' And location='dc'
and columnName in (‘a’, ‘b’) and lat=‘ten’;
<other optional code>;
run;
答案 1 :(得分:0)
与SQL查询不同,SAS数据步骤将导致创建新的数据集。如果不需要新的数据集,则可以使用“ data _null_;”。另外,有些SAS过程将只显示数据集,例如SQL“ select”即可。
SAS中的“集合”等同于SQL中的“来自”:它指定用于构建新数据集的基础数据集。
默认情况下,SAS数据步骤保留“设置”数据集的所有变量。它等效于SQL中的“选择*”。如果只需要一些变量,则可以在SAS中使用“ keep”和“ drop”语句。
“ where”子句和“ and” /“ or”运算符在SAS和SQL中的工作方式相似,但语法略有不同。
答案 2 :(得分:0)
数据步骤中的if … then
与问题中显示的SQL没有对应关系。 SQL中的条件分配是使用case
语句完成的。
例如
这样的DATA步骤语句data want;
set have;
…
if date="20180112" and type="apple" and location="dc" then do;
Blah1="rain";
Blah2="something else";
end;
将与SQL保持一致
Proc SQL;
create table want as
select …
, case when date="20180112" and type="apple" and location="dc"
then "rain"
else ""
end as Blah1
, case when date="20180112" and type="apple" and location="dc"
then "something else"
else ""
end as Blah2
from
have
…
;
对于某些算法,当满足某些条件(if
逻辑)时需要一次分配多个变量:
do; … end;
语法,其中可以包含多个赋值语句。case statement
)分配一个变量,因此必须针对基于条件分配的每个变量重复逻辑代码。