<?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: Calculation of Gap between purchases in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344872#M79298</link>
    <description>Hi ,&lt;BR /&gt;&lt;BR /&gt;Thank you very much for your response. the code gives almost what I want,&lt;BR /&gt;but it seems it doesn't count when you have 2 consecutive missings at the&lt;BR /&gt;beginning. For example for the customer 5, has 2 missing weeks at the&lt;BR /&gt;beginnin, I am expecting to have len_gap_1=2 but the code seems to ignore&lt;BR /&gt;the first time they have missed purchases.. Is that right?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thank You&lt;BR /&gt;&lt;BR /&gt;customer_id w1 w2 len_gap_1&lt;BR /&gt;1 10 4 7&lt;BR /&gt;2 9 . 1&lt;BR /&gt;3 29 4 7&lt;BR /&gt;4 8 4 3&lt;BR /&gt;5 . . 1&lt;BR /&gt;6 . 0 1&lt;BR /&gt;7 10 4 5&lt;BR /&gt;8 9 . 1&lt;BR /&gt;9 . 1 1&lt;BR /&gt;10 . 1 1&lt;BR /&gt;11 8 . 1&lt;BR /&gt;12 1 1 .&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
    <pubDate>Tue, 28 Mar 2017 04:17:15 GMT</pubDate>
    <dc:creator>Question</dc:creator>
    <dc:date>2017-03-28T04:17:15Z</dc:date>
    <item>
      <title>Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344677#M79227</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to calculate the gap between purchases for ecah customer. I have for example the file below, an dwould like to calculate the 3 variables on the righ (First Gaps, Second Gap etc...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your help would be much appreciated,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank You&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="1159"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;customer_id&lt;/TD&gt;
&lt;TD width="64"&gt;w1&lt;/TD&gt;
&lt;TD width="64"&gt;w2&lt;/TD&gt;
&lt;TD width="64"&gt;w3&lt;/TD&gt;
&lt;TD width="64"&gt;w4&lt;/TD&gt;
&lt;TD width="64"&gt;w5&lt;/TD&gt;
&lt;TD width="64"&gt;w6&lt;/TD&gt;
&lt;TD width="64"&gt;w7&lt;/TD&gt;
&lt;TD width="64"&gt;w8&lt;/TD&gt;
&lt;TD width="64"&gt;w9&lt;/TD&gt;
&lt;TD width="64"&gt;w10&lt;/TD&gt;
&lt;TD width="64"&gt;w11&lt;/TD&gt;
&lt;TD width="64"&gt;w12&lt;/TD&gt;
&lt;TD width="64"&gt;w13&lt;/TD&gt;
&lt;TD width="18"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="82"&gt;First Gap&lt;/TD&gt;
&lt;TD width="79"&gt;Secong Gap&lt;/TD&gt;
&lt;TD width="64"&gt;Third Gap&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD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;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;7 weeks&lt;/TD&gt;
&lt;TD&gt;1 week&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;~&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;3 weeks&lt;/TD&gt;
&lt;TD&gt;1 week&lt;/TD&gt;
&lt;TD&gt;3 weeks&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Mon, 27 Mar 2017 15:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344677#M79227</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-03-27T15:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344683#M79230</link>
      <description>&lt;P&gt;Because of the way you posted data the "last 3 columns" are appearing over the forum marginal information and almost impossible to read.&lt;/P&gt;
&lt;P&gt;If you have a SAS data set with your example data then you can turn that into data step code: Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think we are going to have to assume that the values you show as ~ are actually missing values in your SAS data.&lt;/P&gt;
&lt;P&gt;In which case it appears that your request is actually to "count consecutive missing values from variables w1 to w13" in up to 3 groups. Does that sound like your need?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2017 16:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344683#M79230</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-27T16:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344712#M79236</link>
      <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;100% with respect to his comments about the data and description of the problem. &amp;nbsp;That said, I took a shot. I also had doubts whether the data you presented represented all the possible cases (# of gaps, when they start and end...), so I added some additional cases, including no gaps. The code below should be a start for you. I also added output to store the week in which the gap started, since that seems like it might be useful to have.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input customer_id w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13;
datalines;
1 10 4 4 10 8 8 . . . . . . .
2 9 . 2 2 . . . . . . . . .
3 29 4 2 2 . . . . . . . 4 . 
4 8 4 2 4 . . . 2 . 4 . . .
5 . . 1 2 3 4 . 5 . 7 . . .
6 . 0 1 2 3 4 . 5 . 7 . . .
7 10 4 4 10 8 8 . . . . . 1 2
8 9 . 2 2 . . . . . . . . 1
9 . 1 . 2 . 4 . 4 . 9 . . 1
10 . 1 . 2 . 4 . 4 . 9 . 1 .
11 8 . 1 . 2 . 4 . 4 . 9 . 1
12 1 1 1 1 1 1 1 1 1 1 1 1 1
;

data want;
    set have;
    array w{*} w1 - w13;
    array len_gap_{6};
    array wk_gap_{6};
    gapNum = 0; /* Initialize a counter for the gap number. */
    do i = 1 to dim(w);
        if missing(w(i)) then do;
            gapLen = gapLen + 1;
            if gapLen = 1 then gapNum = gapNum + 1;
            if gapNum &amp;gt; 0 then len_gap_(gapNum) = gapLen;
            if gapLen = 1 then wk_gap_(gapNum) = i;
        end;
        else do;
            gapLen = 0;
        end;
    end;

run;

proc print;
    var customer_id len_gap:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Mar 2017 18:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344712#M79236</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-03-27T18:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344872#M79298</link>
      <description>Hi ,&lt;BR /&gt;&lt;BR /&gt;Thank you very much for your response. the code gives almost what I want,&lt;BR /&gt;but it seems it doesn't count when you have 2 consecutive missings at the&lt;BR /&gt;beginning. For example for the customer 5, has 2 missing weeks at the&lt;BR /&gt;beginnin, I am expecting to have len_gap_1=2 but the code seems to ignore&lt;BR /&gt;the first time they have missed purchases.. Is that right?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thank You&lt;BR /&gt;&lt;BR /&gt;customer_id w1 w2 len_gap_1&lt;BR /&gt;1 10 4 7&lt;BR /&gt;2 9 . 1&lt;BR /&gt;3 29 4 7&lt;BR /&gt;4 8 4 3&lt;BR /&gt;5 . . 1&lt;BR /&gt;6 . 0 1&lt;BR /&gt;7 10 4 5&lt;BR /&gt;8 9 . 1&lt;BR /&gt;9 . 1 1&lt;BR /&gt;10 . 1 1&lt;BR /&gt;11 8 . 1&lt;BR /&gt;12 1 1 .&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Tue, 28 Mar 2017 04:17:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344872#M79298</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-03-28T04:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344874#M79300</link>
      <description>&lt;PRE&gt;

data have;
    input customer_id w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13;
datalines;
1 10 4 4 10 8 8 . . . . . . .
2 9 . 2 2 . . . . . . . . .
3 29 4 2 2 . . . . . . . 4 . 
4 8 4 2 4 . . . 2 . 4 . . .
5 . . 1 2 3 4 . 5 . 7 . . .
6 . 0 1 2 3 4 . 5 . 7 . . .
7 10 4 4 10 8 8 . . . . . 1 2
8 9 . 2 2 . . . . . . . . 1
9 . 1 . 2 . 4 . 4 . 9 . . 1
10 . 1 . 2 . 4 . 4 . 9 . 1 .
11 8 . 1 . 2 . 4 . 4 . 9 . 1
12 1 1 1 1 1 1 1 1 1 1 1 1 1
;
proc transpose data=have out=temp;
by customer_id;
var w:;
run;
proc means data=temp noprint;
by customer_id col1 notsorted;
output out=temp1;
run;
data temp2;
 set temp1(where=(col1 is missing));
 by customer_id;
 if first.customer_id then count=0;
 count+1;
run;
proc transpose data=temp2 out=temp3 prefix=gap_;
by customer_id;
id count;
var _freq_;
run;
data want;
 merge have temp3;
 by customer_id;
 drop _name_;
run;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Mar 2017 04:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344874#M79300</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-28T04:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344963#M79318</link>
      <description>&lt;P&gt;I think the following changes fix the issue. I checked more thoroughly than I did before, but you'll want to check yourself, obviously. Looks like you have alternative solutions, too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input customer_id w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13;
datalines;
1 10 4 4 10 8 8 . . . . . . .
2 9 . 2 2 . . . . . . . . .
3 29 4 2 2 . . . . . . . 4 . 
4 8 4 2 4 . . . 2 . 4 . . .
5 . . 1 2 3 4 . 5 . 7 . . .
6 . 0 1 2 3 4 . 5 . 7 . . .
7 10 4 4 10 8 8 . . . . . 1 2
8 9 . 2 2 . . . . . . . . 1
9 . 1 . 2 . 4 . 4 . 9 . . 1
10 . 1 . 2 . 4 . 4 . 9 . 1 .
11 8 . 1 . 2 . 4 . 4 . 9 . 1
12 1 1 1 1 1 1 1 1 1 1 1 1 1
13 . . . 2 3 4 . 5 . 7 . . .
;

data want;
    set have;
    array w{*} w1 - w13;
    array len_gap_{7};
    array wk_gap_{7};

    gapLen = 0;
    gapNum = 0;

    do i = 1 to dim(w);
        if missing(w(i)) then do;
            gapLen = gapLen + 1;
            if gapLen = 1 then gapNum = gapNum + 1;
            if gapNum &amp;gt; 0 and gapLen = 1 then wk_gap_(gapNum) = i;
            if gapNum &amp;gt; 0 then len_gap_(gapNum) = gapLen;
        end;
        else do;
            gapLen = 0;
        end;
    end;

run;

proc print;
    var customer_id len_gap:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Mar 2017 13:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344963#M79318</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-03-28T13:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation of Gap between purchases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344965#M79319</link>
      <description>Thank You Ksharp,&lt;BR /&gt;&lt;BR /&gt;This woks perfectly &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Tue, 28 Mar 2017 13:42:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-of-Gap-between-purchases/m-p/344965#M79319</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-03-28T13:42:15Z</dc:date>
    </item>
  </channel>
</rss>

