/* Input the data                                     */ 
data sample;                                                
   input id time conc lasttime lastconc cmax tmax weight @@;
   lconc=log(conc);                                         
   cards;                                                   
1  0.0  0.00 8.0 0.54 3.50 4.0 0                            
1  0.5  0.00 8.0 0.54 3.50 4.0 0                            
1  1.0  1.15 8.0 0.54 3.50 4.0 0                            
1  1.5  1.98 8.0 0.54 3.50 4.0 0                            
1  2.0  2.82 8.0 0.54 3.50 4.0 0                            
1  3.0  3.43 8.0 0.54 3.50 4.0 0                            
1  4.0  3.50 8.0 0.54 3.50 4.0 0                            
1  5.0  2.79 8.0 0.54 3.50 4.0 1                            
1  6.0  1.64 8.0 0.54 3.50 4.0 1                            
1  7.0  0.93 8.0 0.54 3.50 4.0 1                            
1  8.0  0.54 8.0 0.54 3.50 4.0 1                            
2  0.0  0.00 8.0 0.57 3.53 3.0 0                            
2  0.5  0.00 8.0 0.57 3.53 3.0 0                            
2  1.0  1.20 8.0 0.57 3.53 3.0 0                            
2  1.5  2.01 8.0 0.57 3.53 3.0 0                            
2  2.0  2.92 8.0 0.57 3.53 3.0 0                            
2  3.0  3.53 8.0 0.57 3.53 3.0 0                            
2  4.0  3.51 8.0 0.57 3.53 3.0 0                            
2  5.0  2.81 8.0 0.57 3.53 3.0 1                            
2  6.0  1.69 8.0 0.57 3.53 3.0 1                            
2  7.0  1.00 8.0 0.57 3.53 3.0 1                            
2  8.0  0.57 8.0 0.57 3.53 3.0 1                            
;                                                           
                                                            
   /* Print the data set SAMPLE                           */
proc print data=sample noobs;                               
run;                                                        
                                                            
       /* PROC MODEL code to run regression and calculate */
       /* pharmacokinetic and regression parameters       */
       /* by patient identifier ID                        */
proc model data=sample;                                     
   by id;                                                   
   endogenous lconc;                                        
   exogenous  time;                                         
   parms int k;                                             
   lconc=int-k*time;
   pred=.;                                                  
   if weight=1 or time=lasttime then pred=exp(pred.lconc);  
   h=time*conc;                                             
                                                            
      /* Linear trapezoidal rule                          */
   if time le tmax then do;                                 
      auc=zlag(auc)+(time-zlag(time))*(conc+zlag(conc))/2;  
      aumc=zlag(aumc)+(time-zlag(time))*(h+zlag(h))/2;      
   end;                                                     

      /* Log trapezoidal rule                             */
   if time gt tmax then do;                                 
                                                            
      /* Make correction to prevent zero divide under     */
      /* log trap method. This converts the calculation   */
      /* to a linear trap method.                         */
      if h=lag(h) or conc=lag(conc) then do;                
         auc=zlag(auc)+(time-lag(time))*(conc+lag(conc))/2; 
         aumc=zlag(aumc)+(time-lag(time))*(h+lag(h))/2;     
      end;                                                  
      else do;                                              
                                                            
         /* Log trapezoidal rule calculation              */
         auc=zlag(auc)+(time-lag(time))*(lag(conc)-conc)/   
         (log(lag(conc) /conc)                              
         aumc=zlag(aumc)+(time-lag(time))*(lag(h)-h)/       
         (log(lag(h)/h));                                   
                                                            
       end;                                                 
   end;  

      /* Half-life calculation                            */
   k_hl=log(2)/k;                                           
                                                            
      /* Estimate the equation                            */
   fit lconc/out=outmod(drop=_estype_ _type_ _weight_)      
             outactual;                                     
                                                            
      /* Specify created variables to output              */
   outvars pred auc aumc k k_hl;                            
                                                            
      /* Specify input data set variables to output       */
   id conc weight tmax cmax;                                
   weight weight;                                           
run;                                                        
quit;                                                       
                                                            
   /* Print pharmacokinetic results                       */
proc print data=outmod noobs uniform;                       
run;