Differences between revisions 7 and 9 (spanning 2 versions)
Revision 7 as of 2023-06-13 21:08:03
Size: 1823
Comment:
Revision 9 as of 2023-06-15 15:19:55
Size: 4151
Comment:
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
See also `Cdhms`. 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.
Line 21: Line 28:
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
}}}
Line 27: Line 41:
See also `Chms`. 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.
Line 35: Line 56:
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
}}}
Line 41: Line 69:
See also `Cmdyhms`.

----



== Y ==

----



== Yh ==

----



== Ym ==

----



== Yq ==

----



== Yw ==
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.
Line 83: Line 88:
== String ==

----



== StrOfReal ==

----



== Random Number Functions ==

=== Runiform ===
== RBeta ==

----



== RBinomial ==

----



== RCauchy ==

----



== RChi2 ==

----



== RExponential ==

----



== RGamma ==

----



== RHypergeometric ==

----



== RIGaussian ==

----



== RLaplace ==

----



== RLogistic ==

----



== RNBinomial ==

-----



== RNormal ==

----



== RPoisson ==

----



== RT ==

----



== RUniform ==
Line 112: Line 187:
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.
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 ==

----



== String ==

Alias for `strofreal`.

----



== StrOfReal ==

----



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


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.


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


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.


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.


Real


RBeta


RBinomial


RCauchy


RChi2


RExponential


RGamma


RHypergeometric


RIGaussian


RLaplace


RLogistic


RNBinomial


RNormal


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


RUniformInt


RWeibull


RWeibullPH


String

Alias for strofreal.


StrOfReal


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