使用GT和LT的PROC SQL条件插入

问题描述 投票:0回答:2

我需要插入一堆行,空行,除了我的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开头。

非常感谢!

sql insert sas proc
2个回答
2
投票

我想你想要一个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;

1
投票

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
  ;
© www.soinside.com 2019 - 2024. All rights reserved.