*! sf36 version 2.2.2 September 21, 1999  Philip Ryan
*! after Ryan and Wolfe (sf36_aut.do and sf36_man.do)
*! countries enabled for Summary Phys and Ment Health scales: AUS US UK
*  change since last version: increased precision imputing missing values
program define sf36
version 6
#delimit ;
syntax  [if] [in], NATVER(string) [ STUB(string) CHECK
               BY(string) VARlab VALlab];
#delimit cr
set more off
marksample touse
if "`by'" != "" {
unab by: `by'
}

if ("`stub'" =="") {
local stub="SF36_"
}

if (length("`stub'") >5) {
di in r "length of variable prefix " in wh "`stub'" in r " exceeds 5"
exit
}

#delimit ;

if (("`natver'" != "AUS") & ("`natver'" != "US") &
("`natver'" != "UK") & ("`natver'" != "")) {;
di in r "national version must be "
in wh " AUS US "  in r "or" in wh " UK";
;
exit;
};

* edit above if other countries become valid;

#delimit cr

di " "
* =============================================
* if CHECK is turned on - start capture assert commands
* =============================================

if ("`check'" !="") {
sf36chk `touse' `stub' `natver'
}

* =======================================================
* making copies of original `stub'nn$ variables ==> tx_nn$
* =======================================================

#delimit ;

quietly {;

gen tx_01 = `stub'01 if `touse' ;
label var tx_01 "`stub'01  missings processed";

gen tx_02 = `stub'02 if `touse' ;
label var tx_02 "`stub'02  missings processed";

for any a b c d e f g h i j: gen tx_03X = `stub'03X if `touse' ;
for any a b c d e f g h i j:
label var tx_03X "`stub'03X   missings processed";

for any a b c d: gen tx_04X = `stub'04X if `touse' ;
for any a b c d  :
label var tx_04X "`stub'04X   missings processed";

for any a b c: gen tx_05X = `stub'05X if `touse' ;
for any a b c  :
label var tx_05X "`stub'05X   missings processed";

gen tx_06 = `stub'06 if `touse' ;
label var tx_06 "`stub'06  missings processed";

gen tx_07 = `stub'07 if `touse' ;
label var tx_07 "`stub'07  missings processed";

gen tx_08 = `stub'08 if `touse' ;
label var tx_08 "`stub'08  missings processed";

for any a b c d e f g h i : gen tx_09X = `stub'09X if `touse' ;
for any a b c d e f g h i   :
label var tx_09X "`stub'09X   missings processed";

if "`natver'" == "UK" {;
gen tx_09j = `stub'09j if `touse';
label var tx_09j "`stub'09j   missings processed";
};

if "`natver'" != "UK" {;
gen tx_10 = `stub'10 if `touse' ;
label var tx_10 "`stub'10  missings processed";

for any a b c d: gen tx_11X = `stub'11X if `touse' ;
for any a b c d  :
label var tx_11X "`stub'11X   missings processed";
};

if "`natver'" == "UK" {;
for any a b c d: gen tx_10X = `stub'10X if `touse' ;
for any a b c d  :
label var tx_10X "`stub'10X   missings processed";
};

};

#delimit cr


* ==============================================
* automatically recoding out-of-range to missing
* ==============================================

quietly {
#delimit ;

replace tx_01 = . if !(`stub'01 >0 & `stub'01 < 6) & `touse';
replace tx_02 = . if !(`stub'02 >0 & `stub'02 < 6 ) & `touse';

for any a b c d e f g h i j:
replace tx_03X = . if !(`stub'03X >0 & `stub'03X <4) & `touse';

if "`natver'" != "UK" {;
for any a b c d:
replace tx_04X = . if !(`stub'04X == 1 | `stub'04X == 2) & `touse';

for any a b c :
replace tx_05X = . if !(`stub'05X == 1 | `stub'05X == 2) & `touse';
};

if "`natver'" == "UK" {;
for any a b c d:
replace tx_04X = . if !(`stub'04X == 0 | `stub'04X == 1) & `touse';

for any a b c :
replace tx_05X = . if !(`stub'05X == 0 | `stub'05X == 1) & `touse';
};


replace tx_06 = . if !(`stub'06 >0 & `stub'06 < 6) & `touse';
replace tx_07 = . if !(`stub'07 >0 & `stub'07 < 7) & `touse';
replace tx_08 = . if !(`stub'08 >0 & `stub'08 < 6) & `touse';

for any a b c d e f g h i:
replace tx_09X = . if !(`stub'09X >0 & `stub'09X <7) & `touse';

if "`natver'" == "UK" {;
replace tx_09j = . if !(`stub'09j >0 & `stub'09j <7) & `touse';
};

if "`natver'" != "UK" {;
replace tx_10 = . if !(`stub'10 >0 & `stub'10 < 6) & `touse';

for any a b c d:
replace tx_11X = . if !(`stub'11X >0 & `stub'11X < 6) & `touse';
};

if "`natver'" == "UK" {;
for any a b c d:
replace tx_10X = . if !(`stub'10X >0 & `stub'10X < 6) & `touse';
};

};

#delimit cr

* ============================================
* begin labelling existing `stub'nn$ variables
* ============================================

#delimit ;
if "`varlab'" !="" {;

quietly {;

label var `stub'01 "general health";

label var `stub'02 "health compared to 1 year ago";

label var `stub'03a "limit vigorous activity";
label var `stub'03b "limit moderate activity";
label var `stub'03c "limit carry groceries";
label var `stub'03d "limit climb >1 flight stairs";
label var `stub'03e "limit climb 1 flight stairs";
label var `stub'03f "limit bend kneel stoop";
label var `stub'03g "limit walking 1 km";
label var `stub'03h "limit walking half km";
label var `stub'03i "limit walking 100m";
label var `stub'03j "limit bath dress self";

label var `stub'04a "cut worktime due phys hlth";
label var `stub'04b "accomplish less due phys hlth";
label var `stub'04c "limit kind work due phys hlth";
label var `stub'04d "difficulty work due phys hlth";

label var `stub'05a "cut worktime due emot prob";
label var `stub'05b "accomplish less due emot prob";
label var `stub'05c "not careful due emot prob";

label var `stub'06 "interference social activity";

lab var `stub'07 "bodily pain";

label var `stub'08 "pain interferes with work";

label var `stub'09a "feel full of life";
label var `stub'09b "very nervous person";
label var `stub'09c "nothing cheers up";
label var `stub'09d "calm and peaceful";
label var `stub'09e "lot of energy";
label var `stub'09f "felt down";
label var `stub'09g "feel worn out";
label var `stub'09h "been a happy person";
label var `stub'09i "feel tired";

if "`natver'" == "UK" {;
label var `stub'09j "time health interferes";
};

if "`natver'" != "UK" {;
label var `stub'10 "time health interferes";

label var `stub'11a "get sick easier than others";
label var `stub'11b "healthy as anyone";
label var `stub'11c "expect health to get worse";
label var `stub'11d "health is excellent";
};

if "`natver'" == "UK" {;
label var `stub'10a "get sick easier than others";
label var `stub'10b "healthy as anyone";
label var `stub'10c "expect health to get worse";
label var `stub'10d "health is excellent";
};



};
};

#delimit cr

* ==========================================
* end labelling existing `stub'nn$ variables
* ==========================================


* =========================================
* begin labelling existing `stub'nn$ values
* =========================================

#delimit ;
if "`vallab'" !="" {;

quietly {;

*** 01 ***;
if "`natver'" != "UK" {;
label def lab_01 1 "excellent" 2 "very good" 3 "good"
4 "fair" 5 "poor";
};
if "`natver'" == "UK" {;
label def lab_01 5 "excellent" 4.4 "very good" 3.4 "good"
2 "fair" 1 "poor";
};

label val `stub'01 lab_01;

*** 02 ***;
if "`natver'" != "UK" {;
label def lab_02 1 "much better" 2 "somewhat better" 3 "same"
4 "somewhat worse" 5 "much worse";
};
if "`natver'" == "UK" {;
label def lab_02 5 "much better" 4 "somewhat better" 3 "same"
2 "somewhat worse" 1 "much worse";
};
label val `stub'02 lab_02;

*** 03 ***;
label def lab_03 1 "yes a lot" 2 "yes a bit" 3 "no";
for any a b c d e f g h i j: label val `stub'03X lab_03;

*** 04 & 05 ***;
if "`natver'" != "UK" {;
label def lab_0405 1 yes 2 no;
};
if "`natver'" == "UK" {;
label def lab_0405 0 yes 1 no;
};

for any a b c d: label val `stub'04X lab_0405;
for any a b c : label val `stub'05X lab_0405;

*** 06 ***;
if "`natver'" != "UK" {;
label def lab_06 1 "nil" 2 "slight" 3 "moderate"
4 "quite" 5 "extreme";
};
if "`natver'" == "UK" {;
label def lab_06 5 "nil" 4 "slight" 3 "moderate"
2 "quite" 1 "extreme";
};
label val `stub'06 lab_06;

*** 07 ***;
if "`natver'" != "UK" {;
lab def lab_07 1 "nil" 2 "very mild" 3 "mild" 4 "moderate"
5 "severe" 6 "very severe";
};
if "`natver'" == "UK" {;
lab def lab_07 6 "nil" 5 "very mild" 4 "mild" 3 "moderate"
2 "severe" 1 "very severe";
};
lab val `stub'07 lab_07;

*** 08 ***;
if "`natver'" != "UK" {;
label def lab_08 1 "nil" 2 "a little" 3 "moderate"
4 "quite" 5 "extreme";
};
if "`natver'" == "UK" {;
label def lab_08 5 "nil" 4 "a little" 3 "moderate"
2 "quite" 1 "extreme";
};
label val `stub'08 lab_08;

*** 09 ***;
label def lab_09 1 "all" 2 "most" 3 "good bit"
4 "some" 5 "little" 6 "none";
for any a b c d e f g h i: label val `stub'09X lab_09;
if "`natver'" == "UK" {;
label val `stub'09j lab_09;
};

*** 10 11 ***;
if "`natver'" != "UK" {;
label def lab_10 1 "all" 2 "most" 3 "some" 4 "little" 5 "none";
label val `stub'10 lab_10;

label def lab_11 1 "defntly True" 2 "mostly True" 3 "Don't Know"
4 "mostly False" 5 "defntly False";
for any a b c d : label val `stub'11X lab_11;
};

if "`natver'" == "UK" {;
label def lab_10a 1 "defntly True" 2 "mostly True" 3 "Don't Know"
4 "mostly False" 5 "defntly False";
for any a c : label val `stub'10X lab_10a;
label def lab_10b 5 "defntly True" 4 "mostly True" 3 "Don't Know"
2 "mostly False" 1 "defntly False";
for any b d : label val `stub'10X lab_10b;
};


};
};

#delimit cr

* =======================================
* end labelling existing `stub'nn$ values
* =======================================



* ====================================================
* processing scale: Physical Functioning  PF  10 items
* ====================================================

#delimit ;
quietly {;

tempvar tempmis tff;

egen `tempmis' = rmiss(tx_03*) if `touse';

replace tx_03a = . if `tempmis' >5 & `touse';
replace tx_03b = . if `tempmis' >5 & `touse';
replace tx_03c = . if `tempmis' >5 & `touse';
replace tx_03d = . if `tempmis' >5 & `touse';
replace tx_03e = . if `tempmis' >5 & `touse';
replace tx_03f = . if `tempmis' >5 & `touse';
replace tx_03g = . if `tempmis' >5 & `touse';
replace tx_03h = . if `tempmis' >5 & `touse';
replace tx_03i = . if `tempmis' >5 & `touse';
replace tx_03j = . if `tempmis' >5 & `touse';

egen `tff' = rmean(tx_03*) if `touse';

replace tx_03a = `tff' if tx_03a == . & `tempmis' <6
 & `touse';
replace tx_03b = `tff' if tx_03b == . & `tempmis' <6
 & `touse';
replace tx_03c = `tff' if tx_03c == . & `tempmis' <6
 & `touse';
replace tx_03d = `tff' if tx_03d == . & `tempmis' <6
 & `touse';
replace tx_03e = `tff' if tx_03e == . & `tempmis' <6
 & `touse';
replace tx_03f = `tff' if tx_03f == . & `tempmis' <6
 & `touse';
replace tx_03g = `tff' if tx_03g == . & `tempmis' <6
 & `touse';
replace tx_03h = `tff' if tx_03h == . & `tempmis' <6
 & `touse';
replace tx_03i = `tff' if tx_03i == . & `tempmis' <6
 & `touse';
replace tx_03j = `tff' if tx_03j == . & `tempmis' <6
 & `touse';

egen PF = rsum(tx_03*) if `tempmis' <6  & `touse';
label var PF "Physical Functioning";
drop `tff' `tempmis';
};
#delimit cr


* ========================================================
* processing scale: Role Functioning Physical  RP  4 items
* ========================================================

quietly {
#delimit ;
tempvar tempmis tff;

egen `tempmis' = rmiss(tx_04*) if `touse';
replace tx_04a = . if `tempmis' >2 & `touse';
replace tx_04b = . if `tempmis' >2 & `touse';
replace tx_04c = . if `tempmis' >2 & `touse';
replace tx_04d = . if `tempmis' >2 & `touse';

egen `tff' = rmean(tx_04*) if `touse';

replace tx_04a = `tff' if tx_04a == . & `tempmis' <3
 & `touse';
replace tx_04b = `tff' if tx_04b == . & `tempmis' <3
 & `touse';
replace tx_04c = `tff' if tx_04c == . & `tempmis' <3
 & `touse';
replace tx_04d = `tff' if tx_04d == . & `tempmis' <3
 & `touse';

egen RP = rsum(tx_04*) if `tempmis' <3 & `touse';
label var RP "Role Functioning Physical";

#delimit cr
}

* =========================================================
* processing scale: Role Functioning Emotional  RE  3 items
* =========================================================

quietly {
#delimit ;
tempvar tempmis tff;

egen `tempmis' = rmiss(tx_05*) if `touse';
replace tx_05a = . if `tempmis' >1 & `touse';
replace tx_05b = . if `tempmis' >1 & `touse';
replace tx_05c = . if `tempmis' >1 & `touse';

egen `tff' = rmean(tx_05*) if `touse';
replace tx_05a = `tff' if tx_05a == . & `tempmis' <2
 & `touse';
replace tx_05b = `tff' if tx_05b == . & `tempmis' <2
 & `touse';
replace tx_05c = `tff' if tx_05c == . & `tempmis' <2
 & `touse';

egen RE = rsum(tx_05*) if `tempmis' <2 & `touse';
label var RE "Role Functioning Emotional";

#delimit cr
}

* ============================================
* processing scale: Mental Health  MH  5 items
* ============================================

quietly {
#delimit ;
tempvar tempmis tff;

for any d h: recode tx_09X 6=1 5=2 4=3 3=4 2=5 1=6 if `touse';

egen `tempmis' =
rmiss(tx_09b tx_09c tx_09d tx_09f tx_09h) if `touse';

replace tx_09b = . if `tempmis' >2 & `touse';
replace tx_09c = . if `tempmis' >2 & `touse';
replace tx_09d = . if `tempmis' >2 & `touse';
replace tx_09f = . if `tempmis' >2 & `touse';
replace tx_09h = . if `tempmis' >2 & `touse';

egen `tff' =
rmean(tx_09b tx_09c tx_09d tx_09f tx_09h) if `touse';

replace tx_09b = `tff' if tx_09b == . & `tempmis' <3
 & `touse';
replace tx_09c = `tff' if tx_09c == . & `tempmis' <3
 & `touse';
replace tx_09d = `tff' if tx_09d == . & `tempmis' <3
 & `touse';
replace tx_09f = `tff' if tx_09f == . & `tempmis' <3
 & `touse';
replace tx_09h = `tff' if tx_09h == . & `tempmis' <3
 & `touse';

egen MH =
rsum(tx_09b tx_09c tx_09d tx_09f tx_09h) if `tempmis' <3  & `touse';
label var MH "Mental Health";

#delimit cr
}

* =======================================
* processing scale: Vitality  VT  4 items
* =======================================

quietly {
#delimit ;
tempvar tempmis tff;

for any a e: recode tx_09X 6=1 5=2 4=3 3=4 2=5 1=6 if `touse';

egen `tempmis' =
rmiss(tx_09a tx_09e tx_09g tx_09i) if `touse';

replace tx_09a = . if `tempmis' >2 & `touse';
replace tx_09e = . if `tempmis' >2 & `touse';
replace tx_09g = . if `tempmis' >2 & `touse';
replace tx_09i = . if `tempmis' >2 & `touse';

egen `tff' =
rmean(tx_09a tx_09e tx_09g tx_09i) if `touse';

replace tx_09a = `tff' if tx_09a == . & `tempmis' <3
 & `touse';
replace tx_09e = `tff' if tx_09e == . & `tempmis' <3
 & `touse';
replace tx_09g = `tff' if tx_09g == . & `tempmis' <3
 & `touse';
replace tx_09i = `tff' if tx_09i == . & `tempmis' <3
 & `touse';

egen VT =
rsum(tx_09a tx_09e tx_09g tx_09i) if `tempmis' <3 & `touse';
label var VT "Vitality";

#delimit cr
}

* ==========================================
* processing scale: Bodily Pain  BP  2 items
* ==========================================

quietly {
#delimit ;
tempvar tempmis tff;

if "`natver'" != "UK" {;
* no recoding needed for UK;
replace tx_08 =6 if `stub'08 ==1 & `stub'07 == 1  & `touse';
replace tx_08 =5 if `stub'08 ==1 & (`stub'07 >= 2 & `stub'07 <=6)
 & `touse';
replace tx_08 =4 if `stub'08 ==2 & (`stub'07 >= 1 & `stub'07 <=6)
 & `touse';
replace tx_08 =3 if `stub'08 ==3 & (`stub'07 >= 1 & `stub'07 <=6)
 & `touse';
replace tx_08 =2 if `stub'08 ==4 & (`stub'07 >= 1 & `stub'07 <=6)
 & `touse';
replace tx_08 =1 if `stub'08 ==5 & (`stub'07 >= 1 & `stub'07 <=6)
 & `touse';

for num 1 2 3 4 5 \ num 6.0 4.75 3.5 2.25 1:
recode tx_08 X = Y if tx_07 == . & `touse';

recode tx_07 1 = 6.0  2 = 5.4  3 = 4.2  4 = 3.1  5 = 2.2 6 = 1.0
 if `touse';
};

* now comes code common to both UK and non-UK;

egen `tempmis' = rmiss(tx_07 tx_08) if `touse';

replace tx_07 = . if `tempmis' > 1 & `touse';
replace tx_08 = . if `tempmis' > 1 & `touse';

egen `tff' = rmean(tx_07 tx_08) if `touse';

replace tx_07 = `tff' if tx_07 == . & `tempmis' <2
 & `touse';
replace tx_08 = `tff' if tx_08 == . & `tempmis' <2
 & `touse';

egen BP = rsum(tx_07 tx_08) if `tempmis' <2 & `touse';
label var BP "Bodily Pain";

#delimit cr
}

* =============================================
* processing scale: General Health  GH  5 items
* =============================================

quietly {
#delimit ;
tempvar tempmis tff;

if "`natver'" != "UK" {;
recode tx_01  1 = 5.0  2 = 4.4  3 = 3.4  4 = 2.0  5 = 1.0
 if `touse';
for any b d: recode tx_11X 1 = 5  2 = 4  4 = 2  5 = 1
 if `touse';

egen `tempmis' = rmiss(tx_01 tx_11*) if `touse';
replace tx_01  = . if `tempmis' >2 & `touse';

replace tx_11a = . if `tempmis' > 2 & `touse';
replace tx_11b = . if `tempmis' > 2 & `touse';
replace tx_11c = . if `tempmis' > 2 & `touse';
replace tx_11d = . if `tempmis' > 2 & `touse';

egen `tff' = rmean(tx_01 tx_11*) if `touse';

replace tx_01 = `tff' if tx_01 == . & `tempmis' <3
 & `touse';

replace tx_11a = `tff' if tx_11a == . & `tempmis' <3
 & `touse';
replace tx_11b = `tff' if tx_11b == . & `tempmis' <3
 & `touse';
replace tx_11c = `tff' if tx_11c == . & `tempmis' <3
 & `touse';
replace tx_11d = `tff' if tx_11d == . & `tempmis' <3
 & `touse';

egen GH = rsum(tx_01 tx_11*) if `tempmis' <3 & `touse';
};

if "`natver'" == "UK" {;
egen `tempmis' = rmiss(tx_01 tx_10*) if `touse';
replace tx_01  = . if `tempmis' >2 & `touse';

replace tx_10a = . if `tempmis' > 2 & `touse';
replace tx_10b = . if `tempmis' > 2 & `touse';
replace tx_10c = . if `tempmis' > 2 & `touse';
replace tx_10d = . if `tempmis' > 2 & `touse';

egen `tff' = rmean(tx_01 tx_10*) if `touse';

replace tx_01 = `tff' if tx_01 == . & `tempmis' <3
 & `touse';

replace tx_10a = `tff' if tx_10a == . & `tempmis' <3
 & `touse';
replace tx_10b = `tff' if tx_10b == . & `tempmis' <3
 & `touse';
replace tx_10c = `tff' if tx_10c == . & `tempmis' <3
 & `touse';
replace tx_10d = `tff' if tx_10d == . & `tempmis' <3
 & `touse';

egen GH = rsum(tx_01 tx_10*) if `tempmis' <3 & `touse';
};


label var GH "General Health";

#delimit cr
}

* ===============================================
* processing scale: Social Functioning SF 2 items
* ===============================================

quietly {
#delimit ;
tempvar tempmis tff;

if "`natver'" != "UK" {;
recode tx_06 1 = 5  2 = 4  4 = 2  5 = 1 if `touse';

egen `tempmis' = rmiss(tx_06 tx_10) if `touse';

replace tx_06 = . if `tempmis' >1 & `touse';
replace tx_10 = . if `tempmis' >1 & `touse';

egen `tff' = rmean(tx_06 tx_10) if `touse';


replace tx_06 = `tff' if tx_06 == . & `tempmis' <2
 & `touse';
replace tx_10 = `tff' if tx_10 == . & `tempmis' <2
 & `touse';

egen SF = rsum(tx_06 tx_10) if `tempmis' <2 & `touse';
};

if "`natver'" == "UK" {;
egen `tempmis' = rmiss(tx_06 tx_09j) if `touse';

replace tx_06 = . if `tempmis' >1 & `touse';
replace tx_09j = . if `tempmis' >1 & `touse';

egen `tff' = rmean(tx_06 tx_09j) if `touse';


replace tx_06 = `tff' if tx_06 == . & `tempmis' <2
 & `touse';
replace tx_09j = `tff' if tx_09j == . & `tempmis' <2
 & `touse';

egen SF = rsum(tx_06 tx_09j) if `tempmis' <2 & `touse';
};
label var SF "Social Functioning";

#delimit cr
}

* =========================================================
* processing scale: Reported Health Transition  HT  2 items
* =========================================================

quietly {

#delimit ;
gen HT = `stub'02 if `touse';
label var HT "reported Health Transition";

#delimit cr
}


* =======================================
* transform scores for each of the scales
* =======================================


quietly {

gen PF_trans = (((PF -10)/20) * 100) if `touse'
gen GH_trans = (((GH - 5)/20) * 100) if `touse'
gen VT_trans = (((VT - 4)/20) * 100) if `touse'
gen MH_trans = (((MH - 5)/25) * 100) if `touse'

if "`natver'" != "UK" {
gen RP_trans = (((RP - 4)/4 ) * 100) if `touse'
gen RE_trans = (((RE - 3)/3 ) * 100) if `touse'
gen SF_trans = (((SF - 2)/8 ) * 100) if `touse'
gen BP_trans = (((BP - 2)/10) * 100) if `touse'
}
if "`natver'" == "UK" {
gen RP_trans = (((RP)/4 ) * 100) if `touse'
gen RE_trans = (((RE)/3 ) * 100) if `touse'
gen SF_trans = (((SF - 2)/9 ) * 100) if `touse'
gen BP_trans = (((BP - 2)/9) * 100) if `touse'
}


label var PF_trans "Physical Function-transformed"
label var RP_trans "Role Physical-transformed"
label var BP_trans "Bodily Pain-transformed"
label var GH_trans "General Health-transformed"
label var VT_trans "Vitality-transformed"
label var SF_trans "Social Function-transformed"
label var RE_trans "Emotional Role-tranformed"
label var MH_trans "Mental Health-transformed"

}

* ================================================================
* Begin section on Physical Summary Score and Mental Summary Score
* ================================================================


quietly {

tempvar pcpf pcrp pcbp pcgh pcvt pcsf pcre pcmh
tempvar mcpf mcrp mcbp mcgh mcvt mcsf mcre mcmh
tempvar nmiss

if "`natver'" == "AUS"  {

* ------------------------------------------------------------------
* Australian data from Aust Bureau Stats National Health Survey 1995
* ------------------------------------------------------------------

local PF_tmn = 83.46290
local PF_tsd = 23.22864
local PF_tpf =  0.47268
local PF_tmf = -0.24358

local RP_tmn = 80.28166
local RP_tsd = 34.83783
local RP_tpf =  0.38210
local RP_tmf = -0.13410

local BP_tmn = 76.94163
local BP_tsd = 24.83714
local BP_tpf =  0.36750
local BP_tmf = -0.12414

local GH_tmn = 71.81575
local GH_tsd = 20.35165
local GH_tpf =  0.18993
local GH_tmf =  0.05271

local VT_tmn = 64.47694
local VT_tsd = 19.77187
local VT_tpf = -0.01883
local VT_tmf =  0.27100

local SF_tmn = 85.05929
local SF_tsd = 22.29047
local SF_tpf = -0.01324
local SF_tmf =  0.26460

local RE_tmn = 83.19165
local RE_tsd = 32.15215
local RE_tpf = -0.14971
local RE_tmf =  0.35922

local MH_tmn = 75.97772
local MH_tsd = 16.96210
local MH_tpf = -0.27145
local MH_tmf =  0.48753
} * end AUS data


if "`natver'" == "US"  {

* ---------------------------------------------------------------------
* US data from SF-36 Phys and Mental Health Summary Scales Users Manual
* ---------------------------------------------------------------------

local PF_tmn = 84.5204
local PF_tsd = 22.8940
local PF_tpf =  0.42402
local PF_tmf = -0.22999

local RP_tmn = 81.19907
local RP_tsd = 33.7929
local RP_tpf =  0.35119
local RP_tmf = -0.12329

local BP_tmn = 75.49196
local BP_tsd = 23.55879
local BP_tpf =  0.31754
local BP_tmf = -0.09731

local GH_tmn = 72.21316
local GH_tsd = 20.16964
local GH_tpf =  0.24954
local GH_tmf = -0.01571

local VT_tmn = 61.05453
local VT_tsd = 20.86942
local VT_tpf =  0.02877
local VT_tmf =  0.23534

local SF_tmn = 83.59753
local SF_tsd = 22.37642
local SF_tpf = -0.00753
local SF_tmf =  0.26876

local RE_tmn = 81.29467
local RE_tsd = 33.02717
local RE_tpf = -0.19206
local RE_tmf =  0.43407

local MH_tmn = 74.84212
local MH_tsd = 18.01189
local MH_tpf = -0.22069
local MH_tmf =  0.48581
} * end US data


if "`natver'" == "UK"  {

* -------------------------------------------------------------------
* UK data from OHLS in UK Sf36 analysis and interpretation manual
*  Jenkinson et al, Oxford 1996 (and errata sheet)
* -------------------------------------------------------------------

local PF_tmn = 88.40
local PF_tsd = 17.98
local PF_tpf =  0.418
local PF_tmf = -0.213

local RP_tmn = 85.82
local RP_tsd = 29.93
local RP_tpf =  0.334
local RP_tmf = -0.087

local BP_tmn = 81.49
local BP_tsd = 21.69
local BP_tpf =  0.366
local BP_tmf = -0.125

local GH_tmn = 73.52
local GH_tsd = 19.90
local GH_tpf =  0.222
local GH_tmf =  0.036

local VT_tmn = 61.13
local VT_tsd = 19.67
local VT_tpf = -0.017
local VT_tmf =  0.286

local SF_tmn = 88.01
local SF_tsd = 19.58
local SF_tpf =  0.083
local SF_tmf =  0.201

local RE_tmn = 82.93
local RE_tsd = 31.76
local RE_tpf = -0.179
local RE_tmf =  0.394

local MH_tmn = 73.77
local MH_tsd = 17.24
local MH_tpf = -0.200
local MH_tmf =  0.444
} /* end UK data*/



gen `pcpf' = ((PF_trans - `PF_tmn')/`PF_tsd')*`PF_tpf' if `touse'
gen `pcrp' = ((RP_trans - `RP_tmn')/`RP_tsd')*`RP_tpf' if `touse'
gen `pcbp' = ((BP_trans - `BP_tmn')/`BP_tsd')*`BP_tpf' if `touse'
gen `pcgh' = ((GH_trans - `GH_tmn')/`GH_tsd')*`GH_tpf' if `touse'
gen `pcvt' = ((VT_trans - `VT_tmn')/`VT_tsd')*`VT_tpf' if `touse'
gen `pcsf' = ((SF_trans - `SF_tmn')/`SF_tsd')*`SF_tpf' if `touse'
gen `pcre' = ((RE_trans - `RE_tmn')/`RE_tsd')*`RE_tpf' if `touse'
gen `pcmh' = ((MH_trans - `MH_tmn')/`MH_tsd')*`MH_tpf' if `touse'

* calculate mc transformed scores for 8 scales

gen `mcpf' = ((PF_trans - `PF_tmn')/`PF_tsd')*`PF_tmf' if `touse'
gen `mcrp' = ((RP_trans - `RP_tmn')/`RP_tsd')*`RP_tmf' if `touse'
gen `mcbp' = ((BP_trans - `BP_tmn')/`BP_tsd')*`BP_tmf' if `touse'
gen `mcgh' = ((GH_trans - `GH_tmn')/`GH_tsd')*`GH_tmf' if `touse'
gen `mcvt' = ((VT_trans - `VT_tmn')/`VT_tsd')*`VT_tmf' if `touse'
gen `mcsf' = ((SF_trans - `SF_tmn')/`SF_tsd')*`SF_tmf' if `touse'
gen `mcre' = ((RE_trans - `RE_tmn')/`RE_tsd')*`RE_tmf' if `touse'
gen `mcmh' = ((MH_trans - `MH_tmn')/`MH_tsd')*`MH_tmf' if `touse'

* sum pc & mc scale scores to give raw factor scores

#delimit ;

egen raw_PHYS = rsum(`pcpf' `pcrp' `pcbp' `pcgh'
 `pcvt' `pcsf' `pcre' `pcmh')  if `touse';
egen raw_MENT = rsum(`mcpf' `mcrp' `mcbp' `mcgh'
 `mcvt' `mcsf' `mcre' `mcmh')  if `touse';

#delimit cr

* calculate standardised scores from raw factor scores

gen std_PHYS = 50+ (raw_PHYS*10) if `touse'
gen std_MENT = 50+ (raw_MENT*10) if `touse'


* code as missing scores derived from incomplete data

#delimit ;
egen `nmiss' = rmiss(PF_trans  RP_trans  BP_trans  GH_trans
VT_trans  SF_trans  RE_trans  MH_trans) if `touse';
#delimit cr

replace raw_PHYS = . if `nmiss' > 0 & `touse'
replace raw_MENT = . if `nmiss' > 0 & `touse'
label var raw_PHYS "Raw factor score Physical Health"
label var raw_MENT "Raw factor score Mental Health"
replace std_PHYS = . if `nmiss' > 0 & `touse'
replace std_MENT = . if `nmiss' > 0 & `touse'
label var std_PHYS "Standardised Physical Health Summary"
label var std_MENT "Standardised Mental Health Summary"

* ==============================================================
* End section on Physical Summary Score and Mental Summary Score
* ==============================================================

} * end quietly


* ===============================
* Finished!  SF36 data processed.
*      Now display results.
* ===============================

local newin `in'
if ("`newin'" == "") {
local newin "no in condition"
}
local newif `if'
if ("`newif'" == "") {
local newif "no if condition"
}

#delimit ;
di " ";
display in bl "Note";
display in bl "----";
di in ye "All new variables in the data set were constructed
under the qualifiers:";
di in gr "   if: " in wh "`newif'";
di in gr "   in: " in wh "`newin'";
di in ye "Observations on the new variables not satisfying
these criteria will be";
di in ye "set to missing. Observations on original " in wh
"`stub'" in gr "nn$" in ye " variables are unchanged.";
di in ye "A" in wh " note" in ye " to this effect has been
written to the data in memory.";
if "`by'" != "" {;
di in ye "Summary statistics are given for all data combined
and then for groups";
di "defined by variable(s): " in wh "`by'" in ye ".";
sort `by';
};
#delimit cr

di " "
di " "
di in bl "Raw scale statistics"
di in bl "--------------------"
if "`by'"!= ""{
di " "
di in bl "-> `by'=     combined data"
}
summ PF RP BP GH VT SF RE MH HT if `touse'
di " "
if "`by'" !=""{
by `by': summ PF RP BP GH VT SF RE MH HT if `touse'
}
di " "
di in bl "Transformed scale statistics"
di in bl "----------------------------"
if "`by'"!= ""{
di " "
di in bl "-> `by'=     combined data"
}
summ PF_tr RP_tr BP_tr GH_tr VT_tr SF_tr RE_tr MH_tr if `touse'
di " "
if "`by'" !=""{
by `by': summ PF_tr RP_tr BP_tr GH_tr VT_tr SF_tr RE_tr MH_tr if `touse'
}
di " "

#delimit ;

di in bl "Summary PHYSICAL & MENTAL HEALTH scale statistics ("
in wh "`natver'" in bl ")";
di in bl "    raw factor scores and standardised scores";
di in bl "---------------------------------------------------"
"----";
if "`by'"!= ""{;
di " ";
di in bl "-> `by'=     combined data";
};
summ raw_PHYS raw_MENT std_PHYS std_MENT if `touse';
if "`by'" !=""{;
by `by': summ raw_PHYS raw_MENT std_PHYS std_MENT if `touse';
};
di " ";
#delimit cr

note: new variables generated under if: [`newif'], and in: [`newin']
set more on

end



****************************
program define sf36chk
args touse stub natver
set more off
local exitflg=0
gen obsnum = _n
#delimit ;

************************************** stub01;
di _dup(20) "_";
di in gr "Range checking `stub'01 ......";
capture assert (`stub'01 >0 & `stub'01 < 6)  | `stub'01 ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`stub'01";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `stub'01 if (`stub'01 <= 0|`stub'01 >= 6) & `stub'01 <. & `touse',
noobs;
local exitflg=1;
};
else {;
di in wh "`stub'01 ..... OK";
di " ";
};

************************************** stub02;
di " ";
di _dup(20) "_";
di in gr "Range checking `stub'02......";
capture assert (`stub'02 >0 & `stub'02 <6 )  | `stub'02 ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`stub'02";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `stub'02 if (`stub'02 <= 0|`stub'02 >= 6) & `stub'02 <. & `touse',
noobs;
local exitflg=1;
};
else {;
di in wh "`stub'02 ...... OK";
di " ";
};


************************************** stub03 (a b c d e f g h i j);
di " ";
di _dup(20) "_";
unab Q3: `stub'03*;
tokenize `Q3';

while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' >0 & `1' <4)| `1' == . if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' <= 0) | (`1' >= 4)) & (`1' < .) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};


************************************* stub04 (a b c d);
di " ";
unab Q4: `stub'04*;
tokenize `Q4';

if "`natver'" != "UK" {;
while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' == 1 | `1' == 2|`1' == .) if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' < 1) | (`1' > 2 & `1' <.)) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};
};

if "`natver'" == "UK" {;
while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' == 0 | `1' == 1|`1' == .) if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' < 0) | (`1' > 1 & `1' <.)) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};
};

************************************** stub05 (a b c);
di " ";
unab Q5: `stub'05*;
tokenize `Q5';

if "`natver'" != "UK" {;
while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' == 1 | `1' == 2|`1' == .) if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' < 1) | (`1' > 2 & `1' <.)) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};
};


if "`natver'" == "UK" {;
while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' == 0 | `1' == 1|`1' == .) if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' < 0) | (`1' > 1 & `1' <.)) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};
};


************************************** stub06;
disp in gr "Range checking `stub'06 ......";
capture assert (`stub'06 >0 & `stub'06 < 6) | `stub'06 ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`stub'06";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `stub'06 if ((`stub'06 <= 0) | (`stub'06 >= 6)) & (`stub'06 <.)
& `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`stub'06 ...... OK";
};
di " ";
di _dup(20) "_";


************************************* stub07;
disp in gr "Range checking `stub'07 ......";
capture assert (`stub'07 >0 & `stub'07 < 7)  | `stub'07 ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`stub'07";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `stub'07 if ((`stub'07 <= 0) | (`stub'07 >= 7)) & (`stub'07 <.)
& `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`stub'07 ...... OK";
};
di " ";
di _dup(20) "_";


************************************** stub08;
disp in gr "Range checking `stub'08 ......";
capture assert (`stub'08 >0 & `stub'08 < 6)  | `stub'08 ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`stub'08";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `stub'08 if ((`stub'08 <= 0) | (`stub'08 >= 6)) & (`stub'08 <.)
& `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`stub'08 ...... OK";
};
di " ";
di _dup(20) "_";


************************************** stub09 (a b c d e f g h i);
unab Q9: `stub'09*;
tokenize `Q9';

while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' >0 & `1' <7)| `1' == . if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' <= 0) | (`1' >= 7)) & (`1' < .) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};


************************************** stub10;
if "`natver'" != "UK" {;
disp in gr "Range checking `stub'10 ......";
capture assert (`stub'10 >0 & `stub'10 < 6)  | `stub'10 ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`stub'10";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `stub'10 if ((`stub'10 <= 0) | (`stub'10 >= 6)) & (`stub'10 < .)
 & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`stub'10 ...... OK";
};
di " ";
di _dup(20) "_";
};

if "`natver'" == "UK" {;
* stub10 (a b c d);
di " ";
unab Q10: `stub'10*;
tokenize `Q10';

while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert ((`1' > 0) & (`1' < 6)) | (`1' == .) if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' <= 0) | (`1' >= 6)) & (`1' <.) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};
};
************************************** stub11;
if "`natver'" != "UK" {;
* stub11 (a b c d);

unab Q11: `stub'11*;
tokenize `Q11';

while "`1'" != "" {;
disp in gr "Range checking `1' ......";
capture assert (`1' > 0 & `1' < 6) | `1' ==. if `touse' ;
if _rc != 0 {;
di in re "range check failed for " in wh "`1'";
di in re "the following observations (current order in data set) should be
checked:";
li obsnum `1' if ((`1' <= 0) | (`1' >= 6)) & (`1' <.) & `touse', noobs;
local exitflg=1;
};
else {;
di in wh "`1' ...... OK";
};
di " ";
di _dup(20) "_";
macro shift;
};
};

drop obsnum;
di " ";
if `exitflg' {;
di in re "  range checks failed......";
di in re "  review results of checking procedure above, then";
di in re "  edit data and resubmit";
#delimit cr
exit 3000
}
end