source: main/adopters/nm/trunk/src/main/backend_qModules/3.0/yrrs/SurveyCrudeRateCNTY.sas @ 19939

Last change on this file since 19939 was 19939, checked in by LoisHaggard_NM, 9 months ago

Creating subfolders for 2.3 and 3.0 qModules files

File size: 11.0 KB
Line 
1*******************************************************************************************************;
2* Program filename: SurveyCrudeRate.sas                                                                ;
3*                                                                                                      ;
4* This file is "included" for YRRS queries in .def file for crude rates. The macro variables           ;
5* (varname, weight) are also set in the .def file. The %spvar2% variable is referenced in the .def     ;
6* file. The value for %spvar2% is set in the IBIS Module.xml file. It is also a filter-exclude         ;
7* variable, which allows keeps the whole survey dataset in the analytic file, and allows the user to   ;
8* select whether to calculate the at-risk percentage (%VarLevel=1) or the not-at-risk percentage       ;
9* (%VarLevel=2).                                                                                       ;
10*                                                                                                      ;
11* Missing values have been set to '.' for all indicator and dimension variables.                       ;
12*                                                                                                      ;
13* surveyvar1 allow the variance estimate to be calculated with the entire dataset, regardless of other ;
14* dataset filters. This is appropriate according to Michael Friedrichs in Utah.                        ;
15*                                                                                                      ;
16* ageadjfilter has something to do with maintaining the entire population dataset in the results for   ;
17* the state overall.                                                                                   ;
18*******************************************************************************************************;
19OPTIONS OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL SOURCE2 nodate nonumber nocenter linesize=MAX pagesize=4000;
20
21*Get an unduplicated list of all the years in the user-filtered dataset;
22/*data one;
23        set tmp;
24        keep year %cross1%
25        ?cross2? %cross2%
26        ;
27        proc sort nodupkey out=two;
28        by year;
29        proc print; title1 ' '; title2 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'; title3 'two';
30        run;
31
32* df = # of psu unique values minus # of strata unique values ;
33data three;
34        set two;
35          if year=2001 then df=754;
36          if year=2003 then df=457;
37          if year=2005 then df=1091;
38          if year=2007 then df=727;
39          if year=2009 then df=1305;
40          if year=2011 then df=1066;
41          if year=2013 then df=609;
42          if year=2015 then df=526;
43          if year=2017 then df=597;
44        proc print; title3 'three';
45        run;
46proc summary data=three;
47        class %cross1%
48        ?cross2? %cross2%
49        ;
50        VAR df;
51        OUTPUT OUT=df1 SUM(df)=df;
52        RUN;
53        proc print data=df1 noobs; title3 'df1'; run;
54
55data df;
56        set df1;
57        where _TYPE_=0;
58        keep df;
59        proc print data=df noobs; title3 'last final df';
60        run;
61*/
62
63
64title1 ' ';
65title2 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~';
66******************************************************************************;
67%macro crudrt(varname,weight);
68
69        %if (%cross1%=cnty) or (%cross1%=region) or (%cross1%=urbanrur)
70                ?cross2? or (%cross2%=cnty) or (%cross2%=region) or (%cross2%=urbanrur)
71                %then %do;
72                %LET weight = wt_cnty;
73        %end;
74
75title3 'CODE FOR ADJUSTING FOR MULTIPLE YEARS' ;
76
77%macro year;
78
79        proc print data=tmp (obs=5);
80                var %cross1%
81                ?cross2? %cross2%
82                &weight. ;
83                title3 '1st 5 obs, weight before weighting by year';
84                run;
85
86        %if (%cross1% ^= year)
87                ?cross2? and (%cross2% ^= year)
88                %then %do;
89
90                proc freq data=tmp noprint;
91                        tables year / out=wgt;
92                        run;
93                        proc print data=wgt noobs; title3 'wgt: year frequency table'; run;
94
95                data wgt;
96                        set wgt;
97                        keep year wpercent;
98                        wpercent=percent/100;
99                        run;
100                        proc print data=wgt noobs; title3 'wgt: year percentages, multiply weight var by these'; run;
101
102                proc sort data=tmp; by year; run;
103                data tmp;
104                        merge wgt tmp;
105                        by year;
106                        &weight.=&weight.*wpercent;
107                        run;
108                        proc print data=tmp (obs=5);
109                                var %cross1%
110                                ?cross2? %cross2%
111                                wpercent &weight.;
112                                title3 '1st 5 obs, tmp: for submission to proc surveymeans';
113                                run;
114        %end;
115%mend year;
116%year;
117
118******************************************************************************;
119title3 'PROC SURVEYMEANS OUTPUT';
120
121?cross1? proc surveymeans data=tmp nobs sum mean stderr;
122?cross1?        var &varname. ;
123?cross1?        class &varname. ;
124?cross1?        strata stratum ;
125?cross1?        cluster psu;
126?cross1?        domain
127?cross1?          %cross1%  %surveyvar1% ;
128?cross1?        ods output statistics=stats
129?cross1?        summary=sum
130?cross1?        domain=domain ;
131?cross1?        weight &weight. ;
132?cross1? run;
133
134?cross1? ?cross2? proc surveymeans data=tmp nobs sum mean stderr;
135?cross1? ?cross2?       var &varname. ;
136?cross1? ?cross2?       class &varname. ;
137?cross1? ?cross2?       strata stratum ;
138?cross1? ?cross2?       cluster psu;
139?cross1? ?cross2?       domain
140?cross1? ?cross2?         %cross1%*%cross2%  %surveyvar1% ;
141?cross1? ?cross2?       ods output statistics=stats
142?cross1? ?cross2?         summary=sum
143?cross1? ?cross2?         domain=domain ;
144?cross1? ?cross2?       weight &weight. ;
145?cross1? ?cross2? run;
146
147proc print data=stats;  title3 'stats for overall sample ';
148proc print data=domain; title3 'domain - stats for sross var(s)';
149
150*****************************************************************;
151********* tmp1: Grab stats for dimension totals *****************;
152*****************************************************************;
153data tmp1;
154        format %cross1% %cross2% 5.;
155        set stats;
156        &varname.=input(VarLevel, 8.);
157        percent=mean;
158        %cross1% = -1;
159        ?cross2? %cross2% = -1;
160        proc print noobs;
161        title3 'tmp1: Grab state overall, add varname and percent, code crossvar(s) to -1';
162        run;
163proc summary data=tmp1;    *tmp1 has stats for overall total by indicator values, e.g., yes/no. Need to run this to catch records with missing on crossvars;
164        var N;
165        output out=sampleD_Ovrl (DROP = _TYPE_ _FREQ_) sum(N)=Denom;
166        run;
167data sampleD_Ovrl;
168        set sampleD_Ovrl;
169        %cross1% = -1;
170        ?cross2? %cross2% = -1;
171        &varname. = .;
172        proc print noobs; title3 'sampleD_Ovrl, Sample Size (denominator), sum across sample N values in tmp1';
173        run;
174
175
176******************************************************************;
177********* tmp2: Grab %, SE, codes for indicator variable *********;
178******************************************************************;
179data tmp2;
180        format %cross1% %cross2% 5.;
181        set domain;
182        &varname.=input(VarLevel, 8.);
183        percent=mean;
184        %if '%cross1%' != 'year' %then %do;
185                if year_sflag<=0 then delete;
186        %end;
187        drop DomainLabel;
188        proc print noobs; title3 'tmp2: stats for cross variable(s)';
189        run;
190       
191proc summary data=tmp2; *tmp2 has stats for domain (crossby) variables;
192        var N;
193        class %cross1%
194        ?cross2? %cross2%
195        &varname. ;
196        output out=sampleD_Domain(DROP = _TYPE_ _FREQ_) sum(N)=Denom;
197        proc sort data=sampleD_Domain; by %cross1%
198        ?cross2? %cross2%
199        ;
200        proc print data=sampleD_Domain; title3 'sampleD_Domain';
201        run;
202data sampleD_Domain;
203        set sampleD_Domain;
204        if &varname. =.;
205        if %cross1% = . then delete;
206        ?cross2? if %cross2% = . then delete;
207        proc print noobs; title3 'sampleD_Domain, Sample Size (denominator), sum across sample N values values in tmp2';
208        run;
209data sampleD;
210        set sampleD_Domain sampleD_Ovrl;
211        drop &varname.;
212        run;
213        proc print noobs; title3 'sampleD, final';
214        run;
215
216**********************************************************;
217**** tmp3: concatenated total rows with domain rows ******;
218**** tmp4: Merge df values for user dataset **************;
219*****(df dataset created at top of this program **********;
220**********************************************************;
221data tmp3;
222        set tmp2 tmp1;
223        drop LowerCLMean UpperCLMean VarLevel DomainLabel VarLabel VarName Sum StdDev;
224        proc print noobs; title3 'tmp3 - concatenated total rows with domain rows';
225        run;
226
227/* Not needed for HI method
228proc sql;
229        create table tmp4 as
230        select tmp3.*, df.*
231        from tmp3, df;
232        quit;
233        proc print noobs; title3 'tmp4 - add df - sum across years (what if query is by year?)';
234        run;
235*/
236
237**********************************************************;
238****** tmp5: Grab sample size (denominator) **************;
239****** N Number of respondents with each response ********;
240****** Denom denominator in each cross by group **********;
241**********************************************************;
242proc sql;
243        create table tmp5 as
244        select tmp3.*, SampleD.*
245        from tmp3, SampleD
246        where tmp3.%cross1%=SampleD.%cross1%
247        ?cross2? and tmp3.%cross2%=SampleD.%cross2%
248        ;
249        quit;
250        proc print data=tmp5 noobs; title3 'tmp5: with Denom';
251        run;
252
253*****************************************************************;
254********** tmp6: Calculate asymmetric confidence ints ***********;
255*****************************************************************;
256data tmp6;
257        set tmp5;
258        f=log(percent)-log(1-percent); 
259        s=stderr/(percent*(1-percent));
260
261        *****old method;
262        /* alpha=0.05;
263        Lf=f+tinv(alpha/2,df)*s; 
264        Uf=f-tinv(alpha/2,df)*s;  */
265
266        *****new method;
267        Lf=f-1.96*s;
268        Uf=f+1.96*s;
269
270        lower=exp(Lf)/(1+exp(Lf));   
271        upper=exp(Uf)/(1+exp(Uf));   
272
273        if %cross1%=. then %cross1%=-1;
274        proc sort data=tmp6; by %cross1%
275                ?cross2? %cross2%
276                ;
277        proc print noobs; title3 'tmp6: Calculate asymmetric CIs';
278        run;
279
280************************************************************;
281********** tmp6: Add RSE, redflag to dataset, clean up *****;
282************************************************************;
283* redflag is the statistical stability indicator, based on  ;
284* the relative standard error (RSE, or coefficient of       ;
285* variation.                                                ;
286************************************************************;
287
288data tmp7;
289        set tmp6;
290
291        if 0<percent<.50 then RSE=(StdErr/percent);
292        if .50<=percent<1 then RSE=(StdErr/(1-percent));
293
294        redflag=put('-', $14.);
295        if rse>.3 then redflag=put('Unstable', $14.);
296        if rse>.5 then redflag=put('Very Unstable', $14.);
297        if denom=1 then redflag=put('Unstable', $14.);
298
299        if (%cross1%=-1) then %cross1%=.;
300        ?cross2? if (%cross2%=-1) then %cross2%=.;
301        drop RSE StdErr alpha f s Lf Uf ;
302
303        proc sort data=tmp7; by %cross1%
304        ?cross2? %cross2%
305        &varname.
306        ;
307        run;
308
309        proc print data=tmp7 noobs; title3 'tmp7: Add redflag to dataset, clean up';
310        run;
311
312************************************************************;
313*********** tmp: Convert values for cell suppression *******;
314************************************************************;
315data tmp;
316        set tmp7;
317        if &varname in (%spvar2% .);   *spvar2 is the indicator DIMENSION/VALUE passed in by the URL;
318        percent=100*percent;
319        lower=100*lower;
320        upper=100*upper;
321        if (0<=Denom<50) then do;
322                percent = .A;
323                lower = .A;
324                upper = .A;
325                n = .A;
326                denom = .A;
327                redflag=put('Not Reportable', $14.);
328        end;
329        proc print data=tmp noobs; title3 'final tmp: Convert values for cell suppression';
330        run;
331
332%mend;
333
Note: See TracBrowser for help on using the repository browser.