<?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: Find number of repeat visits within a year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457767#M116105</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;there are several ways to perform.&lt;/P&gt;&lt;P&gt;I have 4 in mind:&lt;/P&gt;&lt;P&gt;- use the classic retain&lt;/P&gt;&lt;P&gt;- use proc SQL update&lt;/P&gt;&lt;P&gt;- use call execute&lt;/P&gt;&lt;P&gt;- use the new function dosubl for a more straight forward approach which blows my mind &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've described the retain and dosubl in the code below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
informat ID $1. TIME_IN mmddyy10.;
input id time_in;
format TIME_IN mmddyy10.;
datalines;
A 5/8/2015 
A 10/25/2016 
B 6/3/2016 
B 7/6/2016 
B 8/22/2016 
B 10/31/2016 
B 1/9/2017 
B 2/24/2017 
B 4/7/2017 
C 7/7/2014 
C 7/29/2014 
C 8/24/2015 
C 10/28/2015 
C 1/4/2016 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* RETAIN approach */
data test1;
   set test;
   by id time_in;
   retain min max h_repeatVisits .;
   if first.id then do;
      min=intnx("day", time_in, 1, "sameday");
      max=intnx("year", time_in, 1, "sameday");
      h_repeatVisits=0;
   end;
   if min&amp;lt;time_in&amp;lt;=max then h_repeatVisits=h_repeatVisits+1;
run;
proc sort data=test1; by id descending time_in;run; 

data test2;
   set test1;
   by id descending time_in;
   retain repeatVisits .;
   if first.id then repeatVisits=h_repeatVisits;
   drop min max h_repeatVisits;
run;
proc sort data=test2; by id time_in; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* DOSUBL approach */
data test3;
   set test;
   by id time_in;
   retain repeatVisits 0;
   if first.id then do;
      rc=dosubl('%LET n=0; 
                 PROC SQL noprint; 
                    SELECT count(*) INTO :n 
                    FROM test 
                    WHERE id EQ "'||strip(id)||'" 
                    AND '||put(time_in,best8.)||'&amp;lt;time_in&amp;lt;= '||put(intnx("year", time_in, 1, "sameday"),best8.)
             ||';QUIT;');
      repeatVisits=input(symget('n'),best8.);
   end;
   drop rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 Apr 2018 14:29:30 GMT</pubDate>
    <dc:creator>Oligolas</dc:creator>
    <dc:date>2018-04-26T14:29:30Z</dc:date>
    <item>
      <title>Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457721#M116078</link>
      <description>&lt;P&gt;Hello, I need to find the number of repeat visits within a year, starting from the initial visit. Ex., data is as below. A has 0 repeat visits within a year, B has 6, C has 1. I tried the LAG and INTCK functions, but couldn't get the correct answer.&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="156"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;ID&lt;/TD&gt;
&lt;TD width="100"&gt;TIME_IN&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;A&lt;/TD&gt;
&lt;TD width="100"&gt;5/8/2015&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;A&lt;/TD&gt;
&lt;TD width="100"&gt;10/25/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;6/3/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;7/6/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;8/22/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;10/31/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;1/9/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;2/24/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;B&lt;/TD&gt;
&lt;TD width="100"&gt;4/7/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;C&lt;/TD&gt;
&lt;TD width="100"&gt;7/7/2014&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;C&lt;/TD&gt;
&lt;TD width="100"&gt;7/29/2014&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;C&lt;/TD&gt;
&lt;TD width="100"&gt;8/24/2015&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;C&lt;/TD&gt;
&lt;TD width="100"&gt;10/28/2015&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;C&lt;/TD&gt;
&lt;TD width="100"&gt;1/4/2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 26 Apr 2018 13:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457721#M116078</guid>
      <dc:creator>Xinxin</dc:creator>
      <dc:date>2018-04-26T13:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457734#M116087</link>
      <description>&lt;P&gt;And what defines a repeat visit?&amp;nbsp; Post test data in the form of a datastep and show what you want to have output.&amp;nbsp; Then explain the logic of why.&amp;nbsp; As for your problem, simply:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain repeat;
  by id;
  if first.id then repeat=0;
  else repeat=repeat+1;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Apr 2018 13:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457734#M116087</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-26T13:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457735#M116088</link>
      <description>&lt;P&gt;I'm not sure I follow your logic? How does B have 6 repeat visits? What is the exact definition for a repeat visit?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 13:31:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457735#M116088</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-04-26T13:31:54Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457738#M116090</link>
      <description>&lt;P&gt;Here's a simple way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if first.id then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;repeat_visits=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;baseline_date = time_in;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else if time_in &amp;lt;= baseline_date + 365 then repeat_visits + 1;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;/P&gt;
&lt;P&gt;retain baseline_date;&lt;/P&gt;
&lt;P&gt;keep id baseline_date repeat_visits;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are fancier ways (other than adding 365) to define "within a year" including ways that account for leap years.&amp;nbsp; So it depends on how exact you want to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 13:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457738#M116090</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-26T13:35:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457767#M116105</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;there are several ways to perform.&lt;/P&gt;&lt;P&gt;I have 4 in mind:&lt;/P&gt;&lt;P&gt;- use the classic retain&lt;/P&gt;&lt;P&gt;- use proc SQL update&lt;/P&gt;&lt;P&gt;- use call execute&lt;/P&gt;&lt;P&gt;- use the new function dosubl for a more straight forward approach which blows my mind &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've described the retain and dosubl in the code below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
informat ID $1. TIME_IN mmddyy10.;
input id time_in;
format TIME_IN mmddyy10.;
datalines;
A 5/8/2015 
A 10/25/2016 
B 6/3/2016 
B 7/6/2016 
B 8/22/2016 
B 10/31/2016 
B 1/9/2017 
B 2/24/2017 
B 4/7/2017 
C 7/7/2014 
C 7/29/2014 
C 8/24/2015 
C 10/28/2015 
C 1/4/2016 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* RETAIN approach */
data test1;
   set test;
   by id time_in;
   retain min max h_repeatVisits .;
   if first.id then do;
      min=intnx("day", time_in, 1, "sameday");
      max=intnx("year", time_in, 1, "sameday");
      h_repeatVisits=0;
   end;
   if min&amp;lt;time_in&amp;lt;=max then h_repeatVisits=h_repeatVisits+1;
run;
proc sort data=test1; by id descending time_in;run; 

data test2;
   set test1;
   by id descending time_in;
   retain repeatVisits .;
   if first.id then repeatVisits=h_repeatVisits;
   drop min max h_repeatVisits;
run;
proc sort data=test2; by id time_in; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* DOSUBL approach */
data test3;
   set test;
   by id time_in;
   retain repeatVisits 0;
   if first.id then do;
      rc=dosubl('%LET n=0; 
                 PROC SQL noprint; 
                    SELECT count(*) INTO :n 
                    FROM test 
                    WHERE id EQ "'||strip(id)||'" 
                    AND '||put(time_in,best8.)||'&amp;lt;time_in&amp;lt;= '||put(intnx("year", time_in, 1, "sameday"),best8.)
             ||';QUIT;');
      repeatVisits=input(symget('n'),best8.);
   end;
   drop rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Apr 2018 14:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457767#M116105</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2018-04-26T14:29:30Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457769#M116107</link>
      <description>&lt;P&gt;Count the occurrence of year within each ID and subtract 1?&lt;/P&gt;
&lt;PRE&gt;proc summary data=have nway;
   class id date;
   format date year4.;
   output out=yearcount (drop=_type_);
run;&lt;/PRE&gt;
&lt;P&gt;will have a variable _freq_ with the count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assumes that your date is a SAS date value and NOT a character variable.&lt;/P&gt;
&lt;P&gt;A data step could subtract the 1.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 14:36:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457769#M116107</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-26T14:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of repeat visits within a year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457782#M116113</link>
      <description>&lt;P&gt;Thank you for the various methods&amp;nbsp;and increasing my knowledge ... I really appreciate it !!&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 15:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-repeat-visits-within-a-year/m-p/457782#M116113</guid>
      <dc:creator>Xinxin</dc:creator>
      <dc:date>2018-04-26T15:03:55Z</dc:date>
    </item>
  </channel>
</rss>

