Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: checking data using assert

From   Sergiy Radyakin <>
To   "" <>
Subject   Re: st: checking data using assert
Date   Tue, 28 May 2013 22:53:31 -0400

This does not look like a job for assert. List seems a better fit.

On Tue, May 28, 2013 at 10:00 PM, Raoul Reulen <> wrote:
> Hi All,
> I am currently running several do-files in order to check data. I would like Stata to check whether a certain

1. count if dob>=dod

statement is true, e.g. date of birth (dob) < date of death (dod). If
it is not true I want it to display a message
saying what the problem is and display the ID numbers for those
records were the statement was not true. Lastly,
2. list id if dob>=dod

I want to list several variables (e,g. id, dob, dox) to inspect the
actual problem.
3. list id dob dox if dob>=dod

> I have done the following, but wondered if there are easier/more efficient ways of doing it?
> ******************************
> .gen byte tag = dod <  dob
> .qui:  levelsof   id    if tag==1
> .cap: assert  tag==0
> .if _rc di _new as err  "date of death before birth date; see id:   " as text "`r(levels)'"
> .list id dob dod  if tag==1 , noobs N
> .drop tag
> ******************************


sysuse auto, clear
generate id=_n
count if price>5000
if (r(N)>0) {
    display as error "Wow!Some cars are too expensive for me.Just look at them:"
    levelsof id if (price>5000)
    list id price make if (price>5000)

In any case it is safer to use a tempvar instead of 'tag' in your code.

If you are trying to avoid levelsof for any reason, perhaps Mata?

tempvar touse
generate touse=price>5000
mata st_view(V=.,.,"id","touse")
mata V'

Best, S.R.

> Many thanks for help! Appreciated as usual!
> All the best
> Raoul
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index