Dear all,

Would you be able to advise me on the following?

I have written several checks to flag data inconsistencies (e.g. are there duplicate subject ids in my data set?)

If such a check finds any inconsistencies (at least one observation), I want to export these cases to Excel, so I can share these observations with the colleague that is responsible for collecting and cleaning the data.

Stata will stop running my code if the inconsistency check finds no observations ("Too many or no variables specified"). However, I have about 20 of these inconsistency checks and it is time consuming to tell Stata to continue running the rest of the code every time it does not find any observations in an inconsistency check.

So what I would like is for the code below to run only if there is at least one observation to export and if there are no observations to continue running my dofile instead of stopping and telling me "Too many or no variables specified")

I have tried:

(A):
export excel using "$data/data_inconsistency_check1.xls", replace firstrow(var) if _n > 0
--> Result: Unfortunately export excel does not accept if statements

(B):

foreach i in subjectid {
export excel using "$data/data_consistency_check1.xls", replace firstrow(var)
}
--> Result: this gives me the same "Too many or no variables specified" message when there are no observations to export, and if there would be more than 1 observation, the export would be performed multiple times, so this would not be a suitable solution to be honest.

Thank you kindly for your ideas and suggestions,

Best wishes,

Moniek