Background Story:

In ADLB avisitn means analysis visit number sequence, most of the time it is a standard variable, such that each Visit or Visname maps to a corresponding number. However, we had a study with convoluted raw data, in which multiple visits were recorded on the same day or many additional visits. Today we will go over a basic and an advanced example of ADLB.Avisitn.

Raw LB Data

Standard Analysis Visit Number (avisitn):

Key variable: Visname , Avisitn, Day

*Avisitn:

Complex Avisitn:

There are multiple visits in the same day or additional visits in the same cycle.

Key idea: create intermedia variables mindt, newdt, use the function max, and first.subjid for additional cycle number.

Sample Code:

if index(day,”Additional”) >0 then add_day=1;
if index(day,”Additional”) >0 then ord=2;

*Sort the visit without additional day;
proc sort data=lb nodupkey out=r(keep=subjid visname adt day visit avisit);
by subjid visit visname day ;
where add_day^=1;
run;

*Set the reference lab date as adt;

data r1;
format mindt date9.;
set r;
mindt=adt;
rename day=daynf avisit=avisitf;
proc sort; by subjid visit visname day adt;
run;

data lb1a;
merge lb1(in=a) r1(in=b);
if a ;
by subjid visit visname day;
format newdt date9.;
newdt=coalesce(mindt,adt);
run;

proc sort data=lb1a;
by subjid visit visname newdt add_day dayn;
run;

data lb1a(drop=avisitn);
set lb1a;
if add_day=1 then avisitn=.;
avisitn1=avisitn;
if index(visname,”Cycle”) > 0 then avis = input(scan(visname,2,’’),8.);
run;

data lb1b;
set lb1a;
by subjid visit visname newdt add_day dayn;
retain avisitn;
if avisitn1^=. or first.subjid then avisitn=avisitn1;
else do;
if add_day=1 and first.dayn then avisitn=max(avisitn, avis)+0.01;
end;
run;

Sample Output:

Note:
Thanks Hongyan for suggesting creating an intermedia variable and code it the universal method. I was trying to code it case by case, which is an unsuccessful try.