Suppose I have a dataset containing two variables X and Y. Without resorting the data, I would like to add 2 flag variables, one indicating whether the value of X has occurred earlier in the dataset and one indicating whether the value of Y has occurred earlier.

What's the best way to do that in Stata?