source: main/adopters/nm/trunk/src/main/backend_qModules/3.0/yrrs/SurveyCrudeRateSTWIDE.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: 10.8 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=10;
38        if year=2007 then df=11;
39        if year=2009 then df=12;
40        if year=2011 then df=29;
41        if year=2013 then df=14;
42        if year=2015 then df=158;
43        if year=2017 then df=38;
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
69title3 'CODE FOR ADJUSTING FOR MULTIPLE YEARS';
70
71%macro year;
72
73        proc print data=tmp (obs=5);
74                var %cross1%
75                ?cross2? %cross2%
76                &weight. ;
77                title3 '1st 5 obs, weight before weighting by year';
78                run;
79
80        %if (%cross1% ^= year)
81                ?cross2? and (%cross2% ^= year)
82                %then %do;
83
84                proc freq data=tmp noprint;
85                        tables year / out=wgt;
86                        run;
87                        proc print data=wgt noobs; title3 'wgt: year frequency table'; run;
88
89                data wgt;
90                        set wgt;
91                        keep year wpercent;
92                        wpercent=percent/100;
93                        run;
94                        proc print data=wgt noobs; title3 'wgt: year percentages, multiply weight var by these'; run;
95
96                proc sort data=tmp; by year; run;
97                data tmp;
98                        merge wgt tmp;
99                        by year;
100                        &weight.=&weight.*wpercent;
101                        run;
102                        proc print data=tmp (obs=5);
103                                var %cross1%
104                                ?cross2? %cross2%
105                                wpercent &weight.;
106                                title3 '1st 5 obs, tmp: for submission to proc surveymeans';
107                                run;
108        %end;
109%mend year;
110%year;
111
112******************************************************************************;
113title3 'PROC SURVEYMEANS OUTPUT';
114
115?cross1? proc surveymeans data=tmp nobs sum mean stderr;
116?cross1?        var &varname. ;
117?cross1?        class &varname. ;
118?cross1?        strata stratum ;
119?cross1?        cluster psu;
120?cross1?        domain
121?cross1?          %cross1%  %surveyvar1% ;
122?cross1?        ods output statistics=stats
123?cross1?        summary=sum
124?cross1?        domain=domain ;
125?cross1?        weight &weight. ;
126?cross1? run;
127
128?cross1? ?cross2? proc surveymeans data=tmp nobs sum mean stderr;
129?cross1? ?cross2?       var &varname. ;
130?cross1? ?cross2?       class &varname. ;
131?cross1? ?cross2?       strata stratum ;
132?cross1? ?cross2?       cluster psu;
133?cross1? ?cross2?       domain
134?cross1? ?cross2?         %cross1%*%cross2%  %surveyvar1% ;
135?cross1? ?cross2?       ods output statistics=stats
136?cross1? ?cross2?         summary=sum
137?cross1? ?cross2?         domain=domain ;
138?cross1? ?cross2?       weight &weight. ;
139?cross1? ?cross2? run;
140
141proc print data=stats;  title3 'stats for overall sample ';
142proc print data=domain; title3 'domain - stats for sross var(s)';
143
144*****************************************************************;
145********* tmp1: Grab stats for dimension totals *****************;
146*****************************************************************;
147data tmp1;
148        format %cross1% %cross2% 5.;
149        set stats;
150        &varname.=input(VarLevel, 8.);
151        percent=mean;
152        %cross1% = -1;
153        ?cross2? %cross2% = -1;
154        proc print noobs;
155        title3 'tmp1: Grab state overall, add varname and percent, code crossvar(s) to -1';
156        run;
157proc 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;
158        var N;
159        output out=sampleD_Ovrl (DROP = _TYPE_ _FREQ_) sum(N)=Denom;
160        run;
161data sampleD_Ovrl;
162        set sampleD_Ovrl;
163        %cross1% = -1;
164        ?cross2? %cross2% = -1;
165        &varname. = .;
166        proc print noobs; title3 'sampleD_Ovrl, Sample Size (denominator), sum across sample N values in tmp1';
167        run;
168
169
170******************************************************************;
171********* tmp2: Grab %, SE, codes for indicator variable *********;
172******************************************************************;
173data tmp2;
174        format %cross1% %cross2% 5.;
175        set domain;
176        &varname.=input(VarLevel, 8.);
177        percent=mean;
178        %if '%cross1%' != 'year' %then %do;
179                if year_sflag<=0 then delete;
180        %end;
181        drop DomainLabel;
182        proc print noobs; title3 'tmp2: stats for cross variable(s)';
183        run;
184       
185proc summary data=tmp2; *tmp2 has stats for domain (crossby) variables;
186        var N;
187        class %cross1%
188        ?cross2? %cross2%
189        &varname. ;
190        output out=sampleD_Domain(DROP = _TYPE_ _FREQ_) sum(N)=Denom;
191        proc sort data=sampleD_Domain; by %cross1%
192        ?cross2? %cross2%
193        ;
194        proc print data=sampleD_Domain; title3 'sampleD_Domain';
195        run;
196data sampleD_Domain;
197        set sampleD_Domain;
198        if &varname. =.;
199        if %cross1% = . then delete;
200        ?cross2? if %cross2% = . then delete;
201        proc print noobs; title3 'sampleD_Domain, Sample Size (denominator), sum across sample N values values in tmp2';
202        run;
203data sampleD;
204        set sampleD_Domain sampleD_Ovrl;
205        drop &varname.;
206        run;
207        proc print noobs; title3 'sampleD, final';
208        run;
209
210**********************************************************;
211**** tmp3: concatenated total rows with domain rows ******;
212**** tmp4: Merge df values for user dataset **************;
213*****(df dataset created at top of this program **********;
214**********************************************************;
215data tmp3;
216        set tmp2 tmp1;
217        drop LowerCLMean UpperCLMean VarLevel DomainLabel VarLabel VarName Sum StdDev;
218        proc print noobs; title3 'tmp3 - concatenated total rows with domain rows';
219        run;
220
221/* Not needed for HI method
222proc sql;
223        create table tmp4 as
224        select tmp3.*, df.*
225        from tmp3, df;
226        quit;
227        proc print noobs; title3 'tmp4 - add df - sum across years (what if query is by year?)';
228        run;
229*/
230
231**********************************************************;
232****** tmp5: Grab sample size (denominator) **************;
233****** N Number of respondents with each response ********;
234****** Denom denominator in each cross by group **********;
235**********************************************************;
236proc sql;
237        create table tmp5 as
238        select tmp3.*, SampleD.*
239        from tmp3, SampleD
240        where tmp3.%cross1%=SampleD.%cross1%
241        ?cross2? and tmp3.%cross2%=SampleD.%cross2%
242        ;
243        quit;
244        proc print data=tmp5 noobs; title3 'tmp5: with Denom';
245        run;
246
247*****************************************************************;
248********** tmp6: Calculate asymmetric confidence ints ***********;
249*****************************************************************;
250data tmp6;
251        set tmp5;
252        f=log(percent)-log(1-percent); 
253        s=stderr/(percent*(1-percent));
254
255        *****old method;
256        /* alpha=0.05;
257        Lf=f+tinv(alpha/2,df)*s; 
258        Uf=f-tinv(alpha/2,df)*s;  */
259
260        *****new method;
261        Lf=f-1.96*s;
262        Uf=f+1.96*s;
263
264        lower=exp(Lf)/(1+exp(Lf));   
265        upper=exp(Uf)/(1+exp(Uf));   
266
267        if %cross1%=. then %cross1%=-1;
268        proc sort data=tmp6; by %cross1%
269                ?cross2? %cross2%
270                ;
271        proc print noobs; title3 'tmp6: Calculate asymmetric CIs';
272        run;
273
274************************************************************;
275********** tmp6: Add RSE, redflag to dataset, clean up *****;
276************************************************************;
277* redflag is the statistical stability indicator, based on  ;
278* the relative standard error (RSE, or coefficient of       ;
279* variation.                                                ;
280************************************************************;
281
282data tmp7;
283        set tmp6;
284
285        if 0<percent<.50 then RSE=(StdErr/percent);
286        if .50<=percent<1 then RSE=(StdErr/(1-percent));
287
288        redflag=put('-', $14.);
289        if rse>.3 then redflag=put('Unstable', $14.);
290        if rse>.5 then redflag=put('Very Unstable', $14.);
291        if denom=1 then redflag=put('Unstable', $14.);
292
293        if (%cross1%=-1) then %cross1%=.;
294        ?cross2? if (%cross2%=-1) then %cross2%=.;
295        drop RSE StdErr alpha f s Lf Uf ;
296
297        proc sort data=tmp7; by %cross1%
298        ?cross2? %cross2%
299        &varname.
300        ;
301        run;
302
303        proc print data=tmp7 noobs; title3 'tmp7: Add redflag to dataset, clean up';
304        run;
305
306************************************************************;
307*********** tmp: Convert values for cell suppression *******;
308************************************************************;
309data tmp;
310        set tmp7;
311        if &varname in (%spvar2% .);   *spvar2 is the indicator DIMENSION/VALUE passed in by the URL;
312        percent=100*percent;
313        lower=100*lower;
314        upper=100*upper;
315        if (0<=Denom<50) then do;
316                percent = .A;
317                lower = .A;
318                upper = .A;
319                n = .A;
320                denom = .A;
321                redflag=put('Not Reportable', $14.);
322        end;
323        proc print data=tmp noobs; title3 'final tmp: Convert values for cell suppression';
324        run;
325
326%mend;
327
Note: See TracBrowser for help on using the repository browser.