## Identify subjects with a skipped dose

Solved
Frequent Contributor
Posts: 76

# Identify subjects with a skipped dose

[ Edited ]

Hi,

Identify the subjects with skipped dose

ID         Date            Dose

001     01Jan2014      10

001     02jan2014       10

001     03jan2014       10

001     05jan2014       10

002     01jan2014       20

002     02jan2014       20

002     03jan2014       20

002     04jan2014       20

003     02jan2014       10

003     03jan2014       10

003     04jan2014       10

003     06jan2014       10

help me in solving this problem.

Accepted Solutions
Solution
‎04-07-2017 04:06 PM
Super User
Posts: 8,216

## Re: Identify subjects with a skipped dose

[ Edited ]

Here is one way:

```data want (drop=counter first_date);
set have;
retain first_date;
by id;
if not (first.id and last.id);
if first.id then do;
counter=0;
first_date=date;
end;
else counter+1;
if last.id then do;
if date-first_date ne counter then output;
end;
run;
```

Art, CEO, AnalystFinder.com

All Replies
Solution
‎04-07-2017 04:06 PM
Super User
Posts: 8,216

## Re: Identify subjects with a skipped dose

[ Edited ]

Here is one way:

```data want (drop=counter first_date);
set have;
retain first_date;
by id;
if not (first.id and last.id);
if first.id then do;
counter=0;
first_date=date;
end;
else counter+1;
if last.id then do;
if date-first_date ne counter then output;
end;
run;
```

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 76

## Re: Identify subjects with a skipped dose

[ Edited ]

Hi  I have done this in following way,correct  me if any thing is wrong

``````proc sort data=test;
by id;
run;

data test_;
set test;
by id;
x=lag(date);
format x date9.;

if first.id then
x=.;

if not first.id then
y=date-x;

if y ne 1 and y ne . then
x1='unscheduled visit';
run;

data final;
set test_;
where x1='unscheduled visit';
drop x y x1;
run;``````
Super User
Posts: 8,216

## Re: Identify subjects with a skipped dose

Both your and my code appear to be doing the same thing, but your method required two passes of the data, while mine only required one pass.

However, unless your dealing with big data, you probably won't care that it will take twice as long.

Regardless, you could have done yours in one pass if rather than using:

`if y ne 1 and y ne . then x1='unscheduled visit';`

simply using

`if y ne 1 and y ne .;`

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 76

## Re: Identify subjects with a skipped dose

what if in this case if we need to find the unscheduled visits?

001     01jan2012      10

001     08jan2012       20

001     20jan2012       30

001     27jan2012       40

001     02jan2012       99

001     03jan2012       99

002     02jan2012      10

002     05jan2012      20

002     03jan2012      99

Super User
Posts: 8,216

## Re: Identify subjects with a skipped dose

I presume that you mean how do you identify the skipped dossages. Regardless, if I were you I'd wouldn't ask it in this thread but, rather, submit the question as a new thread/question. You will likely get more responses if people have a chance to be recognized as the solution provider.

Art, CEO, AnalystFinder.com

Super User
Posts: 6,933

## Re: Identify subjects with a skipped dose

Does skipped mean they missed a day?

If the first dose is January 2, does that mean that they skipped January 1?

Here's one possibility:

data want;

set have;

by id;

days_skipped = dif(date) * (first.id=0);

run;

It assumes that your data is already sorted by ID, and that your DATE variable contains valid SAS dates.

Frequent Contributor
Posts: 76

## Re: Identify subjects with a skipped dose

Posted in reply to Astounding

cud u pls explain me what does the following statement do

days_skipped = dif(date) * (first.id=0);

Super User
Posts: 8,216

## Re: Identify subjects with a skipped dose

dif(date) takes the difference between the value of date and the value of date on the previous record.

first.id will equal 1 for the first id and 0 for all other records within that id, so first.id=0 will have a value of 1 for each record except the first.id record where it will have a value of 0.

so multiplying the two will product the number of days since the last visit for each record except the first record for each id. The first record for each ID will get a value of 0.

Art, CEO, AnalystFinder.com

p.s. Have you posted your new question in a new thread yet?

Frequent Contributor
Posts: 76

## Re: Identify subjects with a skipped dose

Yes I have posted

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 9 replies
• 204 views
• 1 like
• 3 in conversation