source: main/adopters/hi/trunk/src/main/backend_qModules/3.0/qModules30/brfss/SurveyCrudeRate.sas @ 24986

Last change on this file since 24986 was 24986, checked in by LoisHaggard_STG, 4 months ago

HI 3.0 backend/qModules30 updated BRFSS, YRBS and YTS value attribute names in .SAS files to use correct value attributes for "Stable" for each survey.

File size: 11.7 KB
Line 
1**********************************************************************;
2* Program filename: TotalCrudeSurv.sas                                ;
3*                                                                     ;
4* This file is "included" in each BRFSS indicator .def file for crude ;
5* rates. The macro variables (varname, weight) are also set in the    ;
6* .def file. The %FocusLevel% variable is set in the IBIS Module.xml  ;
7* file as a filter exclude variable. This allows the user to select   ;
8* whether to calculate the at-risk percentage (%VarLevel=1) or the    ;
9* not-at-risk percentage (%VarLevel=2).                               ;
10*                                                                     ;
11* Missing set to . for all indicator and dimension vars               ;
12*                                                                     ;
13**********************************************************************;
14
15OPTIONS MPRINT MLOGIC MLOGICNEST SYMBOLGEN SPOOL SOURCE2 SUMSIZE=138M PAGESIZE=4000 LINESIZE=MAX;
16
17**********************************************************************************************;
18* CODE FOR ADJUSTING FOR MULTIPLE YEARS. ONLY NEEDED IF PRESENTING POPULATION COUNT ESTIMATES.;
19* If cross1 and cross2 are not equal to year, then _llcpwt is divided by the number of years  ;
20* the user selects                                                                            ;
21**********************************************************************************************;
22 
23/* proc print data=tmp (obs=10); run; */
24
25proc freq data=tmp;
26   tables year / out=yrfreq noprint;
27   run;
28   proc print data=yrfreq;
29        title1 ' '; title2 'yrfreq';
30        run;
31 
32 proc summary data=yrfreq;
33        class Year;
34        output out=yrnum n(year)=nyears;
35           proc print data=yrnum;
36                title1 ' '; title2 'yrnum';
37        run;
38
39data yrnum; set yrnum;
40        if year=.;
41        keep year nyears;
42           proc print data=yrnum;
43        title1 ' '; title2 'yrnum only .';
44run;   
45       
46proc sort data=tmp; by year; run;
47
48        proc sql;
49                create table tmpyrwgt as
50                select tmp.*, yrnum.nyears
51                from tmp, yrnum
52        quit;
53      proc print data=tmpyrwgt (obs=10);
54            title1 ' '; title2 'tmpyrwgt';
55          run;
56
57data tmp;
58     set tmpyrwgt;
59         if upcase(%cross1%) ^=YEAR
60         ?cross2? and upcase(%cross2%) ^=YEAR
61         then
62     _llcpwt=_llcpwt/nyears;
63   run;
64   
65/*proc print data=tmp (obs=10);
66        title1 ' '; title2 'checktmp .'; run;
67*/
68 
69%macro crudrt(varname,weight,FocusLevel);
70?cross1? proc surveymeans data=tmp nobs sum mean stderr;
71?cross1?  var &varname. ;
72?cross1?   class &varname. ;
73
74?cross1?   strata STSTR;
75?cross1?   domain
76?cross1? %cross1%
77?cross1? %surveyvar1%
78?cross1? ;
79?cross1?   ods output statistics=stats
80?cross1?        summary=sum
81?cross1?        domain=domain ;
82?cross1?   weight &weight. ;
83?cross1? run;
84
85?cross1? ?cross2? proc surveymeans data=tmp nobs sum mean stderr;
86?cross1? ?cross2?   var &varname. ;
87?cross1? ?cross2?   class &varname. ;
88
89?cross1? ?cross2?   strata STSTR;
90?cross1? ?cross2?  domain
91?cross1? ?cross2? %cross1%*%cross2%
92?cross1? ?cross2? %surveyvar1%
93?cross1? ?cross2? ;
94?cross1? ?cross2?   ods output statistics=stats
95?cross1? ?cross2?       summary=sum
96?cross1? ?cross2?       domain=domain ;
97?cross1? ?cross2?   weight &weight. ;
98?cross1? ?cross2? run;
99
100*****************************************************************;
101********* tmp1: Grab stats for dimension totals *****************;
102*********(NOT SURE WE NEED THIS FOR CRUDE RATES)*****************;
103*****************************************************************;
104proc print data=stats; title1 ' '; title2 'stats';
105
106data tmp1;
107  set stats;
108  &varname.=input(VarLevel, $54.);
109  trow1 = 1;
110  ?cross2? trow2 = 1;
111  drop VarLevel VarName VarLabel StdDev;
112  proc print;
113        title1 '===================================================================';
114        title2 'tmp1: Grab percentage for Hawaii overall';
115run;
116
117******************************************************************;
118********* tmp2: Grab %, SE, codes for indicator variable *********;
119******************************************************************;
120proc print data=domain; title2 'domain';
121
122data tmp2;
123  set domain;
124  &varname.=input(VarLevel, $54.);
125  drop LowerCLMean UpperCLMean VarLevel DomainLabel VarLabel VarName StdDev;
126/* this code has never worked, originally there was a ! (logical OR) operator instead of the ^=
127so the test was never true, fixed code to make test work, but that actually broke the output
128since it has never worked, am commenting it out 
129  %if %UPCASE(%cross1%) ^= YEAR %then %do;
130    if year_sflag<=0  then delete;
131  %end;
132*/
133  proc print; title2 'tmp2: Grab %, SE, codes for indicator variable';
134run;
135
136****************************************************************;
137********** tmp3: Grab sample size (denominator) ****************;
138*** N (sas var), ouput in domain dataset by proc surveymeans ***;
139*** is number of records with valid data for varname          **;
140****************************************************************;
141proc summary data=tmp2;
142  var N;
143  class %cross1%
144  ?cross2? %cross2%
145  &varname.
146  ;
147  output out=sampleN sum(N)=;
148  proc sort data=SampleN; by %cross1%
149  ?cross2? %cross2%
150  ;                     * sum(N)=, sum the values of N across all combinations of cross1, cross2 and varname;
151                        *         gets subtotals by each class var, and a grand total across all values of all class variables;
152  proc print; title2 'sampleN - (Unweighted number of folks who answered either y or n) Number of Records for cell suppression';
153  run;
154data tmp3;
155  format trow1 trow2 1.0;
156  set sampleN;
157  if &varname. ='';
158    sampleN=N;
159  /*vtypetst=vtype(%cross1%);*/
160  drop  _TYPE_ _FREQ_ ;
161  /*Var trow1=1 identified total row(s) for cross1.
162    Var trow2=1 identified total row(s) for cross2.
163        Var trow1=1 AND trow2=1 identified grand total row. */
164 if (vtype(%cross1%)="C" and %cross1%='') then trow1 = 1;
165 if (vtype(%cross1%)="N" and %cross1%=.) then trow1 = 1;
166 ?cross2? if (vtype(%cross2%)="C" and %cross2%='') then trow2 = 1;
167 ?cross2? if (vtype(%cross2%)="N" and %cross2%=.) then trow2 = 1;
168
169  proc print; title2 'tmp3, Sample Size (denominator)';
170run;
171
172*****************************************************************;
173********** tmp4: Calculate asymmetric confidence ints ***********;
174*****************************************************************;
175data tmp4;
176  set tmp2 tmp1;
177        if mean >0 then do;
178                f=log(mean)-log(1-mean); 
179                s=stderr/(mean*(1-mean));
180                Lf=f-1.96*s; 
181                Uf=f+1.96*s; 
182                percent=mean*100;
183          lower=(exp(Lf)/(1+exp(Lf)))*100;   
184          upper=(exp(Uf)/(1+exp(Uf)))*100;
185  end;
186  drop f s Lf Uf VarName;
187  proc sort data=tmp4; by %cross1%
188        ?cross2? %cross2%
189        ;
190  proc print; title2 'tmp4: Calculate asymmetric CIs';
191run;
192
193******************************************************************;
194********* tmp5: Add sample size field to every record ************;
195********* this is only for cell suppression           ************;
196******  N in output is unweighted numerator               ********;
197****  Samplen in output is the unweighted denominator        *****;
198******************************************************************;
199
200data tmp5; *Add Samplen field to each record;
201  merge tmp3 tmp4 ;
202  by %cross1%
203  ?cross2? %cross2%
204  ;
205  proc print; title2 'tmp5: Add sample size (Samplen) field to every record';
206run;
207
208******************************************************************;
209********** tmp6: Add wgtsum field to every record ****************;
210***********Used by Hawaii, they want estimated number in *********;
211***   pop having that category/repsonse, not unweighted samplen **;
212******************************************************************;
213proc summary data=tmp5;
214  var sum;
215  class %cross1%
216  ?cross2? %cross2%
217  &varname.
218  ;
219  output out=wgtsum sum(sum)=wgtsum;
220  proc sort data=wgtsum; by %cross1%
221  ?cross2? %cross2%
222  ;
223  proc print; title2 'wgtsum - estimated population category in that category or response';
224  run;
225  data wgtdata;
226        set wgtsum;
227         if &varname ='';
228  proc print; title2 'wgtdata: Total rows of wgtsum dataset - estimated number in that category or response';
229        run;
230       
231  proc sort data=tmp5;
232        by %cross1%
233        ?cross2? %cross2%
234        ;
235        run;
236  proc sort data=wgtdata;
237        by %cross1%
238        ?cross2? %cross2%
239        ;
240        run;
241
242data tmp6; *Add wgtsum field (pop estimates) to each record by merging wgtdata and tmp5;
243  merge wgtdata tmp5 ;
244  by %cross1%
245  ?cross2? %cross2%
246  ;
247  drop _TYPE_ _FREQ_ ;
248  proc print; title2 'tmp6: Add wgtsum field (pop estimates) to each record by merging wgtdata and tmp5';
249run;
250
251************************************************************;
252********** tmp7: Add RSE, redflag to dataset, clean up *****;
253************************************************************;
254* redflag is the statistical stability indicator, based on  ;
255* the relative standard error (RSE, or coefficient of       ;
256* variation.                                                ;
257************************************************************;
258
259data tmp7;
260  set tmp6;
261
262  if 0<mean<.50 then RSE=(StdErr/mean);
263  if .50<=mean<1 then RSE=(StdErr/(1-mean));
264 
265  /* %%% Need value attribute in DB for Stable, use VALUE_ATTRIBUTE_NAME value here */
266  redflag=put('BRFSSStable', $16.);
267 /**********************************************************************************************************
268  * Not sure we are using this, just commenting out, and keeping it here;
269  *   If we do use this we will have to change logic way below where we Convert values for cell suppression;
270  if rse>.3 then redflag=put('Unstable', $16.);
271  if rse>.5 then redflag=put('VeryUnstable', $16.);
272  ***********************************************************************************************************/
273  /* may want to comment this out at some future date */
274  /* %%% Need value attribute in DB for No Variance, use VALUE_ATTRIBUTE_NAME value here */
275  if mean in (0 1) then redflag=put('NoVariance', $16.);
276
277  if trow1=1 then %cross1%='.';
278  ?cross2? if trow2=1 then %cross2%='.';
279
280  /* Since these are esimates HI wants to round to 100's and sum is weighted numerator */
281 
282   finalsum=ROUND(sum,100);
283   
284   /* had this as  If 0<sum<50 then finalsum=50;, but in reading emails from around 3/18/2019
285    Katherine want this to be if weighted count is less than 50 show 50, because even if no one
286        responded yes to th e particular indicator question, that does not mean no one in population, just sample
287  */
288    If sum<50 then finalsum=50;
289   
290  wgtsum=ROUND(wgtsum,100);
291
292 
293  proc sort data=tmp7; by %cross1%
294  ?cross2? %cross2%
295  &varname.
296  ;
297  run;
298
299proc print data=tmp7; title2 'tmp7: Compute RSE, Add redflag to dataset, delete Unknowns, Set total rows to ., remove other total rows, round num and denom';
300run;
301
302************************************************************;
303*********** tmp: Convert values for cell suppression *******;
304************************************************************;
305data tmp;
306  set tmp7;
307  if &varname = ('%spvar2%'); /*This is the value for the indicator dimension passed in by the URL.;*/
308  if (SampleN<50) OR  (rse >.3) then do;        /* Hawaii cell supression Rule  SampleN (unweighted number of folks who answered either y or n)*/
309
310        percent = .A;   * percent who answered yes or no;
311        lower = .A;             * lower confidence interval;
312        upper = .A;             * upper confidence interval;
313        finalsum = .A;  * pop estimated weighted numerator;
314        wgtsum = .A;    * pop estimated weighted denominator;
315  /* %%% Need value attribute in DB for Not Reportable, use VALUE_ATTRIBUTE_NAME value here */
316        redflag=put('BRFSSSuppressed', $16.);
317  end;
318 
319  proc print data=tmp; title2 'final tmp: Convert values for cell suppression';
320run;
321
322%mend;
323
Note: See TracBrowser for help on using the repository browser.