Date: Mon, 20 Jul 2009 05:30:48 -0700 maryann "SAS(r) Discussion" maryann http://groups.google.com Re: Row calculation in a dataset To: sas-l@uga.edu text/plain; charset=ISO-8859-1

On Jul 18, 5:17 pm, muthia.kachira...@GMAIL.COM (Muthia Kachirayan) wrote: > On Sat, Jul 18, 2009 at 3:40 PM, maryann <jiangqiyangf...@gmail.com> wrote: > > Dear SAS experts: > > > I have a dataset below: > > > cnt1 cnt2 cnt3 cnt4 cnt5 cnt6 cnt7 cnt8 > > 156 100 98 67 78 56 35 12 > > 187 123 100 87 67 54 43 13 > > 198 121 111 89 78 66 44 10 > > > The first row (record) is observed frequency. > > The second and third rows are expected frequencies from 2 model > > fitting. > > How can I get the Chi-squared calculated as (observed-expected)^2/ > > Expected, without using proc transpose? > > Specifically, what I wanted to learn is that how can I refer to a > > specific row in a dataset and use the values for calculation. > > > Thanks! > > maryann, > > Here is a datastep. When _N_ = 1 then observed frequencies are saved into a > temporary array, N[ ]. When _N_ = 2 and 3 the CHISQ is computed using (O - > E)**2/E using expected values saved into the array, E[ ]. > data have; > input cnt1 cnt2 cnt3 cnt4 cnt5 cnt6 cnt7 cnt8; > cards; > 156 100 98 67 78 56 35 12 > 187 123 100 87 67 54 43 13 > 198 121 111 89 78 66 44 10 > ; > run; > data need; > set have; > array n[8] _temporary_; > array e[*] cnt1 - cnt8; > if _n_ = 1 then > do i = 1 to dim(n); > n[i] = e[i]; > end; > else > do i = 1 to 8; > chisq = sum(chisq, (n[i] - e[i]) ** 2 /e[i]); > end; > run; > > Muthia Kachirayan- Hide quoted text - > > - Show quoted text -

Dear Muthia, Thank you very much for your response. Now I learned the magic of temporary array.

However, if my data set looks like below: data have; input cnt1 cnt2 cnt3 cnt4 cnt5 cnt6 cnt7 cnt8; cards; 156 100 98 67 78 56 35 12 187 123 100 87 67 0 0 2 198 121 111 89 78 66 44 10 ; run;

Note that the second record has 0 counts at cnt6 and cnt7 (not cnt8). Then my chi-square calculation should be

sum(observed-expected)^2/expected for cnt1~cnt5, where observed(5) =78+56+35+12 and expected(5)=67+2. and the DF=5;

How can I solve it? I tried but my code below are not working.:(

data need; set have; array n[8] _temporary_; array e[*] cnt1 - cnt8; if _n_ = 1 then do i = 1 to dim(n); n[i] = e[i]; end; else do i = 1 to 8; if E{i}=0 then do; grp=i-1; leave; end; else do; Grp=8-1; end; end; drop i j;

if Grp lt 7 then do; do j=Grp+1 to 8; n{Grp}+n{j}; n{j}=0; E{Grp}+E{j}; E{j}=0; end; end;

run;

Thanks! Maryann

Back to: Top of message | Previous page | Main SAS-L page