Dear Statalist,

I have a dataset with duplicate observations (marked by duplicateID). I want to check if both rows of each duplicate are identical and export the results to Excel if they are not. This works fine as long as there are differences for each variable, but fails when there are no differences for a variable since the export excel command needs at least one observation to export to work. ("r(198) observations must be between 1 and 1048576")

I tried adding an if condition before the export excel command, but Stata then skips the command. I guess the condition is not satisfied, but I am not quite sure why. I am sure the reason is quite trivial and any help would be greatly appreciated.

Code:
 
*The data I have

input duplicateID str10 VarA VarB
 20 "Mark Y." 1994
 20 "Mark Y." 1994
 30 "Suzanne F." 1990
 30 "Suzanne F." 1991
 end
 list
 
*This Code works fine for all variables that have at least one difference, but fails if a variable is identical for all observations

foreach v of varlist _all{
 by ID (`v'), sort: gen diff`v' = `v'[1] != `v'[_N] 
 list ID `v' if diff`v'
 export excel ID `v' if diff`v' using Differences.xlsx, firstrow(var) sheet("`var'") sheetreplace
 drop diff`v'
}

*I tried adding this if condition before the export excel command, but then the section is just not executed

foreach v of varlist _all{
 by ID (`v'), sort: gen diff`v' = `v'[1] != `v'[_N] 
 list ID `v' if diff`v'
 if diff`v' == 1{
 export excel ID `v' if diff`v' using Differences.xlsx, firstrow(var) sheet("`var'") sheetreplace
 }
 drop diff`v'
}