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

Someone help me how to develop below formula into SAS code. I don't have any idea

 

Thanks in advance

 

2018-09-20_14-20-00.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

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.

mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
FreelanceReinh
Jade | Level 19

@ez123:

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

ez123
Fluorite | Level 6

Thanks KurtBremser...

FreelanceReinh
Jade | Level 19

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1434 views
  • 2 likes
  • 4 in conversation