DATA Step, Macro, Functions and more

Identify last trading days in each year from 1998 to 2017

Accepted Solution Solved
Reply
Contributor
Posts: 73
Accepted Solution

Identify last trading days in each year from 1998 to 2017

[ Edited ]

 

 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...


Accepted Solutions
Solution
‎02-24-2018 04:43 PM
Super User
Posts: 23,237

Re: Identify last trading days in each year from 1998 to 2017

[ Edited ]

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


All Replies
SAS Moderator
Posts: 55

Re: Identify last trading days in each year from 1998 to 2017 [how to improve your question]

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 @draycut

.
SAS Moderator
Posts: 55

Re: Identify last trading days in each year from 1998 to 2017 [how to improve your question]

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 @draycut

.
Solution
‎02-24-2018 04:43 PM
Super User
Posts: 23,237

Re: Identify last trading days in each year from 1998 to 2017

[ Edited ]

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...


 

Contributor
Posts: 73

Re: Identify last trading days in each year from 1998 to 2017

Thank you so much!
Trusted Advisor
Posts: 1,309

Re: Identify last trading days in each year from 1998 to 2017

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.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 192 views
  • 0 likes
  • 4 in conversation