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

Hi, 

I tried to screen each observation in my data set and find out the participants (same ID) that meet a standard to generate a new dataset. The standard is variable 1 = 6 or 7. So as long as the participant has one observation meeting the standard, all the observations of that participant will keep in the new dataset. Can you help me with how to do it?  Thanks.

 

The original data looks like:

ID.       variable 1.   variable2-20

1.              1        .......

1               3        .......

2                7        .......

3                 5        .......

3                 3        .......

3                  7          .......

...                ...           .......

 

the new dataset should look like:

ID.       variable 1.   variable2-20

2                7        .......

3                 5        .......

3                 3        .......

3                  7          .......

...                ...           .......

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Still SQL

 

proc sql; 
create table want as
select *
from have
where ID in (select distinct ID from have where variable1 in (6,7));
quit;

View solution in original post

6 REPLIES 6
ballardw
Super User

One way might be

 

proc sql;
   create table want as 
   select b.* 
   from (select distinct id from have
         where variable1 in (6,7) ) as a
        left join
        have as b
        on a.id = b.id
   ;
quit;
knighsson
Obsidian | Level 7
Thank you so much! This is really helpful!

I have a follow-up question: if the standard is that variable1 =1-5 and 6-7, which means each participant in "want" at least has (one observation that its variable1 is 1or 2 or 3 or 4 or 5) AND (another observation that its variable 1= 6 or 7)

How should I change the code?
Thank you!
Reeza
Super User

Still SQL

 

proc sql; 
create table want as
select *
from have
where ID in (select distinct ID from have where variable1 in (6,7));
quit;
knighsson
Obsidian | Level 7
Thank you so much! This is very helpful!

I have a follow-up question: if the standard is that variable1 =1-5 and 6-7, which means each participant in "want" at least has (one observation that its variable1 is 1or 2 or 3 or 4 or 5) AND (another observation that its variable 1= 6 or 7)

How should I change the code?
Thank you!
ed_sas_member
Meteorite | Level 14

Hi @knighsson 

 

You can adapt the code as follows (multiple merging):

proc sql;
	create table want as
	select c.* 
	from ((select distinct id
				 from have
				 where variable1 in (1,2,3,4,5)) as a
         inner join
		 (select distinct id
		 		 from have
				 where variable1 in (6,7)) as b
         on a.id = b.id)
		 left join
		 have as c
		 on a.id = c.id
   ;
quit;

Hope this helps!

Best,

knighsson
Obsidian | Level 7
Thank you!This is very helpful!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 6 replies
  • 842 views
  • 3 likes
  • 4 in conversation