Stata 15 help for confirm

[P] confirm -- Argument verification

Syntax

confirm existence string

confirm [new] file filename

confirm [ numeric | string | date ] format string

confirm names names

confirm [integer] number string

confirm matrix string

confirm scalar string

confirm [ new | numeric | string | str# | type ] variable varlist [, exact]

where type is { byte | int | long | float | double | str# | strL }

Description

confirm verifies that the arguments following confirm ... are of the claimed type and issues the appropriate error message and nonzero return code if they are not.

confirm is useful in do-files and programs when you do not want to bother issuing your own error message. confirm can also be combined with capture to detect and handle error conditions before they arise; see [P] capture.

Option

exact specifies that a match be declared only if the names specified in varlist match. By default, names that are abbreviations of variables are considered to be a match.

Examples

. confirm file `"c:\data\mydata.dta"'

. confirm numeric variable price trunk rep78

You are writing a command that performs some action on each of the variables in the local macro varlist. The action should be different for string (both str# and strL) and numeric variables. The confirm command can be used here in combination with the capture command to switch between the different actions:

foreach v of local varlist { capture confirm numeric variable `v' if !_rc { action for numeric variables } else { action for str# or strL variables } }

An alternative solution using inline expansion of the extended macro function :type (see local) reads

foreach v of local varlist { if substr("`:type `v''",1,3) == "str" { action for string variables } else { action for numeric variables } }

If you need to differentiate between str# and strL variables as well as numeric variables, you could instead code

foreach v of local varlist { capture confirm str# variable `v' if !_rc { action for str# variables } capture confirm strL variable `v' if !_rc { action for strL variables } capture confirm numeric variable `v' if !_rc { action for numeric variables } }


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index