BookmarkSubscribeRSS Feed
DJ09
Calcite | Level 5

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.

5 REPLIES 5
Haikuo
Onyx | Level 15

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

Reeza
Super User

What do you want the output to look like?

DJ09
Calcite | Level 5

I would only like to output only those subjects that has come to all six visits without missing consecutive 3 in a row.

Reeza
Super User

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;

mkeintz
PROC Star

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;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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