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

Hi guys,
I hope you can help with this (please check "Data set" below):

I have subjects that can express different Conditions (Con1-Con4, as given). I can group/order the subjects (column Order) based on time points/dates (not shown), subjects express one of mentioned conditions at certain point of time/order number.

What I am interested here is to select only those subject that have Con4 when previous condition (for the same subject) was con3 - for this data set example I would need only need to filter Subj3 and listed conditions (last condition was Con4 and previous was Con3) while Subj1 does not satisfy that criteria (it has the last condition Con4 but previous one/time point was Con2).

 

SubjectOrderCondition
Subj11Con1
Subj12Con2
Subj13Con2
Subj14Con4
Subj21Con2
Subj22Con4
Subj31Con2
Subj32Con3
Subj33Con4

 

I hope I didn't made it too messy in my explanation... 🙂 

Thank you very much for your time. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
HB
Barite | Level 11 HB
Barite | Level 11
data have;
   input subject $5. order 2. condition $5.;
datalines;
Subj1 1 Con1
Subj1 2 Con2
Subj1 3 Con2
Subj1 4 Con4
Subj2 1 Con2
Subj2 2 Con4
Subj3 3 Con4
Subj3 1 Con2
Subj3 2 Con3
;
run;

proc sort data = have;
	by subject order;
run;

data want;
	set have;
	prev_cond = lag(condition);
	if prev_cond = "Con3" and condition = "Con4";
run;

 

Output is 

subject order condition prev_cond
Subj3 3 Con4 Con3

 

 

Does something like this move you in the right direction?

 

 

Edit:  Because it is what I do, here is a possible Proc SQL solution

 

proc sql;
	select a.subject, a.order, a.condition, b.condition
	from have a left join have b
	on a.subject = b.subject and a.order -1 = b.order
	where a.condition = 'Con4' and b.condition = 'Con3';
quit; 

 

 

View solution in original post

2 REPLIES 2
HB
Barite | Level 11 HB
Barite | Level 11
data have;
   input subject $5. order 2. condition $5.;
datalines;
Subj1 1 Con1
Subj1 2 Con2
Subj1 3 Con2
Subj1 4 Con4
Subj2 1 Con2
Subj2 2 Con4
Subj3 3 Con4
Subj3 1 Con2
Subj3 2 Con3
;
run;

proc sort data = have;
	by subject order;
run;

data want;
	set have;
	prev_cond = lag(condition);
	if prev_cond = "Con3" and condition = "Con4";
run;

 

Output is 

subject order condition prev_cond
Subj3 3 Con4 Con3

 

 

Does something like this move you in the right direction?

 

 

Edit:  Because it is what I do, here is a possible Proc SQL solution

 

proc sql;
	select a.subject, a.order, a.condition, b.condition
	from have a left join have b
	on a.subject = b.subject and a.order -1 = b.order
	where a.condition = 'Con4' and b.condition = 'Con3';
quit; 

 

 

Batta
Obsidian | Level 7
Thank you very much!
This is very helpful.

Best
Batta

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 480 views
  • 2 likes
  • 2 in conversation