BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SanKH1
Quartz | Level 8

I have this dataset

data test;

input store$ type1 type2;

datalines;

1 1 0
1 0 1
2 0 0
2 0 1
3 1 0
3 0 0;

run;

 

We need to only filter stores that meet both type1=1 and type2=1. In this case, only store 1 satisfies this condition. 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

If you prefer to use SQL:

 

proc sql;
create table want as
select * from test
group by store
having max(type1)=1 and max(type2)=1;
quit;
PG

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

We need to only filter stores that meet both type1=1 and type2=1. In this case, only store 1 satisfies this condition.


I don't see any such store satisfying the condition in your data.

--
Paige Miller
SanKH1
Quartz | Level 8
Sorry, I should've explain more. From the same store, first observation: type1 needs to be 1 and second observation: type2 needs to be 1.
PaigeMiller
Diamond | Level 26
proc summary data=have nway;
    class store;
    var type1 type2;
    output out=intermediate sum=;
run;
data want;
    set intermediate;
    where type1=1 and type2=1;
run;
--
Paige Miller
SanKH1
Quartz | Level 8
Thanks, it works. But to simplify things, I didn't include other variables in the datalines that are part of the dataset. Is there a way to keep all columns?
PaigeMiller
Diamond | Level 26

@SanKH1 wrote:
Thanks, it works. But to simplify things, I didn't include other variables in the datalines that are part of the dataset. Is there a way to keep all columns?

It is a good idea to write a thorough problem description. Then we don't have to do "I don't understand"—"Oh thank you for the explanation" — "Here's code that works" — "No it doesn't work for the real problem" dance. Yes of course, there are ways to keep all columns, and I see that @PGStats has provided an alternative, so I won't bother.

--
Paige Miller
SanKH1
Quartz | Level 8
Apologies and noted.
PGStats
Opal | Level 21

If you prefer to use SQL:

 

proc sql;
create table want as
select * from test
group by store
having max(type1)=1 and max(type2)=1;
quit;
PG
SanKH1
Quartz | Level 8
Thanks! Just one question what does max do in this case if you are already saying type1=1?
PGStats
Opal | Level 21

Max(type1)=1 requests stores for which the maximum value of type1 is one, so that it is equal to one for at least one observation.

Note: the code assumes that type1 and type2 only take values 0 or 1.

 

PG
SanKH1
Quartz | Level 8
Thank you!
SanKH1
Quartz | Level 8

I have a follow-up question. Is it possible to create a variable (goal) following the statement "having max(type1)=1 and max(type2)=1" that will be goal=1. Something like if (having statement) then goal=1. I am aware there will only be ones for this newly created variable. But we need this variable so we can then merge with other datasets and then filter rows what will be goal=1.

 

I did this and it worked, but is this the right way?

proc sql;
create table want as
select * , 1 as goal from test
group by store
having max(type1)=1 and max(type2)=1;
quit;

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 354 views
  • 1 like
  • 3 in conversation