Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Run Tests for each year each stock index

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 05-12-2020 05:32 PM
(494 views)

Hello,

I am working on a run test for many different stock indices covering many years. I got the code from SAS help yet it's only run for the whole data, but I need to do it for each year of each index. However, I cannot apply the BY index year in the following step. Please any help would be extremely appreciated.

The following DATA step computes the total number of runs (RUNS), the number of positive values (NUMPOS), and the number of negative values (NUMNEG).

data runcount; set two nobs=nobs; if d=0 then delete; if d>0 then n+1; if d<0 then m+1; retain runs 0 numpos 0 numneg 0; previous=lag(d); if _n_=1 then do; runs=1; prevpos=.; currpos=.; prevneg=.; currneg=.; end; else do; prevpos=( previous > 0 ); currpos=( d > 0 ); prevneg=( previous < 0 ); currneg=( d < 0 ); if _n_=2 and (currpos and prevpos) then numpos+1; else if _n_=2 and (currpos and prevneg) then numneg+1; else if _n_=2 and (currneg and prevpos) then numpos+1; else if _n_=2 and (currneg and prevneg) then numneg+1; if currpos and prevneg then do; runs+1; numpos+1; end; if currneg and prevpos then do; runs+1; numneg+1; end; end; run; data runcount; set runcount end=last; if last; run;

8 REPLIES 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

You need to either add by group processing to your data step or subset it and use a macro on each. I'd probably go towards adding by group processing. If you need help modifying your code you'll need to provide some data that we can work with to test things out.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks Reeza for the reply,

I did try to add "by Indices Year" before if function. But it does not seem working. The counting keep going on across all year and all indices. I added a small sample here, if you could help me revising the code. If I do manual I will need to do it 30 years * 63 indices. Really appreciated.

Best

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@rosiecao2509 wrote:

Thanks Reeza for the reply,

I did try to add "by Indices Year" before if function. But it does not seem working. The counting keep going on across all year and all indices. I added a small sample here, if you could help me revising the code. If I do manual I will need to do it 30 years * 63 indices. Really appreciated.

Best

Show the code used to generate the indices. A data set that is incorrect isn't much help.

The typical cause of a count going on is incorrect use of BY variables coupled with First. or Last. combinations (or not used at all). Resetting at the first of group of records is easy:

data want; set have; by id year; if first.id then var=<something>; run;

So if you wan to count ID

data want; set have; by id year; retain count 0; if first.id then count = count+1; run;

which would set the same value of count for the same value of ID across years. This will require the data to be sorted by ID and Year prior for the By statement to work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks for your reply.

I would like to count How many positive stock return, how many negative return for each stock indices during each year. The below code does count the number of possitive/negative returns, but for all indices and all years. I want to break the counting down for each year each index instead. So the First.ID code does not apply.

data runcount; set two nobs=nobs; if d=0 then delete; if d>0 then n+1; if d<0 then m+1; retain runs 0 numpos 0 numneg 0; previous=lag(d);

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@rosiecao2509 wrote:

Thanks for your reply.

I would like to count How many positive stock return, how many negative return for each stock indices during each year. The below code does count the number of possitive/negative returns, but for all indices and all years. I want to break the counting down for each year each index instead. So the First.ID code does not apply.

data runcount; set two nobs=nobs; if d=0 then delete; if d>0 then n+1; if d<0 then m+1; retain runs 0 numpos 0 numneg 0; previous=lag(d);

WE do not have your data.

Where you say "for each year each index" is very likely to be what you would use instead of First.ID. You have not posted anything with the actual variable names. So I used a generic ID. We have entirely too many folks on this forum that will say "for year" but the variable is something like "year_of_occurence" or some such. So I am not going to guess what your variable names might actually be. Sort your data by your "index" and "year" variables and follow the pattern. Maybe. No data example makes it very hard to provide anything resembling code that will work with your data.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I'm assuming you modified all the first/if _n_ to account for the new groups and each new group?

Post your modified code in that case.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Sorry I am not too sure what does that mean. Can you give a bit more details please?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The parts of your code currently that reset for the start, will need to be reset for the start of each group.

So you need to adjust each section such as:

` if _n_=1 then do;`

This currently does a set of specific code at the beginning of the data step process. But now you need to change it to work for the start of each group most likely. I'm assuming when you added the BY statements you also went through and modified these statements to account for the changes.

My suggestion would be to go through your code and comment which lines are conditional and what conditions are required to determine how you need to change each set of code.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.