我需要插入一堆行,空行,除了我的sasdate var,我需要这个条件的结果:if (17530 < sasdate < 20800) then sasdate = .
但我似乎无法做到这一点。
proc sql;
create table ds as
select sasdate, avg(x) as avg_x
from ds1
group by sasdate;
insert into ds
if (17530 <= sasdate <= 20800) then .;
quit;
我哪里错了?我知道初学者我可能不能在这里使用if
语句,但我不知道还能用什么。
基本上我想在我的数据集的顶部添加一堆行,其中第一个sasdate = 17530,然后nonblank(我正在添加的数据集,ds)以sasdate = 20800开头。
非常感谢!
我想你想要一个case
表达。例如,这将使您范围内的sasdate
值无效:
create table ds as
select (case when sasdate < 17530 or sasdate > 20800 then sasdate end) as sasdate,
avg(x) as avg_x
from ds1
group by ds1.sasdate;
SQL没有用于创建新行的内置迭代器或循环语法。您可以直接加入一个单独的日期表,涵盖范围内的每一天,以“填写”“左”表的日期。
平均函数是MEAN
例如:
data alldays;
do date = 17530 to 20800;
output;
end;
run;
data have;
date = 20800;
x = 1;
do sequence = 1 to 10;
x = sum (x, lag(x));
output;
end;
run;
proc sql;
create table have_ave as
select date, mean (x) as mean_x
from have
group by date;
create table want as
select
coalesce (have_ave.date, alldays.date) as date
, mean_x
from have_ave
right join alldays on have_ave.date = alldays.date
;