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

Hi,

I'm a new struggler with IML, and have a noobish question.

What I have here, or would have, is an inner function that for each "timevalue" x makes constant vector c=x, a running time vector t from moment x till the end of data t(x:m), deducts this t from constant x and multiplies with "price" vector p from moment x onwards (e=p[x:m]). Finally s(x) is sum of these rows.

Simple thing expained in complex way but simply but I would like to construct g(x)=SUM operator from x to m [(x-t(x))*p(x)]. Inner function is valid and for example x=3 creates:

t  c  e  d  s

3 3 2 0 -104

4 3 4 -4  

5 3 7 -14  

6 3 2 -6  

7 3 4 -16  

8 3 8 -40  

9 3 4 -24                     

But how can I insert x there one by one from 1 to m and get a result vector of s(x:m)?

p={5,6,2,4,7,2,4,8,4};

m=nrow(p);

Start uber(p,m);

w=0;

do i=1 to nrow(p);

    x=w+i;

    q=(x:m);                    /*m is sample size, a constant from outside of this loop*/

    t=q`;                        /*just transposing to get column vector into row vector*/

    c=j(nrow(t),1,x);            /*creates row vector of constant x*/

    e=p[x:m];

    d=(c-t)#e;                /*distance times price*/

    s=sum(d);

    end;

    return (s);         /*given series of prices p, inserting x into this loop gives s(x)=cumulative sum of weight times price for (x:T), OR SHOULD DO SO*/

finish;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I'm not 100% sure that I follow your example, but it sounds like you want ti compute the sum s for each value of x in the loop. To do that, just create s as a vector outside of the loop and assign to s inside the loop:

s = j(m,1);

do i = 1 to nrow(p);

...

   s = sum(d);

end;

return( s);

I'll mention that you can accomplish the same computation with many fewer variables. You don't need x (which equals i) and you don't need w, q, e, or c.  Here's a simpler implementation:

s = j(m,1);

do i=1 to nrow(p);

    t=T(i:m);          /*m is sample size, a constant */

    d=(i-t)#p;      /*distance times price*/

    s=sum(d);

end;

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

I'm not 100% sure that I follow your example, but it sounds like you want ti compute the sum s for each value of x in the loop. To do that, just create s as a vector outside of the loop and assign to s inside the loop:

s = j(m,1);

do i = 1 to nrow(p);

...

   s = sum(d);

end;

return( s);

I'll mention that you can accomplish the same computation with many fewer variables. You don't need x (which equals i) and you don't need w, q, e, or c.  Here's a simpler implementation:

s = j(m,1);

do i=1 to nrow(p);

    t=T(i:m);          /*m is sample size, a constant */

    d=(i-t)#p;      /*distance times price*/

    s=sum(d);

end;

LauriN
Calcite | Level 5

That indeed does the trick, and also enlightens how to use a loop, thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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