Hello all. I am working with a dataset that contains many categorical variables with values that are still in written word format, which I am trying to convert into numbers. Initially when I typed "summarize" no observations would register for any of the variables, even though I could see the values when I manually scrolled through the dataset. When I tried to run PCA and Regression on some of the variables, these commands would obviously not work because the data were still in string format and needed to be converted to numbers. I fixed that issue by using: generate oldvar=real(oldvar) for all of the variables I was planning on including in my analyses. After running the real() function I was able to obtain summary statistics and observations for all of the variables i needed for my regression. So I thought my problem of the "no observations" error would be fixed when I ran a regression using the new numerical variables. This was not the case, however. I am able to run PCA on several of the variables successfully (Vo1-Vo10), but the regression model I ran still returned the "no observations" error again when I included the now numerical "Race, Age, Education, Ethnicity, and Income" variables. The same error arises when I try to run a SEM with some of the variables. Has anyone found a fix to this "no observations" error arising even after string variables have been converted to numeric? I'll copy and paste my code below:
*Create race dummy variable with 1 as white, 0 as non white
gen race=""
replace race=Q101
replace race="1" if Q101=="White"
replace race="0" if Q101=="Black or African American"
replace race="0" if Q101=="American Indian or Alaska Native"
replace race="0" if Q101=="Asian"
replace race="0" if Q101=="Native Hawaiian or Pacific Islander"
replace race="0" if Q101=="Other"
*Create ethnicity categorical variable
gen ethnicity="."
replace ethnicity=Q102
replace ethnicity="1" if Q102=="Yes"
replace ethnicity="0" if Q102=="No"
*create income categorical variable
gen income="."
replace income=Q100
replace income="1" if Q100=="Less than $19,999"
replace income="2" if Q100=="$20,000 - $39,999"
replace income="3" if Q100=="$40,000 - $59,999"
replace income="4" if Q100=="$60,000 - $79,999"
replace income="5" if Q100=="$80,000 - $99,999"
replace income="6" if Q100=="$100,000 - $149,999"
replace income="7" if Q100=="More than $150,000"
*Create education categorical variable
gen education="."
replace education=Q99
replace education="1" if Q99=="Less than high school"
replace education="2" if Q99=="High school graduate"
replace education="3" if Q99=="Some college"
replace education="4" if Q99=="2 year degree"
replace education="5" if Q99=="4 year degree"
replace education="6" if Q99=="Professional degree"
replace education="7" if Q99=="Doctorate"
*Create age categorical variable
gen age="."
replace age=Q98
replace age="1" if Q98=="16 - 24"
replace age="2" if Q98=="25 - 34"
replace age="3" if Q98=="35 - 44"
replace age="4" if Q98=="45 - 54"
replace age="5" if Q98=="55 - 64"
replace age="6" if Q98=="65 - or older"
*Code value oriententation questions to prep for PCA
gen vo1="."
replace vo1=DT
replace vo1="1" if DT=="Strongly disagree"
replace vo1="2" if DT=="Somewhat disagree"
replace vo1="3" if DT=="Neither agree nor disagree"
replace vo1="4" if DT=="Somewhat agree"
replace vo1="5" if DT=="Strongly agree"
gen vo2="."
replace vo2=DU
replace vo2="1" if DU=="Strongly disagree"
replace vo2="2" if DU=="Somewhat disagree"
replace vo2="3" if DU=="Neither agree nor disagree"
replace vo2="4" if DU=="Somewhat agree"
replace vo2="5" if DU=="Strongly agree"
gen vo3="."
replace vo3=DV
replace vo3="1" if DV=="Strongly disagree"
replace vo3="2" if DV=="Somewhat disagree"
replace vo3="3" if DV=="Neither agree nor disagree"
replace vo3="4" if DV=="Somewhat agree"
replace vo3="5" if DV=="Strongly agree"
gen vo4="."
replace vo4=DW
replace vo4="1" if DW=="Strongly disagree"
replace vo4="2" if DW=="Somewhat disagree"
replace vo4="3" if DW=="Neither agree nor disagree"
replace vo4="4" if DW=="Somewhat agree"
replace vo4="5" if DW=="Strongly agree"
gen vo5="."
replace vo5=DX
replace vo5="1" if DX=="Strongly disagree"
replace vo5="2" if DX=="Somewhat disagree"
replace vo5="3" if DX=="Neither agree nor disagree"
replace vo5="4" if DX=="Somewhat agree"
replace vo5="5" if DX=="Strongly agree"
gen vo6="."
replace vo6=DY
replace vo6="1" if DY=="Strongly disagree"
replace vo6="2" if DY=="Somewhat disagree"
replace vo6="3" if DY=="Neither agree nor disagree"
replace vo6="4" if DY=="Somewhat agree"
replace vo6="5" if DY=="Strongly agree"
gen vo7="."
replace vo7=DZ
replace vo7="1" if DZ=="Strongly disagree"
replace vo7="2" if DZ=="Somewhat disagree"
replace vo7="3" if DZ=="Neither agree nor disagree"
replace vo7="4" if DZ=="Somewhat agree"
replace vo7="5" if DZ=="Strongly agree"
gen vo8="."
replace vo8=EA
replace vo8="1" if EA=="Strongly disagree"
replace vo8="2" if EA=="Somewhat disagree"
replace vo8="3" if EA=="Neither agree nor disagree"
replace vo8="4" if EA=="Somewhat agree"
replace vo8="5" if EA=="Strongly agree"
gen vo9="."
replace vo9=EB
replace vo9="1" if EB=="Strongly disagree"
replace vo9="2" if EB=="Somewhat disagree"
replace vo9="3" if EB=="Neither agree nor disagree"
replace vo9="4" if EB=="Somewhat agree"
replace vo9="5" if EB=="Strongly agree"
gen vo10="."
replace vo10=EC
replace vo10="1" if EC=="Strongly disagree"
replace vo10="2" if EC=="Somewhat disagree"
replace vo10="3" if EC=="Neither agree nor disagree"
replace vo10="4" if EC=="Somewhat agree"
replace vo10="5" if EC=="Strongly agree"
*Convert model variables from string to numeric
generate Race=real(race)
generate Age=real(age)
generate Education=real(education)
generate Ethnicity=real(ethnicity)
generate Income=real(income)
generate Vo1=real(vo1)
generate Vo2=real(vo2)
generate Vo3=real(vo3)
generate Vo4=real(vo4)
generate Vo5=real(vo5)
generate Vo6=real(vo6)
generate Vo7=real(vo7)
generate Vo8=real(vo8)
generate Vo9=real(vo9)
generate Vo10=real(vo10)
0 Response to No observations r(2000) with Regress and SEM
Post a Comment