***************************************************************************** * * INSTITUTIONAL VARIANCE * * DATE: March 7, 1996 * by: Chris Mohl * Modified: May 11, 2000 * by: Sylvain Nadon * * * This program computes variances for the institutional component of the * NPHS. It uses a two-stage sampling with replacement method. * There is one macro for computing totals and another for computing ratios. * Up to 99 totals or ratios can be calculated at any time. * * ***************************************************************************** * Only the first three data steps need to be modified by the user.; * STEP 1: * Read in the data file. Define the variables that you want a variance for. * For totals name the variables tot1-totn where n is the number of * variables for which estimates are required. Similarly for ratios, define * num1-numm and denom1-denomm as the numerator and denominator of each of * the m ratios. * * Keep the weight, stratum, institution number, a couple of indicator * variables and tot,num and denom variables.; data person; %let datafid='n:\dev\nadosyl\institut\cycle2\variance\pumf\inst.txt'; /* these name will change depending */ %include 'n:\dev\nadosyl\institut\cycle2\variance\pumf\inst_in.sas' ; /* on the selected directory */ * Missing values (please refer to the data dictionary for values to be treated as missing).; if dhi6gage=99 then dhi6gage=.; if ssi6g10 =99 then ssi6g10 =.; if sdi6dlng=99 then sdi6dlng=.; * Total population. ; if (wti6wght>0) then tot1=1; else tot1=0; * Population 65 years old and older.; if (5<=dhi6gage<=10) then tot2=1; else if (1<=dhi6gage<=4 ) then tot2=0; * Women 80 years old and older. ; if (8<=dhi6gage<=10 and dhi6_sex=2) then tot3=1; else if (1<=dhi6gage<=7 and dhi6_sex=2) or (dhi6_sex=1) then tot3=0; * 65 years old and older and close to staff members. ; if (0dimdenom then do; put 'ERROR: More numerator variables ' dimnum ' have been defined than denominator variables ' dimdenom 'for ratios'; put 'ERROR: Number of ratios to be calculated equal to number of denominator variables ' dimdenom; dimnum=dimdenom; end; else if dimdenom>dimnum then do; put 'ERROR: More denominator variables ' dimdenom ' have been defined than numerator variables ' dimnum ' for ratios'; put 'ERROR: Number of ratios to be calculated equal to number of numerator variables ' dimnum; dimdenom=dimnum; end; call symput ('numtot',left(dimtot)); call symput ('numnum',left(dimnum)); call symput ('numden',left(dimdenom)); %mend; * STEP 4: * Macro totals calculates the total and variance of the variables defined earlier.; %macro totals; proc sort data=person; by stratum instno; data insts; set person; by stratum instno; if first.instno; * Count the number of institutions in each stratum; proc summary data=insts nway; class canada stratum; var one; output out=numinst sum=numinst; * Calculate the weighted total for each variable at the stratum level.; proc summary data=person nway; class canada stratum; var tot1-tot&numtot; weight wti6wght; output out=strtot sum=strtot1-strtot&numtot; * Calculate the weighted total for each variable at the institution level.; proc summary data=person nway; class canada stratum instno; var tot1-tot&numtot; weight wti6wght; output out=instot sum=instot1-instot&numtot; * Merge the two totals together and come up with a term that accounts for the * contribution to the variance for each institution.; data mergeest; merge instot(in=in1) strtot(in=in2) numinst(in=in3); by canada stratum; if in1 and in2 and in3; array instot{*} instot1-instot&numtot; array strtot{*} strtot1-strtot&numtot; array term{*} term1-term&numtot; %do i=1 %to &numtot; if numinst=1 then term{&i}=0; else term{&i} = (((instot{&i}*numinst) - strtot{&i}) ** 2) / (numinst*(numinst-1)); %end; * Sum the variance terms up to the Canada level.; proc summary data=mergeest nway; class canada; var term1-term&numtot; output out=variance sum=varian1-varian&numtot; * Sum the weighted variables up to the Canada level.; proc summary data=person nway; class canada; var tot1-tot&numtot; weight wti6wght; output out=cantot sum=cantot1-cantot&numtot; * Output the totals, variances and CVs for each variable, one variable * per line.; data cvs (keep=totdesc variance total cv); merge variance(in=in1) cantot(in=in2); by canada; if in1 and in2; array varian{*} varian1-varian&numtot; array cantot{*} cantot1-cantot&numtot; array cvtot{*} cvtot1-cvtot&numtot; %do i=1 %to &numtot; if cantot{&i}=0 then cvtot{&i}=0; else cvtot{&i} = sqrt(varian{&i}) / cantot{&i} * 100; totdesc=&i; variance=varian{&i}; total=cantot{&i}; cv=cvtot{&i}; output; %end; * Print the results.; proc print split='*'; title1 'Example for the documentation of the 1996-97 PUMF file'; title2 'Estimates, Variances and CVs of Totals'; label totdesc='DESCRIPTION'; var totdesc total variance cv; format totdesc totfrmt. total 10.2 variance 15.2 cv 10.2; %mend; * Macro rates calculates the ratio and variance of the variables defined earlier.; %macro rates; proc sort data=person; by stratum instno; data insts; set person; by stratum instno; if first.instno; * Count the number of institutions in each stratum.; proc summary data=insts nway; class canada stratum; var one; output out=numinst sum=numinst; * Calculate the weighted total for the numerator and denominator for * each variable at the stratum level.; proc summary data=person nway; class canada stratum; var num1-num&numnum denom1-denom&numden; weight wti6wght; output out=strtot sum=strnum1-strnum&numnum strden1-strden&numden; * Calculate the weighted total for the numerator and denominator for * each variable at the institution level.; proc summary data=person nway; class canada stratum instno; var num1-num&numnum denom1-denom&numden; weight wti6wght; output out=instot sum=insnum1-insnum&numnum insden1-insden&numden; * Merge the two totals together and come up with a term that accounts for the * contribution to the variance for each institution.; data mergeest; merge instot(in=in1) strtot(in=in2) numinst(in=in3); by canada stratum; if in1 and in2 and in3; array insnum{*} insnum1-insnum&numnum; array insden{*} insden1-insden&numden; array strnum{*} strnum1-strnum&numnum; array strden{*} strden1-strden&numden; array term{*} term1-term&numnum; %do i=1 %to &numnum; if numinst=1 then term{&i}=0; else if strden{&i}>0 then term{&i} = (((insnum{&i}*numinst) - (strnum{&i}/strden{&i})*insden{&i}*numinst) ** 2) / (numinst*(numinst-1)); else term{&i}=0; %end; * Sum the variance terms up to the Canada level.; proc summary data=mergeest nway; class canada; var term1-term&numnum; output out=variance sum=varian1-varian&numnum; * Sum the weighted numerators and denominators up to the Canada level.; proc summary data=person nway; class canada; var num1-num&numnum denom1-denom&numden; weight wti6wght; output out=canrat sum=cannum1-cannum&numnum canden1-canden&numden; * Output the ratios, variances and CVs for each variable, one variable * per line.; data cvs (keep=ratdesc variance ratio cv); merge variance(in=in1) canrat(in=in2); by canada; if in1 and in2; array varian{*} varian1-varian&numnum; array cannum{*} cannum1-cannum&numnum; array canden{*} canden1-canden&numden; array cvtot{*} cvtot1-cvtot&numtot; array canrat{*} canrat1-canrat&numnum; array cvrat{*} cvrat1-cvrat&numnum; %do i=1 %to &numnum; if canden{&i}=0 then canrat{&i} = 0; else canrat{&i} = cannum{&i}/canden{&i}; if canden{&i}=0 or cannum{&i}= 0 then cvrat{&i}=0; else cvrat{&i} = sqrt(varian{&i}/canden{&i}**2) / canrat{&i} * 100; ratdesc=&i; variance=varian{&i}/canden{&i}**2; ratio=canrat{&i}; cv=cvrat{&i}; output; %end; run; * Print the results.; proc print split='*'; title1 'Example for the documentation of the 1996-97 PUMF file'; title2 'Estimates, Variances and CVs of ratios'; label ratdesc='DESCRIPTION'; var ratdesc ratio variance cv; format ratdesc ratfrmt. ratio 15.5 variance 15.8 cv 10.2; run; %mend; * Count always has to be called. But totals or rates may be commented out * by surrounding the appropriate statement with /* */ * if either totals or ratios are not being calculated.; %count; %totals; %rates; run;