Stata Numeric Functions

Stata supports these numeric functions in the global scope.


General Purpose

Function Name

Meaning

Example

abs(n)

Absolute value function

ceil(n)

Round up to an integer

comb(n,k)

Combinatorial function

exp(n)

Exponential function: en

expm1(n)

High precision implementation of exp(n)-1

floor(n)

Round down to an integer

int(n)

Round towards 0 to an integer

-5 = int(-5.8)

invlogit(n)

Inverse logit function

ln(n)

Natural log function

logit(n)

Logit function

ln1m(n)

High precision implementation of ln(n-1)

ln1p(n)

High precision implementation of ln(n+1)

max(n,...)

Returns the value of the greatest argument

5 = max(1,.,5)

mix(n,...)

Returns the value of the least argument

1 = min(1,.,5)

mod(x,y)

x modulo y

real(s)

Convert string s into a real number

round(n)

Round to the nearest integer

round(n,p)

Round to the nearest value for a given precision

sign(n)

Returns -1 if n<0, 0 if n=0, and 1 if n>0

sqrt(n)

Square root function

string(n)

Convert numeric value n into a string

strofreal(n)

Convert numeric value n into a string

sum(x)

Running sum of variable x

trunc(n)

Round towards 0 to an integer

-5 = trunc(-5.8)


Date and Time Functions

These functions return date and datetime formatted values.

Function Name

Meaning

Output Format

dhms(d,h,m,s)

Attach hour, minute, and second data to a date

%tc

Cdhms(d,h,m,s)

Attach hour, minute, and second data to a date

%tC

dmy(d,m,y)

Calculate a date from a day, month, and year

%td

hms(h,m,s)

Calculate a time from an hour, minute, and second

%tc

Chms(h,m,s)

Calculate a time from an hour, minute, and second

%tC

mdy(m,d,y)

Calculate a date from a month, day, and year

%td

mdyhms(m,d,y,h,m,s)

Calculate a datetime from a year, month, day, hour, minute, and second

%tc

Cmdyhms(m,d,y,h,m,s)

Calculate a datetime from a year, month, day, hour, minute, and second

%tC

y(y)

Convert a numeric year into the number of years since the epoch

%ty

yh(y,h)

Convert a year and halfyear into the number of half years since the epoch

%th

ym(y,m)

Convert a year and month into the number of months since the epoch

%tm

yq(y,q)

Convert a year and quarter into the number of quarters since the epoch

%tq

yw(y,w)

Convert a year and week into the number of weeks since the epoch

%tw


Statistical Functions

Function Name

Meaning

Example

invnormal(p)

Inverse cumulative standard normal distribution

1.959964 = invnormal(1-0.05/2)

normal(z)

Cumulative standard normal distribution

.9750021 = normal(1.96)

runiform()

Random number from uniform distribution from 0 to 1

runiform(a,b)

Random number from uniform distribution from a to b

Of relevance to random values: see set rng and set seed.


See also

Stata mathematical functions

Stata datetimes


CategoryRicottone

Stata/NumericFunctions (last edited 2025-03-05 03:59:22 by DominicRicottone)