我正在运行类似以下代码的内容:
proc sql;
create table my_table as
select * from other_table
where myvar between "01" and "08";
quit;
然后我得到的结果包括“ 000”。如果我改写
proc sql;
create table my_table as
select * from other_table
where myvar between "01" and "08" and not "000";
quit;
我得到一个空的数据集。
我是新手,但是可以帮助您。
答案 0 :(得分:4)
我无法重现此行为:
data have;
input myvar :$3.;
cards;
00
04
08
000
001
;
run;
proc sql;
select myvar from have
where myvar between "01" and "08";
quit;
是否有可能已将格式应用于变量,从而将"01"-"08"
范围内的另一个值伪装为"000"
?您可以通过指定显示结果时使用的格式来排除这种情况:
proc sql;
select myvar format=$3. from have
where myvar between "01" and "08";
quit;
当将where子句更改为包含"000"
时,结果确实得到了"00"
:
proc sql;
select myvar from have
where myvar between "00" and "08"
order by myvar;
quit;
这似乎是因为比较不同长度的字符串时,默认SAS排序规则序列中的空格在零之前。
添加额外条件会导致返回零行:
proc sql;
select myvar from have
where (myvar between "00" and "08") and not "000";
quit;
这是因为在SAS中,任何字符串的逻辑值均为True
,然后您将其取反。我添加了括号来阐明正在发生的事情。如果删除not
,则会得到between
表达式返回的所有行。
如果要排除所有3位代码以及000,可以通过明确检查长度来做到这一点:
proc sql;
select myvar from have
where (myvar between "00" and "08") and length(myvar) = 2;
quit;
答案 1 :(得分:1)
另一个想法是将您的文本转换为数字,然后使用1到8之间的数字...
where input(char,8.) between 1 and 8;