diff --git a/R/RunModel_Lag.R b/R/RunModel_Lag.R
index 78c997f78339637ab336ffda9ecaef5603427826..d0f8b04c0cb37b4f15c1774542afa5614995db31 100644
--- a/R/RunModel_Lag.R
+++ b/R/RunModel_Lag.R
@@ -82,9 +82,10 @@ RunModel_Lag <- function(InputsModel, RunOptions, Param) {
   # Convert back Qsim to mm
   OutputsModel$Qsim <- OutputsModel$Qsim / sum(InputsModel$BasinAreas, na.rm = TRUE) / 1e3
 
-  if("StateEnd" %in% RunOptions$Outputs_Sim) {
+  if ("StateEnd" %in% RunOptions$Outputs_Sim) {
     OutputsModel$StateEnd$SD <- lapply(seq(NbUpBasins), function(x) {
-      Qupstream[(LengthTs - floor(PT[upstream_basin])):LengthTs]})
+      Qupstream[(LengthTs - floor(PT[x])):LengthTs]
+    })
   }
 
   return(OutputsModel)