For several countries, I have one dataset and I want to keep a list of variables (specified in a global vlist) for each of them. Some variables do not exist in some countries.

I want Stata to ignore the non-existence of these variables and execute the keep command for the rest of the variables. However, the problem is the additional for loop on the country-level, that I'm struggling to incorporate.

This solution may appear counter-intuitive at first but it is equivalent to the one above. The difference is that the inner loop is substituted with macro list manipulation. See here for more information.
– Pearly SpencerMay 14 '18 at 14:31

Thanks for the plus, but what is counter-intuitive here, e.g. puzzling or backwards? The syntax may be novel to a reader, but it's an implementation of "I need to identify the variables that actually exist from a list of those wanted"
– Nick CoxMay 14 '18 at 14:56

There is nothing wrong with the syntax you propose. If anything, it is more clean. But i think beginners find it easier to think in terms of loops than macro list manipulations. I have seen this several times before.
– Pearly SpencerMay 14 '18 at 15:02

I see. That could well be, depending on their previous programming experience. I often see people programming unnecessary loops over observations.
– Nick CoxMay 14 '18 at 15:09