Hi
I have difficulty writing a code in proc sql to do the following iterative computation.
The data with three groups looks like this (first ten observations),
Group Var
2 .23
2 .37
2 .10
2 .20
2 .10
and within each group numbers add up to one.
I would like to get the sum the squared difference of all numbers.
For example for the first group in bold, I would like to do something like this:SUM[ (.60-.20)**2 + (.60-.20)**2 + (.20-.20)**2],
for the second there will be like sum of ten squared differences. Finally I would like divide this number by the number of observations in each group.
How can I do this with proc sql
Thank you for your help,
Metin
Just take all possible combinations (FULL JOIN) of the data with itself. This will give you the values you listed, but also the same with the numbers in the opposite order. It will also include where you have crossed value with itself, but that will contribute 0 to the total sum of squares. So just divide the sum by 2 to get back to the number you requested.
To find the number in each group notice that if N is 3 then there are 3*3 rows when crossed with itself.
proc sql noprint ;
create table want as
select a.grp
, sum( (a.val - b.val) ** 2 )/2 as sumsq
, (calculated sumsq)/sqrt(count(*)) as meansq
from have a
, have b
where a.grp = b.grp
group by 1
order by 1
;
quit;
I am afraid it is hard for SQL but easy for data step. Are you trying to get somewhat model's average square error ?
data have; input Group Var ; cards; 1 .60 1 .20 1 .20 2 .23 2 .37 2 .10 2 .20 2 .10 3 .57 3 .43 ; run; data want; set have; by group; array x{100} _temporary_; if first.group then do;n=0; sum=0;call missing(of x{*}) ;end; n+1; x{n}=var; if last.group then do; do i=1 to n-1; do j=i+1 to n; sum+(x{i}-x{j})**2; end; end; chisq=divide(sum,n); output; end; drop i j n var; run;
Xia Keshan
Thank you Xia and Tom for your time and codes.
Your answers have been very helpful, especially because I had a chance to compare both code (proc sql and data step) and understand the logic.
Just take all possible combinations (FULL JOIN) of the data with itself. This will give you the values you listed, but also the same with the numbers in the opposite order. It will also include where you have crossed value with itself, but that will contribute 0 to the total sum of squares. So just divide the sum by 2 to get back to the number you requested.
To find the number in each group notice that if N is 3 then there are 3*3 rows when crossed with itself.
proc sql noprint ;
create table want as
select a.grp
, sum( (a.val - b.val) ** 2 )/2 as sumsq
, (calculated sumsq)/sqrt(count(*)) as meansq
from have a
, have b
where a.grp = b.grp
group by 1
order by 1
;
quit;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.