I suppose you are considering a study with sample sizes large enough
that it is appropriate to use an approximate normal test.
Suppose you want to compare two populations $(1$ and $2)$ with Success probabilities $p_1, p_2$ and sample sizes $n_1, n_2,$ respectively
and that you want to be reasonably sure to reject $H_0: p_1 - p_2 = 0$
at the 5% level against a two-sided alternative, if $|p_1 - p_2| > .04.$
Suppose you have data as below:
set.seed(1234)
n1 = n2 = 3500
p1 = .60; p2 = .64
x1 = rbinom(1, n1, p1); x1
[1] 2128
x2 = rbinom(1, n2, p2); x2
[1] 2227
prop.test(c(x1,x2), c(n1,n2), cor=F)$p.val
[1] 0.01466729
So sample sizes of 4000 were sufficient to reject the null hypothesis
in this particular case. The full output (not just the P-value) is shown
below. I declined continuity correction on account of the large sample sizes.
prop.test(c(x1,x2), c(n1,n2), cor=F)
2-sample test for equality of proportions
without continuity correction
data: c(x1, x2) out of c(n1, n2)
X-squared = 5.956, df = 1, p-value = 0.01467
alternative hypothesis: two.sided
95 percent confidence interval:
-0.050992366 -0.005579062
sample estimates:
prop 1 prop 2
0.6080000 0.6362857
Now the question is whether this particular dataset is typical
or just lucky by chance. We can do a simulation to approximate
the power (probability $H_0$ is rejected) in such experiments.
set.seed(2021)
n1 = n2 = 3500; p1 = .6; p2 = .64
pv=replicate(10^4, prop.test( c(rbinom(1,n1,p1),rbinom(1,n2,p2)),
c(n1,n2),cor=F)$p.val)
mean(pv <= .05)
[1] 0.9324
The power of prop.test
for the specified parameters is about $93\%,$
So with samples of size 3500 you have a good chance that the test
will find a real difference between success probabilities 0.60 and 0.64.
[Note: Differences of 0.04 between $p_1$ and $p_2$ are somewhat easier to detect near 0 and 1 (say 0.10 vs. 0.14) and harder to detect near 0.5 (say 0.48 vs. 0.52). Simulation takes this into account, an approximate formula for sample size may or may not do so.]
set.seed(2021)
n1 = n2 = 3500; p1 = .1; p2 = .14
pv=replicate(10^4, prop.test( c(rbinom(1,n1,p1),rbinom(1,n2,p2)),
c(n1,n2),cor=F)$p.val)
mean(pv <= .05)
[1] 0.9992
set.seed(2021)
n1 = n2 = 3500; p1 = .48; p2 = .52
pv=replicate(10^4, prop.test( c(rbinom(1,n1,p1),rbinom(1,n2,p2)),
c(n1,n2),cor=F)$p.val)
mean(pv <= .05)
[1] 0.9137
When you have a useful formula for computing sample size, you can plug in the
values I used above to see if you get about the same answer I got by simulation.
Alternatively, you can forget about finding a formula for $n$ and change
the parameters in my simulation to find the sample size that matches
your situation.
There are some online 'power and sample size' calculators on the Internet.
If you want to explore this, try to find one sponsored by a government agency or the statistics department of a major university.