Size: 1738
Comment:
|
← Revision 15 as of 2025-05-20 19:25:02 ⇥
Size: 7615
Comment: Date diff functions
|
Deletions are marked like this. | Additions are marked like this. |
Line 11: | Line 11: |
== Dhms == | == General Purpose == |
Line 13: | Line 13: |
See also `Cdhms`. | ||'''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 19: | Line 43: |
== Dmy == | == Date and Time Functions == These functions take numbers but return [[Stata/DataFormats#Date_and_Datetime_Formats|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` || These functions take two arguments: a date and a number. The number is a weekday identifier and must be between 0 (representing Sunday) through 6 (representing Saturday). The return value must be between 1 and 7 (inclusive). ||'''Function Name''' ||'''Meaning''' || ||`dayssincedow(d,n)` ||Days between reference date and last weekday `n`|| ||`dayssinceweekday(d,n)`||Days between reference date and last weekday `n`|| ||`daysuntildow(d,n)` ||Days between reference date and next weekday `n`|| ||`daysuntilweekday(d,n)`||Days between reference date and next weekday `n`|| These functions also operate on date or datetime formatted values and return numbers. ||'''Function Name''' ||'''Meaning''' || ||`age(dob,d)` ||Integer age calculated from `dob` and reference date || ||`age_frac(dob,d)` ||Decimal age calculated from `dob` and reference date || ||`Clockdiff(a,b,u)` ||Integer difference between two datetimes, assuming `%tC` format|| ||`Clockdiff_frac(a,b,u)`||Decimal difference between two datetimes, assuming `%tC` format|| ||`clockdiff(a,b,u)` ||Integer difference between two datetimes, assuming `%tc` format|| ||`clockdiff_frac(a,b,u)`||Decimal difference between two datetimes, assuming `%tc` format|| ||`datediff(a,b,u)` ||Integer difference between two dates || ||`datediff_frac(a,b,u)` ||Decimal difference between two dates || Most of the above functions require a (case-insensitive) unit `u` argument, specifying what the return value should be encoded as. This then also determines ''whether'' there is a fractional element that can be returned by the `_frac` version of the function. * For date differences (e.g., `datediff`), `u` can be: * `"day"` or `"d"` for days * `"month"`, `"mon"`, or `"m"` for months * `"year"` or `"y"` for years * For datetime differences (e.g., `clockdiff`), `u` can be: * `"day"` or `"d"` for days * `"hour"` or `"h"` for hours * `"minute"`, `"min"`, or `"m"` for minutes * `"second"`, `"sec"`, or `"s"` for seconds * `"millisecond"` or `"ms"` for milliseconds Additionally, the functions operating on dates (e.g., `datediff`) can take an (case-insensitive) optional argument, specifying what date should be considered the anniversary for February 29th outside of leap years. This argument can be: * `"1mar"`, `"01mar"`, `"mar1"`, or `"mar01"` (the default) * `"28feb"` or `"feb28"` |
Line 25: | Line 103: |
== Hms == | == Statistical Functions == |
Line 27: | Line 105: |
See also `Chms`. | ||'''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 33: | Line 117: |
== Mdy == | == See also == |
Line 35: | Line 119: |
---- | [[https://www.stata.com/manuals/fnmathematicalfunctions.pdf|Stata mathematical functions]] |
Line 37: | Line 121: |
== Mdyhms == See also `Cmdyhms`. ---- == Y == ---- == Yh == ---- == Ym == ---- == Yq == ---- == Yw == ---- == Random Number Functions == === 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 '''`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. |
[[https://www.stata.com/manuals/ddatetime.pdf|Stata datetimes]] |
Stata Numeric Functions
Stata supports these numeric functions in the global scope.
Contents
General Purpose
Function Name |
Meaning |
Example |
abs(n) |
Absolute value function |
|
ceil(n) |
Round up to an integer |
|
comb(n,k) |
|
|
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) |
|
|
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 take numbers but 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 |
These functions take two arguments: a date and a number. The number is a weekday identifier and must be between 0 (representing Sunday) through 6 (representing Saturday). The return value must be between 1 and 7 (inclusive).
Function Name |
Meaning |
dayssincedow(d,n) |
Days between reference date and last weekday n |
dayssinceweekday(d,n) |
Days between reference date and last weekday n |
daysuntildow(d,n) |
Days between reference date and next weekday n |
daysuntilweekday(d,n) |
Days between reference date and next weekday n |
These functions also operate on date or datetime formatted values and return numbers.
Function Name |
Meaning |
age(dob,d) |
Integer age calculated from dob and reference date |
age_frac(dob,d) |
Decimal age calculated from dob and reference date |
Clockdiff(a,b,u) |
Integer difference between two datetimes, assuming %tC format |
Clockdiff_frac(a,b,u) |
Decimal difference between two datetimes, assuming %tC format |
clockdiff(a,b,u) |
Integer difference between two datetimes, assuming %tc format |
clockdiff_frac(a,b,u) |
Decimal difference between two datetimes, assuming %tc format |
datediff(a,b,u) |
Integer difference between two dates |
datediff_frac(a,b,u) |
Decimal difference between two dates |
Most of the above functions require a (case-insensitive) unit u argument, specifying what the return value should be encoded as. This then also determines whether there is a fractional element that can be returned by the _frac version of the function.
For date differences (e.g., datediff), u can be:
"day" or "d" for days
"month", "mon", or "m" for months
"year" or "y" for years
For datetime differences (e.g., clockdiff), u can be:
"day" or "d" for days
"hour" or "h" for hours
"minute", "min", or "m" for minutes
"second", "sec", or "s" for seconds
"millisecond" or "ms" for milliseconds
Additionally, the functions operating on dates (e.g., datediff) can take an (case-insensitive) optional argument, specifying what date should be considered the anniversary for February 29th outside of leap years. This argument can be:
"1mar", "01mar", "mar1", or "mar01" (the default)
"28feb" or "feb28"
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.