Hello there,
I have a dataset like
subjid visit
101 1
101 2
101 3
101 4
101 5
101 6
102 1
102 5
102 6
I would like to flag the subject like 102 that has consecutive 3 visit missing and delete that subject from my study. Can someone share the logic of how to do that? Thanks for all your time and help.
Given the assumption that your data has already been sorted by subjid, visit, a classic 2XDOW will do: (of course there are other approaches as well)
data have;
input subjid visit;
cards;
101 1
101 2
101 3
101 4
101 5
101 6
102 1
102 5
102 6
;
data want;
do until(last.subjid);
set have;
by subjid;
if dif(visit) >= 3 then _flag=1;
end;
do until(last.subjid);
set have;
by subjid;
if _flag ne 1 then output;
end;
drop _flag;
run;
Haikuo
What do you want the output to look like?
I would only like to output only those subjects that has come to all six visits without missing consecutive 3 in a row.
Can a person have more than one visit? e.g 2 visit 2's? If not then a quick SQL step would work:
proc sql;
create table want as
select *
from have
group by subjid
having count(subjid)=6;
quit;
Here's a single data step technique that works without the double DOW. IWhenever a new subjid starts, it compares the id to the subjid 5 records ahead. If they match, keep the subjid. This assumes that there cannot be more than one record per visit:
data have;
input subjid visit @@;
datalines;
101 1 101 2 101 3 101 4 101 5 101 6
102 1 102 5 102 6
run;
data want (drop=_:);
set have ;
by subjid;
if eod=0 then set have (firstobs=6 keep=subjid rename=(subjid=_subjid6)) end=eod;
else _subjid6=.;
retain _flag;
if first.subjid=1 then _flag=(subjid=_subjid6);
if _flag=1;
run;
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!
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.