Solved
Contributor
Posts: 41

# FCMP complex loop to write function

I'm trying to write a following function that I would like to minimize using a sas optlso. But I'm having difficulty in figuring out the logic. Any help in creating the logic would be very helpful.

here is the function:

The part that I cannot figure out is i ne j ne k ne i. If you can modify the below code to reflecy te a

Below is a reproducible example. Any help would be greating appreciated.

```proc fcmp outlib=sasuser.myfuncs.mypkg;
function fdef1(x1, x2,x3,x4,x5,x6,x7);
array x[7];
array y[7,7] /nosym (	1.0000 4.0000 9.0000 6.0000 6.0000 5.0000 5.0000
0.2500 1.0000 7.0000 5.0000 5.0000 3.0000 4.0000
0.1111 0.1429 1.0000 0.2000 0.2000 0.1429 0.2000
0.1667 0.2000 5.0000 1.0000 1.0000 0.3333 0.3333
0.1667 0.2000 5.0000 1.0000 1.0000 0.3333 0.3333
0.2000 0.3333 7.0000 3.0000 3.0000 1.0000 2.0000
0.2000 0.2500 5.0000 3.0000 3.0000 0.5000 1.0000);

fx = 0;
do i= 1 to dim(y);
do j = i+1 to dim(y);
do k = 1 to dim(y);
fx = fx + ((y[i,k]*y[k,j])-(x[i]/x[j]))**2;
end;
end;
end;
return(fx);
endsub;
run;```

One additional question:  Is there a way to print out the indices i j and k in the loop to a SAS log/dataset to QC ?

Accepted Solutions
Solution
‎12-15-2016 02:41 PM
Super User
Posts: 13,924

## Re: FCMP complex loop to write function

Is this what you need:

if i ne j and j ne k and k ne i then fx = fx + ((y[i,k]*y[k,j])-(x[i]/x[j]))**2;

All Replies
Solution
‎12-15-2016 02:41 PM
Super User
Posts: 13,924

## Re: FCMP complex loop to write function

Is this what you need:

if i ne j and j ne k and k ne i then fx = fx + ((y[i,k]*y[k,j])-(x[i]/x[j]))**2;

Contributor
Posts: 41

## Re: FCMP complex loop to write function

Thank you very much. Can you please also let me know if I can print i,j,k to the log or lst ?

Super User
Posts: 13,924

## Re: FCMP complex loop to write function

Likely not to LST,  but did you try something like:

Put "In function FDEF1 " i=;

Completely untested though.

I can see a use for debugging with this but since functions can get many times in a procedure or datastep I would recommend not leaving them in the final version.

☑ This topic is solved.

Discussion stats
• 3 replies
• 305 views
• 1 like
• 2 in conversation