DATA Step, Macro, Functions and more

iterative computation by groups

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

iterative computation by groups

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

1     .60

1     .20

1     .20

2     .23

2     .37

2     .10

2     .20

2     .10

3     .57

3     .43

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 thisSmiley FrustratedUM[ (.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


Accepted Solutions
Solution
‎06-14-2014 09:29 AM
Super User
Super User
Posts: 6,500

Re: iterative computation by groups

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;

View solution in original post


All Replies
Super User
Posts: 9,681

Re: iterative computation by groups

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

Contributor
Posts: 43

Re: iterative computation by groups

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.  

Solution
‎06-14-2014 09:29 AM
Super User
Super User
Posts: 6,500

Re: iterative computation by groups

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 346 views
  • 6 likes
  • 3 in conversation