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

Limiting range of variables in a function

Hi, I would like to make such a version of this function 'mhatx1' which omits x=i from the sum i.e. computes t4 where x is not equal to i. Is there some way to use logical operators in this case? Something like temp1=x-i where x-i is not equal to 0, else . ?

start mhatx1(m,p,h,pi,e);

t4=j(m,1);

do x=1 to nrow(p);

i=T(1:m);

temp1=x-i;

ue=Kmod(temp1,h,pi,e)#p;

le=Kmod(temp1,h,pi,e);

t4=sum(ue)/sum(le);

end;

return (t4);

finish;

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

Re: Limiting range of variables in a function

If I understand your quetion, there are several ways to accomplish what you are asking. The method to choose depends on whether you want to modify the computation of ue and le, or just the sums.

Here are some functions that are good to know about:

• The REMOVE function.   You can use this function if you want to remove certain elements from temp1. For example, the zero always occurs at the x_th position, so the following statement modifies temp1 so that it contains m-1 elements:
temp1=remove(x-i,x);  /* exclude 0 from elements */
• The LOC function:  Although it is not necessary here, you might need to find the indices of the elements tot satisfy some criterion before you can remove them. The LOC function enables you to find the elements. For details, see Finding data that satisfy a criterion - The DO Loop

I think the REMOVE function is all you need here.

2 REPLIES 2
SAS Super FREQ

Re: Limiting range of variables in a function

If I understand your quetion, there are several ways to accomplish what you are asking. The method to choose depends on whether you want to modify the computation of ue and le, or just the sums.

Here are some functions that are good to know about:

• The REMOVE function.   You can use this function if you want to remove certain elements from temp1. For example, the zero always occurs at the x_th position, so the following statement modifies temp1 so that it contains m-1 elements:
temp1=remove(x-i,x);  /* exclude 0 from elements */
• The LOC function:  Although it is not necessary here, you might need to find the indices of the elements tot satisfy some criterion before you can remove them. The LOC function enables you to find the elements. For details, see Finding data that satisfy a criterion - The DO Loop

I think the REMOVE function is all you need here.

Calcite | Level 5

Re: Limiting range of variables in a function

Thank you! That REMOVE was what I was looking for, even though I just ended up removing t'th solutions of the inner functions ue and le from summations:

start mhatx2(m,p,h,pi,e);

t5=j(m,1);

do x=1 to nrow(p);

i=T(1:m);

temp1=x-i;

ue=Kmod(temp1,h,pi,e)#p;

le=Kmod(temp1,h,pi,e);

t5=(sum(ue)-ue)/(sum(le)-le);

end;

return (t5);

finish;

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
• 2 replies
• 851 views
• 0 likes
• 2 in conversation