본문 바로가기

방법론 공부/계량통계 방법론

[STATA] log, capture, delimit command - STATA 시작하기

STATA command를 입력할 때 명령어 앞에 capture 혹은 cap을 붙일 때가 있습니다.

이는 쉽게 말하면 이와 관련된 게 있으면 실행하고, 그렇지 않으면 잔만말고 가만히 있으라는 뜻입니다. 그리고 코멘트의 기록이 남지 않기를 원하면 입력합니다. 


대개 STATA를 통해 데이터를 분석하기 이전에 혹시나 열려있는 log 파일이 있을 경우를 대비해서 log 파일을 닫는 작업을 먼저 실행합니다. 혹시 열러있는 log 파일이 있다면 "log using"이라는 command가 작동하지 않으니까요. 그래서 "log close"를 먼저 합니다. 

(do file에 에러가 있으면 끝까지 명령어들을 실행하기 전에 멈추거든요. 그래서 먼저 닫았다가 다시 열어주는겁니다.)


이때


capture log close


혹은


cap log close


라는 명령어는 열려있는 log 파일이 있다면 닫을 것이고, 열려있는 파일이 없다면 에러 메시지를 띄우지 않고 가만히 있겠죠. (Stata should not complain if there is no log open to close.)


그리고나서


log using mylog.log


라는 명령어를 실행하면 mylog라는 log 파일을 생성하며 이후에 STATA에서 실행시키는 명령어들과 그 결과들을 저장하게 됩니다.


예시를 한번 볼까요.


#delimit ;

set more 1;

drop _all;

cap log close;

log using c:\intropov\logfiles\try1.log, replace;

use c:\intropov\data\hh.dta ;

describe ;

list in 1/3 ;

list hhcode famsize educhead if sexhead==2 & agehead<45;

summarize famsize;

summarize famsize, detail;

sum famsize educhead [aw=weight], d;

tabulate sexhead;

tabulate educhead sexhead, col row chi;

tabulate educhead, summarize(agehead);

label define sexlabel 1 “MALE” 2 “FEMALE”;

label values sexhead sexlabel;

tabulate sexhead;

label variable sexhead "Head Gender";

use c:\intropov\data\hh.dta;

sort hhcode;

save temp, replace;

use c:\intropov\data\consume.dta, clear;

sort hhcode ;

#delimit cr

merge hhcode using temp

tabulate _merge

keep if _merge==3

drop _merge

log close 


이때 맨 처음에 쓰인


#delimit;


은 STATA do-file의 가독성을 높여주기 위한 명령어로 이후에 오는 모든 command가 줄과 상관없이 관련이 있다고 인식하게 합니다. 따라서 이후에 나오는 모든 명령어 마지막에는 세미콜론(;)이 붙여주어야 합니다. 그리고 delimit에 해당되는 부분이 끝난 다음에는


#delimit cr


로 마무리해야 합니다.


다시 한번 위의 예시를 보겠습니다.



#delimit ;

set more 1;

drop _all;

cap log close;

log using c:@intropov\logfiles\try1.log, replace;

use c:\intropov\data\hh.dta ;

describe ;

list in 1/3 ;

list hhcode famsize educhead if sexhead==2 & agehead<45;

summarize famsize;

summarize famsize, detail;

sum famsize educhead [aw=weight], d;

tabulate sexhead;

tabulate educhead sexhead, col row chi;

tabulate educhead, summarize(agehead);

label define sexlabel 1 “MALE” 2 “FEMALE”;

label values sexhead sexlabel;

tabulate sexhead;

label variable sexhead "Head Gender";

use c:\intropov\data\hh.dta;

sort hhcode;

save temp, replace;

use c:\intropov\data\consume.dta, clear;

sort hhcode ;

#delimit cr

merge hhcode using temp

tabulate _merge

keep if _merge==3

drop _merge

log close


이 포스팅이 마음에 드셨다면 공감 버튼을 눌러주세요

로그인 하지 않으셔도 공감하실 수 있답니다.