<?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: Calculate mean &amp;amp; range between 1st and 2nd visit (dates) per Location in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264458#M51863</link>
    <description>&lt;P&gt;Thanks for response.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the suggested code and got the following output: &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Visit = &amp;nbsp;01JAN60 for all rows. &amp;nbsp;Days_between and date = &amp;nbsp;. (missing)&amp;nbsp;&lt;/P&gt;&lt;P&gt;Log states &amp;nbsp;'date is uinitialized'&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also I'm wondering the code line --&amp;gt; &amp;nbsp;dif(date); &amp;nbsp;It doesn't highlight in blue as yours does....&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also is it not working b/c they're not SAS dates? &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 17 Apr 2016 23:03:46 GMT</pubDate>
    <dc:creator>soulful63</dc:creator>
    <dc:date>2016-04-17T23:03:46Z</dc:date>
    <item>
      <title>Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264449#M51858</link>
      <description>&lt;P&gt;Good afternoon,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much on your assistance on the following...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS 9.3 ( The Basic version... NOT Enterprise, etc.). &amp;nbsp;I have a list of 500 people who each traveled to one city on a specific date (Visit) . &amp;nbsp;What I would like to know is what is the range in days between the 1st and 2nd visit per city, then calculate the mean of these ranges ( based on cities with 2 or more visitis). &amp;nbsp; What I did was sort the data by City &amp;amp; then by Visit (date variable). &amp;nbsp;When I did this, &amp;nbsp;found there is at least one visit per city.&amp;nbsp;For some cities, there were only 2 visits, &amp;nbsp;and for other cities&amp;nbsp;there were &amp;gt; &amp;nbsp;2 visits. &amp;nbsp;Figured out how to find range of first and last visit per city (in days) , and easily calculated mean by hand. &amp;nbsp;(see code below)...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc tabulate data = a;&lt;BR /&gt;class city;&lt;BR /&gt;var visit;&lt;BR /&gt;table city*visit, n nmiss (min max mean)*f=mmddyy10. range;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But not sure how to calculate range per city based on 1st and 2nd visit only. &amp;nbsp;Finding the mean of these ranges ( 2 or more visits) I can do by hand pretty quickly, but if possible to code easily, then I would love to know as well. I sincerely appreciate the help :-). &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is sample data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;City &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Visit&lt;/P&gt;&lt;P&gt;Atlanta &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22-Aug-15&lt;/P&gt;&lt;P&gt;Franklin &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11-Nov-15&lt;/P&gt;&lt;P&gt;Licoln Heights &amp;nbsp; &amp;nbsp;25-Oct-15&lt;/P&gt;&lt;P&gt;New Mexico &amp;nbsp; &amp;nbsp; &amp;nbsp;17-Sep-15&lt;/P&gt;&lt;P&gt;Palm Springs &amp;nbsp; &amp;nbsp; 04-Aug-15&lt;/P&gt;&lt;P&gt;Palm Springs &amp;nbsp; &amp;nbsp; 23-Aug-15&lt;/P&gt;&lt;P&gt;Palm Springs &amp;nbsp; &amp;nbsp; 09-Sep-15&lt;/P&gt;&lt;P&gt;Palm Springs &amp;nbsp; &amp;nbsp; 11-Oct-15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;San Diego &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;21-Aug-15&lt;/P&gt;&lt;P&gt;San Diego &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 18-Sep-15&lt;/P&gt;&lt;P&gt;San Diego &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 29-Sep-15&lt;/P&gt;&lt;P&gt;Temecula &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25-Sep-15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Apr 2016 21:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264449#M51858</guid>
      <dc:creator>soulful63</dc:creator>
      <dc:date>2016-04-17T21:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264450#M51859</link>
      <description>&lt;P&gt;So you want:&lt;/P&gt;
&lt;P&gt;-if 2 visits, calculate the range&lt;/P&gt;
&lt;P&gt;-if more than 2 visits, calculate the mean of the ranges of subsequent visits?&lt;/P&gt;
&lt;P&gt;So if you have&lt;/P&gt;
&lt;P&gt;San Diego &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 18-Sep-15&lt;/P&gt;
&lt;P&gt;San Diego &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 27-Sep-15&amp;nbsp; range=9&lt;/P&gt;
&lt;P&gt;San Diego&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28-Sep-15&amp;nbsp; range=1&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;San Diego&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30-Sep-15&amp;nbsp; range=2&amp;nbsp;&amp;nbsp;&amp;nbsp; mean=12/3=4&lt;/P&gt;
&lt;P&gt;Is that right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Apr 2016 22:11:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264450#M51859</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-04-17T22:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264451#M51860</link>
      <description>&lt;P&gt;Assuming you have SAS dates use the dif function in conjunction with by processing. You can add a counter that resets at each city to count visits and only keep second occurrence of desired. I've commented it out below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
Set have;
By id city;

Days_between = dif(date);
If first.city then days_between = .;
&lt;BR /&gt;If first.city then visit=1;&lt;BR /&gt;else visit+1;&lt;BR /&gt;&lt;BR /&gt;*if visit ne 2 then delete;&lt;BR /&gt;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 17 Apr 2016 22:12:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264451#M51860</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-17T22:12:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264452#M51861</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would only want to calculate the range between 1st and 2nd visit for cities with 2 or more visits. &amp;nbsp;So for this San Diego example, &amp;nbsp;I would just want range = 9, &amp;nbsp;then let's say for the next city Palm Springs has 3 visits, &amp;nbsp;and the range between the first &amp;amp; second visit = &amp;nbsp;7, and then the next city, Temecula only has 1 visit, so range = 0. &amp;nbsp;The average would be &amp;nbsp;( (9+7)/2) &amp;nbsp;= &amp;nbsp;8. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Apr 2016 22:19:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264452#M51861</guid>
      <dc:creator>soulful63</dc:creator>
      <dc:date>2016-04-17T22:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264457#M51862</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WANT;
  if LASTOBS then do;                     %* Output mean;
    MEAN=SUM/N;
    output; 
  end;
  set HAVE end=LASTOBS;
  by CITY;
  RANGE_FIRST2 = dif(VISIT);              %* Derive range;
  if lag(first.CITY) and not first.CITY;  %* Only keep 2nd city record;
  SUM+RANGE_FIRST2;                       %* Sum the ranges;
  N+1;                                    %* Count the ranges;
run;

proc print;
  var MEAN;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE class="table" rules="all" frame="box" cellspacing="0" cellpadding="5" summary="Procedure Print: Data Set WORK.WANT"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;MEAN&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="r data"&gt;23.5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sun, 17 Apr 2016 22:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264457#M51862</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-04-17T22:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264458#M51863</link>
      <description>&lt;P&gt;Thanks for response.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the suggested code and got the following output: &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Visit = &amp;nbsp;01JAN60 for all rows. &amp;nbsp;Days_between and date = &amp;nbsp;. (missing)&amp;nbsp;&lt;/P&gt;&lt;P&gt;Log states &amp;nbsp;'date is uinitialized'&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also I'm wondering the code line --&amp;gt; &amp;nbsp;dif(date); &amp;nbsp;It doesn't highlight in blue as yours does....&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also is it not working b/c they're not SAS dates? &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Apr 2016 23:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264458#M51863</guid>
      <dc:creator>soulful63</dc:creator>
      <dc:date>2016-04-17T23:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264459#M51864</link>
      <description>&lt;P&gt;Yes! &amp;nbsp;This worked! &amp;nbsp;Thank you so much! &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Apr 2016 23:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264459#M51864</guid>
      <dc:creator>soulful63</dc:creator>
      <dc:date>2016-04-17T23:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264466#M51867</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input City  &amp;amp; $40.                 Visit : date11.;
format visit date9.;
cards;
Atlanta                22-Aug-15
Franklin              11-Nov-15
Licoln Heights    25-Oct-15
New Mexico      17-Sep-15
Palm Springs     04-Aug-15
Palm Springs     23-Aug-15
Palm Springs     09-Sep-15
Palm Springs     11-Oct-15
San Diego          21-Aug-15
San Diego         18-Sep-15
San Diego         29-Sep-15
Temecula          25-Sep-15
;
run;
proc sort data=have;by city visit;run;
data want;
 set have;
 by city ;
 if first.city then n=0;
 n+1;
 dif=dif(visit);
 if n=2;
run;
proc means data=want mean;
 var dif;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Apr 2016 01:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264466#M51867</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-18T01:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264469#M51869</link>
      <description>Yes, this works! I'm so appreciative for this!&lt;BR /&gt;Thank you!</description>
      <pubDate>Mon, 18 Apr 2016 01:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264469#M51869</guid>
      <dc:creator>soulful63</dc:creator>
      <dc:date>2016-04-18T01:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264473#M51870</link>
      <description>&lt;P&gt;The more, the merrier!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data meanLapse;
set have end=done; by City;
wasFirstCity = lag(first.city);
InitialVisit = lag(visit);
if wasFirstCity and not first.city then do;
    sumLapse + intck("DAY", InitialVisit, visit);
    nLapse + 1;
    end;
if done then do;
    meanLapse = sumLapse / nLapse;
    output;
    end;
keep nLapse meanLapse;
run;

proc print data=meanLapse noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: Sorry &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ﻿&lt;/a&gt;, I hadn't realized that this was almost identical to your solution. &lt;img id="smileyembarrassed" class="emoticon emoticon-smileyembarrassed" src="https://communities.sas.com/i/smilies/16x16_smiley-embarrassed.png" alt="Smiley Embarassed" title="Smiley Embarassed" /&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 03:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264473#M51870</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-18T03:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264474#M51871</link>
      <description>&lt;P&gt;Thanks so much! &amp;nbsp;I appreciate it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 03:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264474#M51871</guid>
      <dc:creator>soulful63</dc:creator>
      <dc:date>2016-04-18T03:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean &amp; range between 1st and 2nd visit (dates) per Location</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264909#M52022</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&amp;nbsp;No worries. Great minds think alike. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2016 21:28:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-amp-range-between-1st-and-2nd-visit-dates-per/m-p/264909#M52022</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-04-19T21:28:58Z</dc:date>
    </item>
  </channel>
</rss>

