簡體   English   中英

SAS和proc SQL

[英]SAS and proc sql

如果滿足條件,我就必須擺脫一個主題。

數據:

Name Value1 
A      60
A      30
B      70
B      30
C      60
C      50
D      70
D      40

我想要的是,如果value = 30,那么這兩行都不應該出現在輸出中。

期望的outpu是

Name Value1 
C      60
C      50
D      70
D      40

我已經在proc sql中編寫了一個代碼

proc sql;
  create table ck1 as
  select * from ip where name in
     (select distinct name from ip where value = 30)
     order by name, subject, folderseq;
quit;

將您的SQL更改為:

proc sql;
  create table ck1 as
  select * from ip where name not in
     (select distinct name from ip where value = 30)
     order by name, subject, folderseq;
quit;

數據步長法:

data have;
input Name $ Value1;
datalines;
A      60
A      30
B      70
B      30
C      60
C      50
D      70
D      40
;;;;
run;
data want;
do _n_ = 1 by 1 until (last.name);
  set have;
  by name;
  if value1=30 then value1_30=1;
  if value1_30=1 then leave;
end;
do _n_ = 1 by 1 until (last.name);
    set have;
    by name;
    if value1_30 ne 1 then output;
end;
run;

還有一種替代的,稍快的方法,在某些情況下,當value1_30為1時避免使用第二條set語句(特別是如果大多數參數中有30條,這會更快,因此您只保留少量的記錄)。

data want;
do _n_ = 1 by 1 until (last.name);
  set have;
  by name;
  counter+1;
  if first.name then firstcounter=counter;
  else if last.name then lastcounter=counter;
  if value1=30 then value1_30=1;
  if value1_30=1 then leave;
end;
if value1_30 ne 1 then
  do _n_ = firstcounter to lastcounter ;
    set have point=_n_;
    output;
  end;
run;

另一個SQL選項...

proc sql number; 
select 
a.name, 
a.value1, 
case
when value1 = 30 then 1
                 else 0
end as flag,
sum(calculated flag) as countflagpername
from have a
group by a.name
having countflagpername = 0
;quit;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM