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

 

 Hello Everyone,

 

I want to keep the last 5 trading day data in each year from 1998 to 2017.  I am using daily VIX data series from Yahoo Finance. I downloaded the data from the following link. Could you please help me in doing the job in SAS?

 

https://finance.yahoo.com/quote/%5EVIX/history?period1=1298523600&period2=1519448400&interval=1d&fil...

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. Sort your data set descending so that you can take the first 5 of each group. 

2. Use a counter variable to take the first five records. 

 

 You can create the counters as illustrated here:

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

Or below:

 

EDIT: Note that this assumes you have a single record for each date, if you don't the answer is different, but the link above illustrates how to count with multiple variables in your BY group. 

 

Screen Shot 2018-02-24 at 2.30.40 PM.png








@nazmul wrote:

 

 Hello Everyone,

 

I want to keep the last 5 trading day data in each year from 1998 to 2017.  I am using daily VIX data series from Yahoo Finance. I downloaded the data from the following link. Could you please help me in doing the job in SAS?

 

https://finance.yahoo.com/quote/%5EVIX/history?period1=1298523600&period2=1519448400&interval=1d&fil...


 

View solution in original post

5 REPLIES 5
Community_Guide
SAS Moderator

Hello @nazmul,


Your question requires more details before experts can help. Can you revise your question to include more information? 

 

Review this checklist:

  • Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
  • When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
  • If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
    use_buttons.png
  • It also helps to include an example (table or picture) of the result that you're trying to achieve.

To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message.  From there you can adjust the title and add more details to the body of the message.  Or, simply reply to this message with any additional information you can supply.

 

edit_post.png

SAS experts are eager to help -- help them by providing as much detail as you can.

 

This prewritten response was triggered for you by fellow SAS Support Communities member @PeterClemmensen

.
Community_Guide
SAS Moderator

Hello @nazmul,


Your question requires more details before experts can help. Can you revise your question to include more information? 

 

Review this checklist:

  • Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
  • When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
  • If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
    use_buttons.png
  • It also helps to include an example (table or picture) of the result that you're trying to achieve.

To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message.  From there you can adjust the title and add more details to the body of the message.  Or, simply reply to this message with any additional information you can supply.

 

edit_post.png

SAS experts are eager to help -- help them by providing as much detail as you can.

 

This prewritten response was triggered for you by fellow SAS Support Communities member @PeterClemmensen

.
Reeza
Super User

1. Sort your data set descending so that you can take the first 5 of each group. 

2. Use a counter variable to take the first five records. 

 

 You can create the counters as illustrated here:

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

Or below:

 

EDIT: Note that this assumes you have a single record for each date, if you don't the answer is different, but the link above illustrates how to count with multiple variables in your BY group. 

 

Screen Shot 2018-02-24 at 2.30.40 PM.png








@nazmul wrote:

 

 Hello Everyone,

 

I want to keep the last 5 trading day data in each year from 1998 to 2017.  I am using daily VIX data series from Yahoo Finance. I downloaded the data from the following link. Could you please help me in doing the job in SAS?

 

https://finance.yahoo.com/quote/%5EVIX/history?period1=1298523600&period2=1519448400&interval=1d&fil...


 

nazmul
Quartz | Level 8
Thank you so much!
mkeintz
PROC Star

If the data are already sorted by stock, and ascending date, the descending sort can be an expensive solution, compared to:

data want (drop=_:);
  merge have (firstobs=1)
        have (firstobs=6 keep=stock rename=(stock=_stock5));
  if stock^=_stock5;
run;

 

Just look ahead 5 observations to see whether the look-ahead stock (_stock5) is the same as the current stock.  The "firstobs=1" parameter is the default value, but I put it in there to draw attention to what is actually happening.  Two data streams (offset by 5 observations) are being read synchronously.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 5 replies
  • 929 views
  • 0 likes
  • 4 in conversation