First of all, it looks like you are mistaken about how the “if” command works.
It can be used for flow control, but since LAMMPS input is executed immediately when a line is encountered, it cannot be used to trigger actions during a run. So to trigger some action via the “if” command, you would first have to use fix halt
to make a run stop because of a given condition, then execute your conditional command(s) and continue the run.
Please see this post for a bit hackish method for accessing data from a previous time step.
If you want something more sophisticated, you will likely have to resolve to writing a custom fix, either in C++ or in python via fix python/invoke
.