Dear data cleaners

I made a small program to check whether there is a constant called cons in the data set, and if not, to make it and set its value equal to 1. I use it when I don't know what a program is doing. The system variable _cons doesn't seem to work in all applications (e.g. in collapse). I would like to throw it on SSC so that if I share my syntax, people can also download it and proceed. I actually have a couple of such little ado's and perhaps I should group them.

Please vent your opinion if this little ado would be a good practice, or whether it is useless or redundant.

Thanks

Sem

cap program drop mkcons
program define mkcons
capture sum cons
if _rc == 0 {
if (r(min) != r(max)) | (r(mean) !=1) {
replace cons = 1
di as result "Cons is not constant or not equal to 1"
}
else {
di as result "Cons = 1 already exists"
}
}
if _rc != 0 {
gene byte cons = 1
di as result "Cons = 1 has been made"
}
end

mkcons