|
Size: 1027
Comment:
|
← Revision 5 as of 2025-10-24 15:51:55 ⇥
Size: 788
Comment: Rewrite
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 2: | Line 2: |
'''Scalars''' are a fundamental data type in Stata, like [[Stata/Matrices|matrices]]. |
|
| Line 9: | Line 11: |
| == Definition == | == Usage == |
| Line 11: | Line 13: |
| To define a scalar, use the `scalar define` command. The `define` keyword can be left off. | Scalars are singular values. To declare a scalar, try: |
| Line 19: | Line 23: |
| Scalar variables are accessed by their name. | Note the `define` subcommand can be omitted. |
| Line 21: | Line 25: |
| {{{ generate double score = i * j }}} |
String scalars can hold arbitrarily long strings, and can also hold binary data. To list all defined scalars, try `scalar list _all` or `scalar dir _all`. To clear all scalars, try `scalar drop _all`. |
| Line 27: | Line 33: |
| === String Scalars === | === Anonymous Scalars === |
| Line 29: | Line 35: |
| String scalars can hold arbitrarily long strings, and can also hold binary data. ---- == Usage == To examine a scalar variable, try: |
To obtain a random, temporary name for a scalar that will never collide with an existing variable or scalar defined, try: |
| Line 40: | Line 38: |
| 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) |
tempname myN scalar `myN' = r(N) |
Stata Scalars
Scalars are a fundamental data type in Stata, like matrices.
Contents
Usage
Scalars are singular values.
To declare a scalar, try:
scalar define i = 1 scalar j = 2 scalar k = "Hello, world!"
Note the define subcommand can be omitted.
String scalars can hold arbitrarily long strings, and can also hold binary data.
To list all defined scalars, try scalar list _all or scalar dir _all.
To clear all scalars, try scalar drop _all.
Anonymous Scalars
To obtain a random, temporary name for a scalar that will never collide with an existing variable or scalar defined, try:
tempname myN scalar `myN' = r(N)
