global greeting Hello world!
global "$greeting"
Hello world!
global demographics price mpg rep78
summ $demographics
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
price | 74 6165.257 2949.496 3291 15906
mpg | 74 21.2973 5.785503 12 41
rep78 | 69 3.405797 .9899323 1 5
local demographics price mpg rep78
summ `demographics'
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
price | 74 6165.257 2949.496 3291 15906
mpg | 74 21.2973 5.785503 12 41
rep78 | 69 3.405797 .9899323 1 5
(iii) tempfile
tempfile new_file
save `new_file', replace
forvalues loop_name = range {
commands
}
forvalues i = 1/5 {
display " i = `i' "
}
i = 1 i = 2 i = 3 i = 4 i = 5
foreach loop_name (in|of listtype) list { commands }
foreach var of varlist price mpg rep78 { egen std_`var' = std(`var') }
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
std_price | 74 -4.83e-10 1 -.9744909 3.302511
std_mpg | 74 -7.00e-09 1 -1.606999 3.40553
std_rep78 | 69 8.64e-10 1 -2.430264 1.610416
collapse price
+---------+
| price |
|---------|
1. | 6,165.3 |
+---------+
collapse price, by(foreign)
+--------------------+
| foreign price |
|--------------------|
1. | Domestic 6,072.4 |
2. | Foreign 6,384.7 |
+--------------------+
collapse (mean)price (max)mpg (min)rep78, by(foreign)
+----------------------------------+
| foreign price mpg rep78 |
|----------------------------------|
1. | Domestic 6,072.4 34 1 |
2. | Foreign 6,384.7 41 3 |
+----------------------------------+
sysuse auto, clear preserve drop make price mpg rep78 save part1_dataset.dta, replace restore preserve collapse (mean)price (mean)mpg, by(foreign) save part2_dataset.dta, replace restore
input id x y 1 1 2 2 1 2 3 1 2 end save dataset1, replace list
+------------+
| id x y |
|------------|
1. | 1 1 2 |
2. | 2 1 2 |
3. | 3 1 2 |
+------------+
b) Data 2
clear input id y z 1 2 3 2 2 3 3 2 3 end save dataset2, replace list
+------------+
| id y z |
|------------|
1. | 1 2 3 |
2. | 2 2 3 |
3. | 3 2 3 |
+------------+
If we wanted to combine these files by stacking them one atop the other, we can use the append command as shown below. While appending datasets, it appends with the same or matched varaibles and others will be shown as missing values.
use dataset1, clear
append using dataset2
list
+----------------+
| id x y z |
|----------------|
1. | 1 1 2 . |
2. | 2 1 2 . |
3. | 3 1 2 . |
4. | 1 . 2 3 |
5. | 2 . 2 3 |
6. | 3 . 2 3 |
+----------------+
(ii) merge
Syntax
One-to-one merge on specified key variables
merge 1:1 varlist using filename [, options]
Many-to-one merge on specified key variables
merge m:1 varlist using filename [, options]
One-to-many merge on specified key variables
merge 1:m varlist using filename [, options]
clear use dataset1, replace merge 1:1 id using dataset2 list
dataset1 dataset2 merged dataset
+------------+ +------------+ +------------------------------+
| id x y | | id y z | | id x y z _merge |
|------------| + |------------| = |------------------------------|
1. | 1 1 2 | 1. | 1 2 3 | 1. | 1 1 2 3 matched (3) |
2. | 2 1 2 | 2. | 2 2 3 | 2. | 2 1 2 3 matched (3) |
3. | 3 1 2 | 3. | 3 2 3 | 3. | 3 1 2 3 matched (3) |
+------------+ +------------+ +------------------------------+
(b) MANY-TO-ONE (m:1)
Syntax:
long wide
+---------------+ +------------------+
| i j a b | | i a1 a2 b1 b2 |
|---------------| <--- reshape ---> |------------------|
| 1 1 1 2 | | 1 1 3 2 4 |
| 1 2 3 4 | | 2 5 7 6 8 |
| 2 1 5 6 | +------------------+
| 2 2 7 8 |
+---------------+
long to wide: reshape wide a b, i(i) j(j) (j existing variable)
wide to long: reshape long a b, i(i) j(j) (j new variable)
(1) UCLA: https://stats.idre.ucla.edu/stata/