**    www.stata.com

*****************************
*                           *
*   introduction to STATA   *
*                           *
*****************************

cd

** set working folder
cd "C:\WORKS\...\EDYI_slab1"


*****************************
*
* import DATA into Stata
*
*****************************

** import excel file
clear

import excel "C:\WORKS\metaptyxiako\Statistika paketa\EDYII_slab1\press.xls", sheet("Sheet1") firstrow

** import text file

insheet using press.txt

describe 

list

count

** save data in stata format
save press, replace

use press, clear



*******************************************
* Stata command syntax
*******************************************
[prefix:] command [varlist][if expression][in range][weight][, options]


******************************
*
* create new variables
*
******************************
describe

** Age at 2005
generate age=2005-byear

** dichotomize variable
gen young=1 if age<=55
replace young=2 if age>55

** 2nd way

recode age (0/55=1) (56/200=2), gen(young2)

sort age
list age young young2

drop young2

** create variable Body-mass index (BMI)
generate bmi= weight/(height/100)^2

** String to numeric variable
gen sex=1 if gender=="M"
replace sex=2 if gender=="F"

** 2nd way

encode gender, gen(sex2)

list gender sex sex2, nolabel

drop sex2


*****************************
*
* labels
*
*****************************
** Label variables
label var byear "Year of birth"
label var bmi "Body-mass index"

** Label values of categ. variables
label define sexlab 1 "Male" 2 "Female"
label values sex sexlab

lab def agelab 1 "<=55 yrs" 2 ">55 yrs"
lab val young agelab


******************************
*
* rename variables
*
******************************

rename age age2005

rename young age_categ


******************************
*
* change values
*
******************************
** create categorical from continuous var and save to new variable
recode bmi (0/18.499=1 "underweight") (18.5/24.999=2 "normal") (25/29.999=3 "overweight") (30/500=4 "obese"), gen(bmicat)

** change values
gen fat=bmicat
replace fat=0 if fat<3
replace fat=1 if fat2==3 | fat2==4

** 2nd way
recode bmicat 1=0 2=0 3=1 4=1, gen(fat2)

list bmi fat fat2
drop fat2


*****************************
*
* create log file / DO file
*
*****************************

log using intro.log

use press, clear
describe
list
count

log close






