본문으로 바로가기

연구제안 및 문의하기

함께하는 KLIPS, 소통하는 KLIPS, 열린 KLIPS

FAQ

가구주의 성별, 연령, 학력 등 인구학적 변수 추출하기 - SAS Array 문, SPSS Repeat 문, Stata Foreach문 이용

  • Date2016.02.03
  • Hit4,093
가구자료를 이용하는 분석모형에서 가구주의 성별, 연령, 학력 등의 인구학적 특성은 핵심적인 변수로 사용되곤 한다. 그러나 가구자료는 이들 정보에 대해 1가구 내 최대 15명의 정보를 담고 있기 때문에 몇 번째 가구원의 정보를 어떻게 취해야 하는지에 대한 문제에 부딪히게 된다. 이런 경우, SAS 사용자는 array문을, SPSS 사용자는 repeat문을 사용하여 쉽게 가구주의 인구학적 특성 변수들을 구성할 수 있다.


*===========================;
* SAS- Array문의 사용 ;
*===========================;

data h19; set a.klips19h;
if hwave19=1;
array h[15] h190261-h190275; /*가구주와의 관계-값이 10이면 본인(가구주)*/
array s[15] h190241-h190255; /*가구원의 성별*/
array a[15] h190361-h190375; /*가구원의 나이*/

do i=1 to 15;
if h[i]=10 then hsex=s[i]; /*가구구주의 성별*/
if h[i]=10 then hage=a[i]; /*가구주의 나이*/
end;

proc freq; table hsex;
proc means; var hage;
run;


*===========================.
* SPSS- Repeat문의 사용 .
*===========================.

GET FILE='D:\19차\users guide\19차년도\klips19h.sav'.

do repeat
head=h190261 to h190275/
sex=h190241 to h190255/
age=h190361 to h190375.
if(head=10) hsex=sex.
if(head=10) hage=age.
end repeat.

fre hsex.
des hage/stat=mean std min max.


/*======================================*/
/* stata - foreach문의 사용 */
/*======================================*/

clear
use klips19h, clear
keep if hwave19==1
local i=1
foreach head of varlist h190261-h190275 {
rename `head' head`i'
local i=`i'+1
}
local j=1
foreach sex of varlist h190241-h190255 {
rename `sex' sex`j'
local j=`j'+1
}
local k=1
foreach age of varlist h190361-h190375 {
rename `age' age`k'
local k=`k'+1
}
gen hsex=.
gen hage=.
foreach n of num 1/15 {
replace hsex=sex`n' if head`n'==10
replace hage=age`n' if head`n'==10
}
tab hsex
sum hage




*===========================#
* R - melt문의 사용 #
*===========================#

library(foreign)
klips19h ← read.spss(file = "C:\\Users\\KLI\\DATA\\1-19spss\\klips19h.sav", use.value.labels = FALSE, to.data.frame = TRUE )
h19 ← klips19h[klips19h$hwave19==1,] #19차 응답 가구만 추출.
library(dplyr)
h ← select(h19,hhid19, h190261:h190275)
s ← select(h19,hhid19, h190241:h190255)
a ← select(h19,hhid19, h190361:h190375)

install.packages("reshape2") # 최초 1회 reshape2 패키지를 설치해준다.
library(reshape2)
hh ← melt(h, id.vars = c("hhid19"), variable.name = "number.h", value.name = "relationship")
ss ← melt(s, id.vars = c("hhid19"), variable.name = "number.s", value.name = "sex")
aa ← melt(a, id.vars = c("hhid19"), variable.name = "number.a", value.name = "age")

hsa ← cbind(hh ,ss ,aa)
head(hsa) #전체 가구원의 성별, 나이 데이터를 담은 데이터 프래임 생성.

hsa.owner ← na.omit(hsa[hsa$relationship == 10, c("relationship", "sex", "age")])
# 가구주의 경우만 추출. 전체 7012케이스.

table(hsa.owner$sex)
summary(hsa.owner$age)

<한국노동패널 1~19차년도 조사자료 User's Guide 135p Q10 참고>

스크랩