I am trying to write a linear constraint that computes the absolute value of a difference, only if both the variables $x$ and $y$ are different from zero.

$x,y$ are binary variables while $s$ is a positive integer variable. $a$ and $b$ are positive integer coefficients.

$|xa-yb| leq s$ only if $x neq 0$ and $y neq 0$

for the first part I did:

$(xa-yb) leq s$ and $-(xa-yb) leq s$

but I don’t know how to proceed with the second part, if that is ever possible.

Thank you in advance

You can change $dots le s$ to $dots le s + M(2 – x – y)$ where $M$ is an a priori upper bound on $|xa-yb|$. If either or both of the binary variables is 0, the inequality will not be restrictive.

You can enforce

$$(x = 1 land y = 1) implies |a-b| le s$$

with linear constraint

$$|a-b|(x + y – 1) le s.$$

Do you instead (or also) want to enforce

$$|a-b| le s implies (x = 1 land y = 1),$$

which is the converse?