SAS INTNX Function for Missing Date

Background Story:

Missing dates are something the pharmaceutical industry deals with on a daily basis.  There are some common methods.

1. Completely missing year, month, and day then no imputation.
2. Set a certain number( ex.Jan01) for missing month and day
3. Compare with treatment start date, and manipulate accordingly.
If the AE start date and the treatment start date have the same year and month then we set the missing AE start date the same as the treatment start date.
if the AE start date and the treatment start date have the same year, the AE start month is later than the treatment start month. we set the AE start date to be the first day of the month(xx-01).
if the AE start date is earlier than the treatment starting month. We set AE start day to be the last day of the month(xx-31/30/29/28)

As we can see, the 3rd method is the best model, however, it is a little complicated to implement it. Because we have to consider different year and month when it comes to the last day of the month.

Luckily we have INTNX function.

```intnx("time scale",variable, relation, "time position");
a=intnx("month", trtsdt, 1, "beginning");
* variable a = the beginning of next month of trtsdt.
we can use 1,2,3,4 ... to present the number we want to add on "time scale"
```

Goal: Imputate Partial Missing AE date.

Key SAS Function: INTNX, Year, Month, Input

Sample Code:

```data ae_date;
set a;
if length(compress(aestdtc))=10 then aestdt=input(aestdtc,yymmdd10.);
*Missing day;
if aestdtc ne " " and trtsdt> . and aestdt=. and amonth=tmonth and ayear=tyear
and length(compress(aestdtc))=7
then aestdt=trtsdt;

if aestdtc ne "" and trtsdt>. and aestdt=. and length(compress(aestdtc))=7 then do;
sampleae=input(substr(aestdtc,1,7)||"-01",yymmdd10.);
if (ayear>tyear) or (ayear=tyear and amonth>tmonth) then aestdt=intnx("month",sampleae,0,"end");
else if (ayear<tyear) or (ayear=tyear and amonth<tmonth) then astdt=intnx("month",sampleae,0,"beginning");
end;

*Missing Month and Day;
if aestdtc ne " " and trtsdt> . and aestdt=. and ayear=tyear
and length(compress(aestdtc))=4
then aestdt=trtsdt;
if aestdtc ne "" and trtsdt>. and aestdt=. and length(compress(aestdtc))=4 then do;
if (ayear<tyear) then aestdt=input(substr(aestdtc,1,4)||"-12-31",yymmdd10.);
else if ayear>tyear then aestdt=input(substr(aestdtc,1,4)||"-01-01",yymmdd10.);
end;
run;```

Sample Output:

General INTNX sample:

```format day week month_ year date9.;
day=intnx('day', '01FEB2010'd, 7); /* +7 days */
week=intnx('week', '01FEB2010'd, 1); /* 01 of Feb 2010 is Monday*/
month_=intnx('month', '01FEB2010'd, 2); /* +2 month */
year=intnx('year', '01FEB2010'd, 1); /* +1 year */

format day week year date9.;
day=intnx('day2', '01FEB2010'd, 2); /* two of 2-days intervals */
week=intnx('week1.3', '01FEB2010'd, 1); /* 01 of Feb 2010 is Sunday. 3rd day of the week is Tuesday */
year=intnx('year1.3', '01FEB2010'd, 1); /* next year | third month | 1st day*/

format seconds minutes hours days weeks datetime20.;
format t_seconds t_minutes t_hours time9.;
seconds=intnx('second', '01FEB2010:00:00:00'dt, 1);
minutes=intnx('minute', '01FEB2010:00:00:00'dt, 1);
hours=intnx('hour', '01FEB2010:00:00:00'dt, 1);
days=intnx('dtDay', '01FEB2010:00:00:00'dt, 1);
weeks=intnx('dtWeek', '01FEB2010:00:00:00'dt, 1);

t_seconds=intnx('second', '00:00:00't, 1);
t_minutes=intnx('minute', '00:00:00't, 1);
t_hours=intnx('hour', '00:00:00't, 1);```

Reference:

https://communities.sas.com/t5/SAS-Communities-Library/INTNX-and-INTCK-Function-Examples/ta-p/475168

Happy Studying!

This site uses Akismet to reduce spam. Learn how your comment data is processed.