Differences between revisions 10 and 12 (spanning 2 versions)
Revision 10 as of 2023-06-15 15:25:53
Size: 4987
Comment:
Revision 12 as of 2025-03-05 01:56:01
Size: 5007
Comment: Finished rewrite
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Abs == == General Purpose ==

||'''Function Name'''||'''Meaning''' ||'''Example''' ||
||`abs(n)` ||Absolute value function || ||
||`ceil(n)` ||Round up to an integer || ||
||`comb(n,k)` ||[[Statistics/Combinations|Combinatorial function|| ||
||`exp(n)` ||Exponential function: ''e^n^'' || ||
||`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 [[Statistics/Logit|logit function]] || ||
||`ln(n)` ||Natural log function || ||
||`logit(n)` ||[[Statistics/Logit|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)`||
Line 17: Line 43:
== Ceil ==
== Date and Time Functions ==

These are largely defined in terms of ''dates'' and ''times'', which have a standardized meaning. They count days (seconds) from the Stata epoch: `01jan1960 00:00:00.000`.

||'''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 time from a year, month, day, hour, minute, and second ||`%tc` ||
||`Cmdyhms(m,d,y,h,m,s)`||Calculate a time 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` ||
Line 23: Line 67:
== CLogLog == == 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 [[Stata/Set#Rng|set rng]] and [[Stata/Set#Seed|set seed]].
Line 29: Line 81:
== Comb == == See also ==
Line 31: Line 83:
---- [[www.stata.com/manuals/fnmathematicalfunctions.pdf|Stata mathematical functions]]
Line 33: Line 85:


== Dhms ==

Convert an date value (formatted as `%td) plus an hour, minute, and second into the number of milliseconds since the Stata epoch (`01jan1960 00:00:00.000`) ''ignoring'' leap seconds.

{{{
generate double datetime = dhms(dmy(day, month, year), hour, minute, second)
format date %tc
}}}

See also `Cdhms`, which creates returns a number that should instead be formatted as `%tC` because it does ''not'' ignore leap seconds.

----



== DiGamma ==

----



== Dmy ==

Convert a day, month, and year into the number of days since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate long date = dmy(day, month, year)
format date %td
}}}

----



== Exp ==

----



== ExpM1 ==

----



== Floor ==

----



== Hms ==

Convert an hour, minute, and second into the number of milliseconds since midnight ''ignoring'' leap seconds.

{{{
generate double datetime = hms(hour, minute, second)
format date %tc
}}}

See also `Chms`, which creates returns a number that should instead be formatted as `%tC` because it does ''not'' ignore leap seconds.

----



== Int ==

----



== InvCLogLog ==

----



== InvLogit ==

----



== Ln ==

----



== Ln1M ==

----



== Ln1P ==

----



== LnFactorial ==

----



== LnGamma ==

----



== Log ==

----



== Log10 ==

----



== Log1M ==

----



== Log1P ==

----



== Logit ==

----



== Max ==

----



== Mdy ==

Convert a month, day, and year into the number of days since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate long date = mdy(month, day, year)
format date %td
}}}

----



== Mdyhms ==

Convert a day, month, year, hour, minute, and second into the number of milliseconds since the Stata epoch (`01jan1960 00:00:00.000`) ''ignoring'' leap seconds.

{{{
generate double datetime = mdyhms(month, day, year, hour, minute, second)
format date %tc
}}}

See also `Cmdyhms`, which creates returns a number that should instead be formatted as `%tC` because it does ''not'' ignore leap seconds.

----



== Min ==

----



== Mod ==

----



== Real ==

----



== RBeta ==

----



== RBinomial ==

----



== RCauchy ==

----



== RChi2 ==

----



== RelDif ==

----



== RExponential ==

----



== RGamma ==

----



== RHypergeometric ==

----



== RIGaussian ==

----



== RLaplace ==

----



== RLogistic ==

----



== RNBinomial ==

-----



== RNormal ==

----



== Round ==

----



== RPoisson ==

----



== RT ==

----



== RUniform ==

'''`runiform(a,b)`''' returns a random number between `a` and `b`. If no parameters are specified, the defaults of `0` and `1` are used.

Below is a demonstration for how an [[SurveySamples#Sampling_Methods|SRS sample]] can be drawn.

{{{
set seed 123456
generate double r_sampled = runiform()
sort r_sampled
generate byte sampled = _n <= 100
}}}

The return value is a [[Stata/DataTypes|double]]; it will be within `a + c(epsdouble)` and `b − c(epsdouble)`.

By default, `runiform` uses the 64-bit Mersenne Twister algorithm. Alternate algorithms are available; see [[Stata/Set#Rng|set rng]].

See also [[Stata/Set#Seed|set seed]] for designing deterministic programs.

----



== RUniformInt ==

----



== RWeibull ==

----



== RWeibullPH ==

----



== Sign ==

----



== SqRt ==

----



== String ==

Alias for `strofreal`.

----



== StrOfReal ==

----



== Sum ==

----



== TriGamma ==

----



== Trunc ==

----



== Y ==

Convert a year into the number of years since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate int year = y(year)
format year %ty
}}}

----



== Yh ==

Convert a year and half year into the number of half years since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate int halfyears = yh(year, halfyear)
format halfyears %th
}}}

----



== Ym ==

Convert a year and month into the number of months since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate int months = y(year, month)
format months %tm
}}}

----



== Yq ==

Convert a year and quarter into the number of years since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate int quarters = yq(year, quarter)
format quarters %tq
}}}

----



== Yw ==

Convert a year and week into the number of weeks since the Stata epoch (`01jan1960 00:00:00.000`).

{{{
generate int weeks = yw(year, week)
format weeks %tw
}}}
[[https://www.stata.com/manuals/ddatetime.pdf|Stata datetimes]]

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)

[[Statistics/Combinations|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 are largely defined in terms of dates and times, which have a standardized meaning. They count days (seconds) from the Stata epoch: 01jan1960 00:00:00.000.

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 time from a year, month, day, hour, minute, and second

%tc

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

Calculate a time 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-05-20 19:25:02 by DominicRicottone)