I have data like below.
id----Visit --------date------- response
01 Base 02APR2019 N
01 Vis 1 18APR2019 N
01 Vis 1 20APR2019 N
01 Vis 2 04MAY2019 Y
01 Vis 3 16MAY2019 N
Want :
1. if next record date difference date <=10 days then I want delete the records regardless of visit name.
2. I want per id until Y response. from Y I dont want records. In this I dont want Vis 3 record from above data.
Please help.
id----Visit --------date------- response
01 Base 02APR2019 N
01 Vis 1 18APR2019 N
01 Vis 1 20APR2019 N
01 Vis 2 04MAY2019 Y
01 Vis 3 16MAY2019 N
3rd and 5th records I want to remove.
3rd - since difference with previous visit is just 2 days. I want more than 10 days gap between visits.
5th - Response is N after Y response. I want to remove all records which occured after Y response for particular subject.
So in total, which records should be deleted here?
id----Visit --------date------- response
01 Base 02APR2019 N
01 Vis 1 18APR2019 N
01 Vis 1 20APR2019 N
01 Vis 2 04MAY2019 Y
01 Vis 3 16MAY2019 N
3rd and 5th records I want to remove.
3rd - since difference with previous visit is just 2 days. I want more than 10 days gap between visits.
5th - Response is N after Y response. I want to remove all records which occured after Y response for particular subject.
data want;
set have;
retain response_flag;
prev_date = lag(date);
response_flag = 0;
if date - prev_date < 10 then delete;
if response_flag = 1 then delete;
if response = 'Y' and response_flag = 0 then response_flag=1;
run;
@vandhan wrote:
id----Visit --------date------- response
01 Base 02APR2019 N
01 Vis 1 18APR2019 N
01 Vis 1 20APR2019 N
01 Vis 2 04MAY2019 Y
01 Vis 3 16MAY2019 N
3rd and 5th records I want to remove.
3rd - since difference with previous visit is just 2 days. I want more than 10 days gap between visits.
5th - Response is N after Y response. I want to remove all records which occured after Y response for particular subject.
what if the RESPONSE='Y' record appears on an observation that your rule says to delete?
Hello @vandhan
data have;
input id Visit & $10. date :date9. response $;
format date date9.;
cards;
01 Base 02APR2019 N
01 Vis 1 18APR2019 N
01 Vis 1 20APR2019 N
01 Vis 2 04MAY2019 Y
01 Vis 3 16MAY2019 N
;
data want ;
do _n_=1 by 1 until(last.id);
set have;
by id;
if (dif(date)>10 or _n_=1) and not _k then output;
if response='Y' then _k=1;
end;
drop _:;
run;
Notes:
(dif(date)>10 or _n_=1) can also be written as (dif(date)>10 or first.id)
"It's a bad idea to run code you don't understand..."
@Reeza That's Bingo!
Reminds me of my dire situation back in Sep-oct 2012, hmm My very first basic exposure to SAS and didn't even understand what's DATA and SET statement. Jeez!, Believe it or not, was sitting at work. Used my desk phone to call my mom. Give her the HAVE and WANT. Mom used som crazy proc sql to avoid hoops of arrays and loops but compelled to use loops at times. Boss, holding me on the fire. Didn't understand the solutions nor the HAVE/WANT and finally However , I quit within 3 months and fell into depression.
"Your mom is a SAS programmer?" - No. She was forced to learn for my sake as I was struggling. So what i learned in 6 years, she did in half the time. We started together. lol
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.