Stata Scalars

Scalars are a data type used for meta programming in the Stata programming language. They are more comparable to macros than any core data type within Stata. See also matrices.


Definition

To define a scalar, use the scalar define command. The define keyword can be left off.

scalar define i = 1
scalar j = 2
scalar k = "Hello, world!"

Scalar variables are accessed by their name.

generate double score = i * j

String Scalars

String scalars can hold arbitrarily long strings, and can also hold binary data.


Usage

To examine a scalar variable, try:

scalar dir i

Use scalar dir _all (or scalar list _all) to list the contents of all scalar variables.

To delete a scalar variable, try:

scalar drop i

Use scalar drop _all to delete all scalar variables.

To obtain a random, temporary name for a scalar that cannot collide with any variables or scalars defined in a dataset, try:

tempname n1 n2

count if cohort==1
scalar `n1' = r(N)

count if cohort==2
scalar `n2' = r(N)


CategoryRicottone

Stata/Scalars (last edited 2023-06-08 00:42:21 by DominicRicottone)