Someone help me how to develop below formula into SAS code. I don't have any idea
Thanks in advance
12 divided by 8 is 1.5.
Start with a loop to build the sum, and use array processing:
array vars {*} var1-var40;
sum = 0;
do i = 1 to 40;
sum = sum + (vars{i} - var0) ** 2;
end;
Now multiply, and take the square root:
want = sqrt(1.5 * sum);
That's it.
There can be many ways to do this. Help yourself by showing us what the starting data look like, and how you where you want the result generated (i.e. in a data set, as a result page, other).
12 divided by 8 is 1.5.
Start with a loop to build the sum, and use array processing:
array vars {*} var1-var40;
sum = 0;
do i = 1 to 40;
sum = sum + (vars{i} - var0) ** 2;
end;
Now multiply, and take the square root:
want = sqrt(1.5 * sum);
That's it.
Or if a more compact form is wanted:
Editted note: And I forgot to implement the square root.
array vars {*} var1-var40;
want= 1.5* (uss(of vars{*}) - 2*var0*sum(of vars{*}) + dim(vars)*var0**2);
want = sqrt(want);
which comes from performing the squaring of the term in your formula. Above USS stands for "uncorrected sums of squares". Which means the sum of squared values.
And if you think it doesn't get any shorter, apply Steiner's translation theorem:
abc=sqrt(1.5*(css(of vars[*])+dim(vars)*(mean(of vars[*])-var0)**2));
(using mkeintz's array and the corrected sum of squares, CSS).
Thanks KurtBremser... and who are all replied
Here are variants of KurtBremser's approach, including test data:
data test;
/* Create test data */
array var[0:40] var0-var40 (5 40*7);
/* Apply the formula */
do i=1 to 40;
s+(var[i]-var0)**2;
end;
abc=sqrt(12/8*s);
/* Write the result to the log before it's overwritten */
put abc=;
/* Alternative way */
array d[40] _temporary_;
do i=1 to 40;
d[i]=(var[i]-var0)**2;
end;
abc=sqrt(12/8*sum(of d[*]));
drop i s;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.