Calcite | Level 5

## Calculation for days of drug usage for one year, but not exceed 120 days between two prescription

Dear all,

I want to calculate the regular use of drug. The defination is that regular users was the regular use of drug for "more than a year" and "the interval between successive prescription drug records cannot exceed 120 days".

I wish the the data was presented as "Category (Correct)".

When the date between two prescriptions were within 120 days (Dif_Days), they belonged the same group.

If the date long than 120 days (Dif_Days), the next prescriptions was categorized as new group.

At one of steps, I cannot run the the correct category. How cou;d I modify the syntax?

DATA Final;
SET data;
by ID Drug_start
if first.ID then Category=1;
if Dif_Days<=120 then Category=1;
else Category+1;
run;

The data was shown as below.

Drug_start: the first date for this prescription

Drug_end:  the end date for this prescription

Drug_day: how long for this prescription

Dif_Days: the difference between the Drug_start and last Drug_end

 ID Drug_start Drug_end Drug_day Dif_Days Category Category (Correct) AAA 20091011 20091109 30 1 1 AAA 20091109 20091208 30 0 1 1 AAA 20110317 20110415 30 464 2 2 AAA 20110523 20110621 30 38 1 2 AAA 20110810 20110908 30 50 1 2 AAA 20110909 20111008 30 1 1 2 AAA 20111005 20111103 30 -3 1 2 AAA 20130217 20130318 30 472 2 3 AAA 20130313 20130411 30 -5 1 3 BBB 1 1 BBB 1 1 CCC 20080605 20080618 14 1 1 CCC 20100311 20100317 7 631 2 2 CCC 20100318 20100331 14 1 1 2 CCC 20110616 20110713 28 442 2 3 CCC 20111124 20111221 28 134 3 4 CCC 20121115 20121212 28 330 4 5 CCC 20121207 20130103 28 -5 1 5 CCC 20130110 20130206 28 7 1 5 CCC 20130418 20130515 28 71 1 5 CCC 20130530 20130626 28 15 1 5 CCC 20130706 20130802 28 10 1 5 CCC 20130727 20130823 28 -6 1 5
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Calculation for days of drug usage for one year, but not exceed 120 days between two prescriptio

I don't understand.

CODE NOT TEST.

DATA Final;
SET data;
by ID ;
if first.ID then Category=0;
if Dif_Days>120 or first.ID then Category+1;
run;

3 REPLIES 3
Super User

## Re: Calculation for days of drug usage for one year, but not exceed 120 days between two prescriptio

First thing: Are your date variables SAS date valued and currently assigned a format of yymmdd8., character or numeric as shown? If they are not SAS you will want to create SAS date values. The approach differs depending on the variable type of your current values.

Then the value of Dif_days can be calculated in a datasetp as:

Dif_days = SASstartdate - lag(SASenddate);  (or do the conversion on the fly with the Lag value of date_end)

You will want to NOT calculate the dif_days this way for the first.ID

Super User

## Re: Calculation for days of drug usage for one year, but not exceed 120 days between two prescriptio

I don't understand.

CODE NOT TEST.

DATA Final;
SET data;
by ID ;
if first.ID then Category=0;
if Dif_Days>120 or first.ID then Category+1;
run;

Calcite | Level 5

## Re: Calculation for days of drug usage for one year, but not exceed 120 days between two prescriptio

Thanks at lot.

It works.

But it seems need to be sort by ID and Drug_start.

DATA Final;
SET data;
by ID Drug_start;
if first.ID then Category=0;
if Dif_Days>120 or first.ID then Category+1;
run;

Discussion stats
• 3 replies
• 720 views
• 0 likes
• 3 in conversation