Statistical programming, matrix languages, and more

Limiting range of variables in a function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

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;


Accepted Solutions
Solution
‎06-27-2014 10:09 AM
SAS Super FREQ
Posts: 3,420

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.

View solution in original post


All Replies
Solution
‎06-27-2014 10:09 AM
SAS Super FREQ
Posts: 3,420

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.

Occasional Contributor
Posts: 14

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 272 views
  • 0 likes
  • 2 in conversation