Folks,
I have a dataset of ids and numeric values. I would like to output two datasets. The first is any id's where either id number has a value greater than zero.
The second is a dataset where either id is only 0.
See below for example;
data have;
input id $ value ;
datalines;
1 0
2 1
2 0
3 0
3 10
4 11
4 15
5 0
5 0
;run;
data want_1;
input id $ value ;
datalines;
2 1
2 0
3 0
3 10
4 11
4 15
;run;
data want_2;
input id $ value ;
datalines;
1 0
5 0
5 0
;run;
proc sql;
create table want_1 as
select * from have
group by id
having sum(value)>0;
quit;
proc sql;
create table want_2 as
select * from have
group by id
having sum(value)=0;
quit;
proc sql;
create table want_1 as
select * from have
group by id
having sum(value)>0;
quit;
proc sql;
create table want_2 as
select * from have
group by id
having sum(value)=0;
quit;
I'm just wondering how would I amend the having part of this sql query if I wanted to output if either value had a string called 'YES'
proc sql;
create table previous_LPT_return as
select * from Stage_2_LPT
group by id_document
having value='YES';
quit;
The above example I've posted seems to only omit all the yes strings
proc sql;
create table previous_LPT_return as
select *
from Stage_2_LPT
where id_document in (
select distinct id_document
from Stage_2_LPT
where value = 'YES'
);
quit;
or you change one line in my previous code:
if value = 'YES' then flag = 1;
Create a lookup table, and merge that back:
data have;
input id $ value ;
datalines;
1 0
2 1
2 0
3 0
3 10
4 11
4 15
5 0
5 0
;
run;
data lookup (keep=id);
set have;
by id;
retain flag;
if first.id then flag = 0;
if value then flag = 1;
if last.id and flag then output;
run;
data want1 want2;
merge
have (in=a)
lookup (in=b)
;
by id;
if b
then output want1;
else output want2;
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 lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.