我有一个数据,其中A列包含以下数据
Column A
--------
1
2
?
2
我使用了查询:
proc sql;
select
if A= '?' then A=., count(*) as N_obs
from freq_sex_Partner
group by Number_of_sexual_partners;
quit;
这不起作用。请提出我该如何更换?达到任何标准值?
答案 0 :(得分:1)
在SQL中,这是CASE语句,而不是IF / THEN。
proc sql;
select
case when a='?' then .
else a end as a, count(*) as N_obs
from freq_sex_Partner
group by Number_of_sexual_partners;
quit;
或者您也可以使用IFC()函数。
proc sql;
select
ifc(a='?', ., a) as a, count(*) as N_obs
from freq_sex_Partner
group by Number_of_sexual_partners;
quit;
答案 1 :(得分:1)
列A包含"?"
,因此它是字符值。 @reeza代码应为then ""
或ifc(a='?',"", a)
。另外,如果您还没有选择分组变量,则N_obs
的上下文会丢失。
建议
data have;
input a $ nsp ;
datalines;
1 2
2 3
? 7
2 7
run;
proc sql;
select
nsp
, case when a='?' then '' else a end as a
, count(*) as nsp_count
from have
group by nsp
;
quit;
当Proc SQL正在执行组聚合与组内单个行的自动重新合并时,查询还将记录消息NOTE: The query requires remerging summary statistics back with the original data.
。