Look carefully at your logic around this comparison:
&validToDate="01JAN5999"
If the variable that is used to store into the macro variable VALIDTODATE is a date value the result will depend of the format attached. So it could be a string of digits or the string 01JAN5999 or something else if a different format is attached. But it will never have quotes around it like your test is looking for.
You might want to just add the quotes to both sides of the comparison.
"&validToDate"="01JAN5999"