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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1088 views
  • 1 like
  • 3 in conversation