## Sunday, July 29, 2012

### If statements and relational operators

* In Stata there are two forms of if statemetns.

* Let's first generate some data
clear
set obs 1000

forv i=1/4 {
gen x`i'=rnormal()
}

* 1. There are the if Statements that are evaluated as a vector each value based on some rule.

* For instance:

replace x1 = 10000 if x1>0

sum

* We can see that some values of x1 are now equal to 1000 while all others are equal to or less than zero.

* 2. This is in contrast to the following commands

* This will sort the elements of x2 t0 have large values first
gsort -x2

if x2>0 replace x2 = 10000

* and

gsort x3

if x3>0 replace x3 = 10000

sum

* We can see obviously what each of these commands produced dramatically different results.

* This is because if the "if" statement is listed before a command then it is only evaluated once.

* And that value when it is evaluated with regards to a variable tells the if statement to only evaluate the first element of that variable.

* Thus the first element of x2 was greater then 0 so the replace command "replace x2 = 10000" was executed (for all elements of x2 while the first element of x3 was less than zero so the replace command was not executed.

* Both types of if Statements are useful though the second one tends to be more frequently used by programmers.

* Say you want to generate 10 variables with half drawn from a uniform and half from a normal distribution.

forv i=1/10 {
if mod(`i',2) == 0 gen z`i' = rnormal()
if mod(`i',2) == 1 gen z`i' = runiform()
}
sum
* We can see that the even numbered zs are normally distributed while the odd are uniformaly distributed.
* Note: a summary command is usually not sufficient to diagnose distributions.

* It is very useful to become knowledgable about relational operators.

* To see the help file on operators look at
help operators