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

Hi all,

 

I have a problem about how select rows of what I want:

for example, I have several subjects with several variables:

 

group id timepoint

TX      01     1

TX      01     2

TX      02     1

TX      03     1

TX      04     2

CT      05     1

CT      06     2

 

I want to select id(s) in TX group but have timepoint 1 and 2 simultaneously. That means I want to delete rows with highlight green color. Anyone has a good idea if I want to use proc sql? Or other ways are okay.

 

Many thanks,

Chen

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Sure :

 

data have;
input group :$2.   id    timepoint;
datalines;
TX       01     1
TX       01     2
TX       02     1
TX       03     1
TX       03     2
CT       04     1
CT       04     2
CT       05     1
;

proc sql;
create table want as
select *
from have
where group="TX"
group by group, id
having max(timepoint=1) and max(timepoint=2)
order by group, id, timepoint;
select * from want;
quit;
PG

View solution in original post

6 REPLIES 6
Reeza
Super User

Is it only 1 & 2 or could you have duplicates or 1/2/3?

 

If it's only 1 & 2 then this works:

 

data want;
set have;

by group id; *may need to sort your data first;

if not (first.id and last.id); *if unique then a record is first and last at same time;

run;
echoli
Obsidian | Level 7

Hi Reeza,

 

Thanks so much for your help! It works!!

But can I have other ways, like proc sql, to solve this question?

 

Many thanks,

Chen

PGStats
Opal | Level 21

It is not clear to me why you keep id=03 and id=04. Id=03 only has timepoint=1 and id=04 only has timepoint=2. Neither has timepoints 1 and 2 simultaneously. What am I missing?

PG
echoli
Obsidian | Level 7

Hi PG,

 

Very sorry that I made mistakes in my example, the correct example should be:

 

group   id    timepoint

TX       01     1

TX       01     2

TX       02     1

TX       03     1

TX       03     2

CT       04     1

CT       04     2

CT       05     1

 

Highlight color in green are the rows not be selected.

Reeza has provide me a good way use data; set; But can i do this by proc sql?

 

Thanks so much for your reply!!!

Chen

PGStats
Opal | Level 21

Sure :

 

data have;
input group :$2.   id    timepoint;
datalines;
TX       01     1
TX       01     2
TX       02     1
TX       03     1
TX       03     2
CT       04     1
CT       04     2
CT       05     1
;

proc sql;
create table want as
select *
from have
where group="TX"
group by group, id
having max(timepoint=1) and max(timepoint=2)
order by group, id, timepoint;
select * from want;
quit;
PG
echoli
Obsidian | Level 7

Hi PG,

 

That works!!! thanks so much for your help!!!

 

Chen

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1442 views
  • 1 like
  • 3 in conversation