{Application 'POPSTANEDITS' logic file generated by CSPro}
PROC GLOBAL
numeric numIndividuals = 0;

// Hot deck array for imputing marital status
array hdMaritalStatusFromAgeSex(P04_AGE_MS_HD_VS6, P03_SEX_VS1) save;

// Hot deck array for imputing born alive
array hdBornAliveFromAgeHighestGrade(P04_AGE_HD_BA_VS7, P10_HIGH_GR_HD_BA_VS2) save;

PROC CENSUS_DICTIONARY_FF
preproc
write("PROV,DIST,VILLAGE,EA,LINE,MSG");

PROC LINE
// Count person records for denom
inc(numIndividuals);

PROC P05_MS

if invalueset($) then
	// Valid value add to hot deck
	putdeck(hdMaritalStatusFromAgeSex, $, P04_AGE, P03_SEX);
else
	// Invalid value, impute new value
	errmsg("Invalid marital status %d", $) denom = numIndividuals;
	write("%v, %v, %v, %v, %d, Invalid marital status",
			PROVINCE, DISTRICT, VILLAGE, EA, LINE);
			 
	// 1) If the individual is 10 years old or under marital status = never married
	// 2) If the relationship is spouse marital status = married
	// 3) If the relationship is head and there is at least one spouse in the 
	//	  household then marital status = married
	// 4) Otherwise use hot deck on age and sex
	
	if P04_AGE <= 10 then
		impute($, 5); // never married
	elseif P02_REL = 2 then
		impute($, 1); // married
	elseif P02_REL = 1 and count(PERSON where P02_REL = 2) >= 1 then
		impute($, 1); // married
	else
		// Impute value from hot deck
		impute($, getdeck(hdMaritalStatusFromAgeSex, P04_AGE, P03_SEX));
	endif;
		
endif;

PROC P11_LITERACY

if not invalueset($) then
	
	errmsg("P11: literacy is invalid. Person %d, literacy %d",
		curocc(), $);
		
	// If age is less than 5, set literacy to blank (question does not apply for under 5)
	// If highest grade completed (P10_HIGH_GR) is greater than or equal to 5 (primary school) set
	// literacy to literate
	// If highest grade completed is less than 5, set literacy to illiterate
	if P04_AGE < 5 then
		impute($, notappl);
	elseif P10_HIGH_GR >= 5 then
		impute($, 1); // literate
	else
		impute($, 2); // illiterate
	endif;
endif;
PROC P18_BORN


// Impute valid values for children born alive using the following rules:
// 1) For males, set children born alive to blank (notappl)
// 2) For females under 12, set children born alive to blank (notappl)
// 3) For all other invalid or unreported values use a hot deck to impute the value. The hot deck should
// be based on age and highest grade completed. Make appropriate value sets of both to use for the
// hot deck and verify that the hot deck is working correctly using saved array viewer.
if P03_SEX = 2 and P04_AGE >= 12 then

	if invalueset($) and $ <> 99 then
		putdeck(hdBornAliveFromAgeHighestGrade, $, P04_AGE, P10_HIGH_GR);
	else
		impute($, getdeck(hdBornAliveFromAgeHighestGrade, P04_AGE, P10_HIGH_GR));
	endif;
	
else
	// Male or woman under 12, must be blank
	if $ <> notappl then
		impute($, notappl);
	endif;
endif;

