Ok, from business logic point, if the value in question is in one of the periods in prior year (and you don't know which one), then last year's M12 V#YTD should be greater than zero and should be equal to said value. Does it make sense?
Notes for the rest:
- As in was already said, you never set initial value to integer as "", it should be 0
- As it was already said, your loop is virtually infinite, so don't do while, limit it one way or another. You can run for loop, say, for set number of iterations and make your while condition an if condition, if it comes to it.
- You do not want to use regular divide sign "/" in your calculations. use OS DIVIDE() function, that mitigates division by zero.