BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sean_OConnor
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
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;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
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;
Sean_OConnor
Obsidian | Level 7

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

Kurt_Bremser
Super User

 

 

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;
Kurt_Bremser
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1234 views
  • 0 likes
  • 3 in conversation