<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Run Tests for each year each stock index in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647630#M78648</link>
    <description>&lt;P&gt;The parts of your code currently that reset for the start, will need to be reset for the start of each group.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to adjust each section such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if _n_=1 then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 May 2020 20:57:41 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-05-13T20:57:41Z</dc:date>
    <item>
      <title>Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647250#M78603</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following DATA step computes the total number of runs (RUNS), the number of positive values (NUMPOS), and the number of negative values (NUMNEG).&lt;/P&gt;&lt;PRE&gt;      data runcount;
        set two nobs=nobs;
        if d=0 then delete;
        if d&amp;gt;0 then n+1;
        if d&amp;lt;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 &amp;gt; 0 );
          currpos=( d &amp;gt; 0 );
          prevneg=( previous &amp;lt; 0 );
          currneg=( d &amp;lt; 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;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 21:32:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647250#M78603</guid>
      <dc:creator>rosiecao2509</dc:creator>
      <dc:date>2020-05-12T21:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647256#M78604</link>
      <description>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.</description>
      <pubDate>Tue, 12 May 2020 21:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647256#M78604</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-12T21:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647412#M78607</link>
      <description>&lt;P&gt;Thanks Reeza for the reply,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 11:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647412#M78607</guid>
      <dc:creator>rosiecao2509</dc:creator>
      <dc:date>2020-05-13T11:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647575#M78641</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328613"&gt;@rosiecao2509&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks Reeza for the reply,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show the code used to generate the indices. A data set that is incorrect isn't much help.&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id year;
   if first.id then var=&amp;lt;something&amp;gt;;
run;&lt;/PRE&gt;
&lt;P&gt;So if you wan to count ID&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id year;
   retain count 0;
   if first.id then count = count+1;
run;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 18:40:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647575#M78641</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-13T18:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647619#M78645</link>
      <description>&lt;P&gt;I'm assuming you modified all the first/if _n_ to account for the new groups and each new group?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post your modified code in that case.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 20:49:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647619#M78645</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-13T20:49:15Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647625#M78646</link>
      <description>&lt;P&gt;Thanks for your reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;PRE&gt;data runcount;
        set two nobs=nobs;
        if d=0 then delete;
        if d&amp;gt;0 then n+1;
        if d&amp;lt;0 then m+1;
        retain runs 0 numpos 0 numneg 0;
        previous=lag(d);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 20:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647625#M78646</guid>
      <dc:creator>rosiecao2509</dc:creator>
      <dc:date>2020-05-13T20:53:10Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647628#M78647</link>
      <description>&lt;P&gt;Sorry I am not too sure what does that mean. Can you give a bit more details please?&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 20:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647628#M78647</guid>
      <dc:creator>rosiecao2509</dc:creator>
      <dc:date>2020-05-13T20:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647630#M78648</link>
      <description>&lt;P&gt;The parts of your code currently that reset for the start, will need to be reset for the start of each group.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to adjust each section such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if _n_=1 then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 20:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647630#M78648</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-13T20:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Run Tests for each year each stock index</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647636#M78649</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328613"&gt;@rosiecao2509&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your reply.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;data runcount;
        set two nobs=nobs;
        if d=0 then delete;
        if d&amp;gt;0 then n+1;
        if d&amp;lt;0 then m+1;
        retain runs 0 numpos 0 numneg 0;
        previous=lag(d);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;WE do not have your data.&lt;/P&gt;
&lt;P&gt;Where you say&amp;nbsp; "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.&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 21:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Run-Tests-for-each-year-each-stock-index/m-p/647636#M78649</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-13T21:43:27Z</dc:date>
    </item>
  </channel>
</rss>

