BookmarkSubscribeRSS Feed
Anon123
Fluorite | Level 6

Hi I have this code

 

%let K=6;

data thesis.test7;

set thesis.test2;

array returns SPY--AFL;

do i= 1+&K to dim(returns);

returns[i]=(returns[i]-lag(returns[i]))/lag(returns[i]);

end;

run;

 

I would like the return computation to start at row 1+K and have the previous rows blank but it starts at column 1+K with the stock prices shown for previous columns.

What am I doing wrong?

 

Thanks in advance.


Screen Shot 2016-07-15 at 16.06.13.png
4 REPLIES 4
ballardw
Super User

The automatic variable _n_ has the row number and can be used for such processing:

data thesis.test7;
   set thesis.test2;
   array returns SPY--AFL;
   if _n_ > &k then do i= 1 to dim(returns);
   returns[i]=(returns[i]-lag(returns[i]))/lag(return​s[i]);
   end;
run;

HOWEVER, the lag function, when it executes for row 7 all the lagged values will be missing because that is the first time the lag function is called.

 

You might be better off creating an array of  non-conditional lagged values and reference that array instead of using lag(returns[i])

Anon123
Fluorite | Level 6
Cool thanks I'll try that out. Just another quick question, I'm new to the language SAS uses,
is there a way to generalize
array returns SPY--AFL
i.e. take the first column to the last column?

Thank you for your time.
ballardw
Super User

To you mean to reverse the order of processing (AFL first and SPY last)? Easiest would be to change the index of the do loop:

 

Do i = dim(returns) to 1 by (-1);

 

If you are thinking of something else you may need to go into more details and the reasons why. Preferrably in a separate topic.

Ksharp
Super User
Better post your data as text , No one would like to type it for you .
And most important thing is post your output too .

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 998 views
  • 0 likes
  • 3 in conversation