Hi,
I would like to delete all rows for IDs who only have 0s for var
So, in the example below, I would delete all rows for ID#2.
Somehow, I have to read all obs in each by group and then flag those who only have 0 but having trouble finding how..
Thanks very much!!
| Have | Want | |||
| ID | var | ID | var | |
| 1 | 1 | 1 | 1 | |
| 1 | 0 | 1 | 0 | |
| 1 | 0 | 1 | 0 | |
| 1 | 0 | 1 | 0 | |
| 1 | 1 | 1 | 1 | |
| 1 | 0 | 1 | 0 | |
| 1 | 1 | 1 | 1 | |
| 1 | 0 | 1 | 0 | |
| 2 | 0 | 3 | 0 | |
| 2 | 0 | 3 | 0 | |
| 2 | 0 | 3 | 0 | |
| 2 | 0 | 3 | 1 | |
| 3 | 0 | |||
| 3 | 0 | |||
| 3 | 0 | |||
| 3 | 1 |
data have;
input ID var ;
cards;
1 1 1 1
1 0 1 0
1 0 1 0
1 0 1 0
1 1 1 1
1 0 1 0
1 1 1 1
1 0 1 0
2 0 3 0
2 0 3 0
2 0 3 0
2 0 3 1
3 0
3 0
3 0
3 1
;
proc sql;
create table want as
select *
from have
group by id
having n(id) ne sum(var=0);
quit;
data have;
input ID var ;
cards;
1 1 1 1
1 0 1 0
1 0 1 0
1 0 1 0
1 1 1 1
1 0 1 0
1 1 1 1
1 0 1 0
2 0 3 0
2 0 3 0
2 0 3 0
2 0 3 1
3 0
3 0
3 0
3 1
;
proc sql;
create table want as
select *
from have
group by id
having n(id) ne sum(var=0);
quit;
thanks!!
data have;
input ID var;
cards;
1 1
1 0
1 0
1 0
1 1
1 0
1 1
1 0
2 0
2 0
2 0
2 0
3 0
3 0
3 0
3 1
;
data want;
if _N_ = 1 then do;
declare hash h(dataset:'have(where=(var=1))');
h.defineKey('ID');
h.defineDone();
end;
set have;
if h.check()=0;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.