BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MetinBulus
Quartz | Level 8

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 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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Ksharp
Super User

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

MetinBulus
Quartz | Level 8

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.  

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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