Differences between revisions 3 and 4
Revision 3 as of 2023-06-08 00:23:43
Size: 492
Comment:
Revision 4 as of 2023-06-10 13:23:25
Size: 2601
Comment:
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
Arithmetic operators include:

||'''Operator''' ||'''Function'''||
||`+` ||addition ||
||`-` ||subtraction ||
||`*` ||multiplication||
||`/` ||division ||
||`^` ||exponentiation||
||unary `-` or `~`||negation ||

String operators include:

||'''Operator'''||'''Function'''||
||`+` ||concatenation ||

There is an operator taking one integer argument and one string argument:

||'''Operator'''||'''Function''' ||
||`*` ||duplication of a string||

Relational operators include:

||'''Operator'''||'''Function''' ||
||`>` ||greater than ||
||`>=` ||greater than or equal||
||`<` ||less than ||
||`<=` ||less than or equal ||
||`==` ||equal ||
||`!=` or `~=` ||not equal ||

Relational operators resolve to values of 0 or 1, so they can be mixed with arithmetic operations.

Relational operators cannot be chained, i.e. `1 < 2 < 3` is not valid syntax.

Logical operators include:

||'''Operator'''||'''Function'''||
||`&` ||and ||
||`|` ||or ||
||`!` or `~` ||not ||
Line 18: Line 59:

Most commands support an `if EXPR` clause. Reference the command's specific syntax.

{{{
replace foo = bar if baz==1
}}}

----



== If ==

A command can be made to operate conditionally in the sense that the ''command itself'' may or may not be called.

{{{
if ${debug}==1 assert errors==0

if ${debug}==1 {
  assert errors==0
}
}}}

An `if` structure can be followed by an `else` structure.

{{{
if ${debug}==1 assert errors==0
else display "Not in debug mode"

if ${debug}==1 {
  assert errors==0
}
else {
  display "Not in debug mode"
}
}}}

An 'elif'-type structure is only acheived by opening a new `if` structure on the `else` structure.

{{{
if ${debug}==1 {
  assert errors==0
}
else if ${debug}==0 {
  display "Not in debug mode"
}
else {
  display "Debug in unknown state"
}
}}}

Stata Logic

Stata supports the following logic structures for programming.


Operators

Arithmetic operators include:

Operator

Function

+

addition

-

subtraction

*

multiplication

/

division

^

exponentiation

unary - or ~

negation

String operators include:

Operator

Function

+

concatenation

There is an operator taking one integer argument and one string argument:

Operator

Function

*

duplication of a string

Relational operators include:

Operator

Function

>

greater than

>=

greater than or equal

<

less than

<=

less than or equal

==

equal

!= or ~=

not equal

Relational operators resolve to values of 0 or 1, so they can be mixed with arithmetic operations.

Relational operators cannot be chained, i.e. 1 < 2 < 3 is not valid syntax.

Logical operators include:

Operator

Function

&

and

|

or

! or ~

not


Conditional Processing

Most commands support an if EXPR clause. Reference the command's specific syntax.

replace foo = bar if baz==1


If

A command can be made to operate conditionally in the sense that the command itself may or may not be called.

if ${debug}==1 assert errors==0

if ${debug}==1 {
  assert errors==0
}

An if structure can be followed by an else structure.

if ${debug}==1 assert errors==0
else display "Not in debug mode"

if ${debug}==1 {
  assert errors==0
}
else {
  display "Not in debug mode"
}

An 'elif'-type structure is only acheived by opening a new if structure on the else structure.

if ${debug}==1 {
  assert errors==0
}
else if ${debug}==0 {
  display "Not in debug mode"
}
else {
  display "Debug in unknown state"
}


By

The by command prefix causes a command to be repeated within groups.

A common usecase of the by structure is for identifying duplciate cases.

sort foo bar baz
by foo bar baz: generate byte duplicate = cond(_N==1, 0, _n)


CategoryRicottone

Stata/Logic (last edited 2023-06-10 13:23:25 by DominicRicottone)