Differences between revisions 2 and 11 (spanning 9 versions)
Revision 2 as of 2022-03-16 15:42:29
Size: 1254
Comment:
Revision 11 as of 2025-03-04 19:55:57
Size: 5109
Comment: Partial rewrite
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Stata supports these '''numeric functions''' in the global scope.
Line 9: Line 11:
== Random Number Functions ==

=== Runiform ===
== 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)` || ||
||`round(n,p)` ||Round to the nearest value for a given precision|| ||

----




== Date and Time Functions ==


||'''Function Name'''||'''Meaning''' ||'''Output Format'''||'''Example'''||
||`dhms(d,h,m,s)` ||Attach hour, minute, and second data to a date ||`%tc` || ||
||`dmy(d,m,y)` ||Calculate a date from a year, month, and day ||`%td` || ||
||`Cdhms(d,h,m,s)` ||Attach hour, minute, and second data to a date ||`%tC` || ||
||`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` || ||

Note that `%tc` formats ignore leap seconds, while `%tC` formats do ''not''.

----



== Statistical Functions ==


||'''Function Name'''||'''Meaning''' ||'''Example''' ||
||`invnormal` ||Inverse cumulative standard normal distribution||`1.959964 = invnormal(1-0.05/2)`||
||`normal` ||Cumulative standard normal distribution ||`.9750021 = normal(1.96)` ||

----









== 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 ==

----



== Round ==

----




== RUniform ==
Line 14: Line 128:

Below is a demonstration for how an [[SurveySamples#Sampling_Methods|SRS sample]] can be drawn.
Line 22: Line 138:
The return value is a [[Stata/Types|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 '''`set rng`'''.



=== Set Rng ===

To use an alternate random number generation algorithm, set the rng value to something other than `default`.

{{{
set rng default
}}}

Possible values include...

 * `mt64` (64-bit Mersenne Twister)
 * `mt64s` (stream algorithm based on the 64-bit Mersenne Twister)
 * `kiss32` (32-bit keep it simple stupid)

In Stata 14, the default flipped from `kiss32` to `mt64`.



=== Set Seed ===

To make random number generation reproducible, set a seed value.

{{{
set seed 42
}}}

A seed value must be an integer, non-negative, and no greater than 2^31^ − 1.
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.

----




== Sign ==

----



== SqRt ==

----



== String ==

Alias for `strofreal`.

----



== StrOfReal ==

----



== Sum ==

----





== 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
}}}

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)

round(n,p)

Round to the nearest value for a given precision


Date and Time Functions

Function Name

Meaning

Output Format

Example

dhms(d,h,m,s)

Attach hour, minute, and second data to a date

%tc

dmy(d,m,y)

Calculate a date from a year, month, and day

%td

Cdhms(d,h,m,s)

Attach hour, minute, and second data to a date

%tC

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

Note that %tc formats ignore leap seconds, while %tC formats do not.


Statistical Functions

Function Name

Meaning

Example

invnormal

Inverse cumulative standard normal distribution

1.959964 = invnormal(1-0.05/2)

normal

Cumulative standard normal distribution

.9750021 = normal(1.96)


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


Round


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 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 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 set rng.

See also set seed for designing deterministic programs.


Sign


SqRt


String

Alias for strofreal.


StrOfReal


Sum


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


CategoryRicottone

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