BookmarkSubscribeRSS Feed
Statsconsultancy
Fluorite | Level 6
My data set has the following form:

subject treatment dateprescribed
1 A 08/08/1997
1 A 09/12/1998
1 A 12/01/1999
1 B 07/09/2000
1 B 05/09/2001
1 A 05/06/2002
1 A 06/10/2002
1 A 09/11/2003
2 A 08/03/1996
2 A 09/12/1998
2 B 12/01/1999
2 B 07/09/2000
2 B 05/09/2001
2 A 05/06/2002
2 A 06/10/2002
2 B 09/11/2003

I want to delete the row if the treatment in that row is the same as the
previous treatment; Thus I want to obtain the following:
subject treatment dateprescribed
1 A 08/08/1997
1 B 07/09/2000
1 A 05/06/2002
2 A 08/03/1996
2 B 12/01/1999
2 A 05/06/2002
2 B 09/11/2003

Thus patient 1 received the treaments in the order ABA, while patient 2 received the treatments in the order ABAB . Can someone help me on how to do that in SAS.

Best wishes
4 REPLIES 4
Flip
Fluorite | Level 6
Sort by subject treatment dateprescribed.

data xxx;
by subject treatment dateprescribed;
if first.treatmant;
run;
data_null__
Jade | Level 19
> Sort by subject treatment dateprescribed.
>
> data xxx;
> by subject treatment dateprescribed;
> if first.treatmant;
> run;

This wont work because the data need to be ordered by date, not treatment. But, the FIRSTing needs to be done by TREATMENT. The NOTSORTED by statement option is the key.


[pre]
data rx;
input (subject treatment dateprescribed)(2*:$1. :mmddyy.);
format date: mmddyy10.;
cards;
1 A 06/10/2002 I mixed up the lines a bit
1 A 09/11/2003
1 A 08/08/1997
1 B 07/09/2000
1 B 05/09/2001
1 A 05/06/2002
1 A 09/12/1998
1 A 12/01/1999
2 A 08/03/1996
2 A 09/12/1998
2 B 12/01/1999
2 B 07/09/2000
2 B 05/09/2001
2 A 05/06/2002
2 A 06/10/2002
2 B 09/11/2003
;;;;
run;
proc sort;
by s: d:;
run;
proc print;
by s: t: notsorted;
id s: t:;
var d:;
run;

data firstInSequence;
set;
by s: t: notsorted;
if first.treatment;
run;
proc print;
by s:;
id s:;
run;
[/pre]
SUN59338
Obsidian | Level 7
May also;

data result;
set old;
if subject = lag(subject) and treatment = lag(treatment) then delete;
run;
ballardw
Super User
Or this may work:

Proc Sort data=old out=new noduprecs; by x y z;run;

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!

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.

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
  • 4 replies
  • 1034 views
  • 0 likes
  • 5 in conversation