<?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: Array help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284045#M57944</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really need to provide an example of what results you are expecting to see to help the community provide and answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I have a potential solution that should work based on your needs and I have added plenty of comments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have any questions then drop me a message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have /* (drop = gap: i) */;&lt;BR /&gt;&lt;BR /&gt;/* &lt;BR /&gt;&lt;BR /&gt;There are four possible solutions based on information provided and depend on&lt;BR /&gt;the specific requirements or answers to these questions (both based on using the first&lt;BR /&gt;row of your input with ID of XX as the example);&lt;BR /&gt;&lt;BR /&gt;Q1. Does the month of status change count as a gap of 0 or 1?&lt;BR /&gt;In example mth2 = 1 and mth 3 = 0, so status change in mth3 but is this gap counted as 0 or 1?&lt;BR /&gt;&lt;BR /&gt;Q2. Do you want to count the intermediate gap values or not?&lt;BR /&gt;In example, mth3 = 0, mth4 to mth8 all = 1, so assuming month of&lt;BR /&gt;status change counts as 1, gap @ mth4 = 1, @ mth5 = 2, @ mth6 = 3, @ mth7 = 4 &amp;amp; @mth8 = 5.&lt;BR /&gt;So do you want to count the gap for mth4 and mth5 and mth6 and mth7 and mth8, or just mth8?&lt;BR /&gt;&lt;BR /&gt;If answer to Q1 is 0, change the two lines with comment &amp;lt;=== Q1&lt;BR /&gt;&lt;BR /&gt;If answer to Q2 is count only maximum gap for a sequence uncomment the line with comment &amp;lt;===Q2&lt;BR /&gt;&lt;BR /&gt;*/&lt;BR /&gt;&lt;BR /&gt;infile datalines;&lt;BR /&gt;input ID $2. mth1-mth12;&lt;BR /&gt;&lt;BR /&gt;* define array to hold the mths data as read in (makes it easier to process);&lt;BR /&gt;array mth(12);&lt;BR /&gt;* define array to hold the calculated gaps;&lt;BR /&gt;array gap(12);&lt;BR /&gt;* define array to hold the count of the different gaps as calculated;&lt;BR /&gt;array m(12) (12 * 0);&lt;BR /&gt;&lt;BR /&gt;* gap(1) will always be the same value on each record as no prior month to compare against;&lt;BR /&gt;gap(1) = 1; /* &amp;lt;=== Q1 */&lt;BR /&gt;&lt;BR /&gt;* work through the rest of months and calculate the gaps;&lt;BR /&gt;* if change from previous month then set gap to 1 (or 0 if change counts as a gap of 0 and gap(1) is set to 0);&lt;BR /&gt;* if no change add 1 to previous gap;&lt;BR /&gt;* if you only want to count the maximum gap between status changes then reset the previous gap to 0;&lt;BR /&gt;* e.g. in row with ID XX, mth8 has a gap of 5 months since previous change;&lt;BR /&gt;&lt;BR /&gt;do i = 2 to 12;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if mth(i) ne mth(i-1) then&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;gap(i) = 1; /* &amp;lt;=== Q1 */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;else do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;gap(i) = gap(i-1) + 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;*gap(i - 1) = 0; /* &amp;lt;=== Q2 */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;* cycle through gaps and increment appropriate gap counter;&lt;BR /&gt;* add one to the appropriate gap counter, e.g. if value of gap8 is 5 add 1 to m5;&lt;BR /&gt;do i = 1 to 12;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if gap(i) gt 0 then&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;m(gap(i)) + 1;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;datalines;&lt;BR /&gt;XX 1 1 0 1 1 1 1 1 0 0 0 1&lt;BR /&gt;YY 0 0 0 1 1 1 1 0 1 0 0 0&lt;BR /&gt;ZZ 0 0 0 1 1 1 1 0 1 0 0 0&lt;BR /&gt;WW 0 0 1 1 0 0 0 0 1 0 0 0&lt;BR /&gt;PP 1 1 1 0 0 0 0 1 0 1 1 1&lt;BR /&gt;;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jul 2016 14:29:55 GMT</pubDate>
    <dc:creator>rivieralad</dc:creator>
    <dc:date>2016-07-13T14:29:55Z</dc:date>
    <item>
      <title>Array help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284009#M57934</link>
      <description>&lt;P&gt;I have a data test and I am trying to find # months it takes for an ID to change status from 1/0.&lt;/P&gt;
&lt;P&gt;For e.g. for ID= XX, I want to create M1,M2,M3,......M12 variables&lt;/P&gt;
&lt;P&gt;where M1 = # times gap = 1 in status change from last status&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; M2 = # times gap = 2 in status change from last status&lt;/P&gt;
&lt;P&gt;and so on. Please advise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; test;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;input&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; ID &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;$2.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; mth1 mth2 mth3 mth4 mth5 mth6 mth7 mth8 mth9 mth10 mth11 mth12;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;infile&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; datalines;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;datalines&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;XX 1 1 0 1 1 1 1 1 0 0 0 1&lt;/P&gt;
&lt;P&gt;YY 0 0 0 1 1 1 1 0 1 0 0 0&lt;/P&gt;
&lt;P&gt;ZZ 0 0 0 1 1 1 1 0 1 0 0 0&lt;/P&gt;
&lt;P&gt;WW 0 0 1 1 0 0 0 0 1 0 0 0&lt;/P&gt;
&lt;P&gt;PP 1 1 1 0 0 0 0 1 0 1 1 1&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;PRINT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 12:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284009#M57934</guid>
      <dc:creator>Siddharth123</dc:creator>
      <dc:date>2016-07-13T12:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Array help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284039#M57940</link>
      <description>&lt;P&gt;Best advice:&amp;nbsp; give an examle of what the result should look like.&amp;nbsp; It's a little tough to figure out from your description.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 14:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284039#M57940</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-13T14:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Array help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284043#M57943</link>
      <description>&lt;P&gt;I predict that someone will answer this soon, but&amp;nbsp;I wanted to mention that this analysis is closely related to the "Runs Test". There are &lt;A href="http://blogs.sas.com/content/iml/2013/10/09/how-to-tell-if-a-sequence-is-random.html" target="_self"&gt;many ways to implement the Runs Test in SAS.&lt;/A&gt;, including the DATA step. I wrote an article about &lt;A href="http://blogs.sas.com/content/iml/2014/01/13/how-to-vectorize-time-series-computations.html" target="_self"&gt;how to count the number of runs in a binary sequence&lt;/A&gt;, but it uses SAS/IML whereas I assume you want to use the DATA step.&amp;nbsp;&amp;nbsp;Depending on what you are trying to accomplish, the articles that I linked to might help you analyze your data.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 14:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284043#M57943</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-07-13T14:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Array help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284045#M57944</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really need to provide an example of what results you are expecting to see to help the community provide and answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I have a potential solution that should work based on your needs and I have added plenty of comments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have any questions then drop me a message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have /* (drop = gap: i) */;&lt;BR /&gt;&lt;BR /&gt;/* &lt;BR /&gt;&lt;BR /&gt;There are four possible solutions based on information provided and depend on&lt;BR /&gt;the specific requirements or answers to these questions (both based on using the first&lt;BR /&gt;row of your input with ID of XX as the example);&lt;BR /&gt;&lt;BR /&gt;Q1. Does the month of status change count as a gap of 0 or 1?&lt;BR /&gt;In example mth2 = 1 and mth 3 = 0, so status change in mth3 but is this gap counted as 0 or 1?&lt;BR /&gt;&lt;BR /&gt;Q2. Do you want to count the intermediate gap values or not?&lt;BR /&gt;In example, mth3 = 0, mth4 to mth8 all = 1, so assuming month of&lt;BR /&gt;status change counts as 1, gap @ mth4 = 1, @ mth5 = 2, @ mth6 = 3, @ mth7 = 4 &amp;amp; @mth8 = 5.&lt;BR /&gt;So do you want to count the gap for mth4 and mth5 and mth6 and mth7 and mth8, or just mth8?&lt;BR /&gt;&lt;BR /&gt;If answer to Q1 is 0, change the two lines with comment &amp;lt;=== Q1&lt;BR /&gt;&lt;BR /&gt;If answer to Q2 is count only maximum gap for a sequence uncomment the line with comment &amp;lt;===Q2&lt;BR /&gt;&lt;BR /&gt;*/&lt;BR /&gt;&lt;BR /&gt;infile datalines;&lt;BR /&gt;input ID $2. mth1-mth12;&lt;BR /&gt;&lt;BR /&gt;* define array to hold the mths data as read in (makes it easier to process);&lt;BR /&gt;array mth(12);&lt;BR /&gt;* define array to hold the calculated gaps;&lt;BR /&gt;array gap(12);&lt;BR /&gt;* define array to hold the count of the different gaps as calculated;&lt;BR /&gt;array m(12) (12 * 0);&lt;BR /&gt;&lt;BR /&gt;* gap(1) will always be the same value on each record as no prior month to compare against;&lt;BR /&gt;gap(1) = 1; /* &amp;lt;=== Q1 */&lt;BR /&gt;&lt;BR /&gt;* work through the rest of months and calculate the gaps;&lt;BR /&gt;* if change from previous month then set gap to 1 (or 0 if change counts as a gap of 0 and gap(1) is set to 0);&lt;BR /&gt;* if no change add 1 to previous gap;&lt;BR /&gt;* if you only want to count the maximum gap between status changes then reset the previous gap to 0;&lt;BR /&gt;* e.g. in row with ID XX, mth8 has a gap of 5 months since previous change;&lt;BR /&gt;&lt;BR /&gt;do i = 2 to 12;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if mth(i) ne mth(i-1) then&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;gap(i) = 1; /* &amp;lt;=== Q1 */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;else do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;gap(i) = gap(i-1) + 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;*gap(i - 1) = 0; /* &amp;lt;=== Q2 */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;* cycle through gaps and increment appropriate gap counter;&lt;BR /&gt;* add one to the appropriate gap counter, e.g. if value of gap8 is 5 add 1 to m5;&lt;BR /&gt;do i = 1 to 12;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if gap(i) gt 0 then&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;m(gap(i)) + 1;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;datalines;&lt;BR /&gt;XX 1 1 0 1 1 1 1 1 0 0 0 1&lt;BR /&gt;YY 0 0 0 1 1 1 1 0 1 0 0 0&lt;BR /&gt;ZZ 0 0 0 1 1 1 1 0 1 0 0 0&lt;BR /&gt;WW 0 0 1 1 0 0 0 0 1 0 0 0&lt;BR /&gt;PP 1 1 1 0 0 0 0 1 0 1 1 1&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 14:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284045#M57944</guid>
      <dc:creator>rivieralad</dc:creator>
      <dc:date>2016-07-13T14:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Array help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284190#M57976</link>
      <description>&lt;P&gt;Here is one way. Look at the data set WANT. The STATUS takes either 0 or 1. The COUNT counts the STATUS until next change.&lt;/P&gt;&lt;P&gt;I am not sure how you want the M1, M2, &amp;nbsp;... are to filled. Show how you want to fit to it from the WANT dat set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input ID $2. mth1 mth2 mth3 mth4 mth5 mth6 mth7 mth8 mth9 mth10 mth11 mth12;
infile datalines;
datalines;
XX 1 1 0 1 1 1 1 1 0 0 0 1
YY 0 0 0 1 1 1 1 0 1 0 0 0
ZZ 0 0 0 1 1 1 1 0 1 0 0 0
WW 0 0 1 1 0 0 0 0 1 0 0 0
PP 1 1 1 0 0 0 0 1 0 1 1 1
;
run;

data want;
      set test ;
      array k[*] mth1 - mth12;
      status = .;
      count = 0;
      do i = 2 to dim(k);
         if k[i] = k[i-1] then count + 1;
         else do;
            count + 1;
            status = k[i - 1];
            output;
            /*put ID =  k[i-1] = count =; */
            count = 0;
         end;
      end;
keep ID status count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2016 20:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-help/m-p/284190#M57976</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-13T20:40:11Z</dc:date>
    </item>
  </channel>
</rss>

