<?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: count recent matches in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310280#M66909</link>
    <description>&lt;PRE&gt;

data have;
informat name $12. city $20. period yymmn6.;
input Name 	City  	Period ;
format period yymmn6.;
cards;
john philadelphia 201601
john philadelphia 201602
john philadelphia 201603
john philadelphia 201604
john philadelphia 201605
john philadelphia 201606
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob philadelphia 201603
bob philadelphia 201604
bob chicago 201605
bob chicago 201606
bob philadelphia 201609
bob philadelphia 201610
;
run;
data temp;
 do until(last.city);
  set have;
  by name city notsorted;
 end;
 flag=(intnx('month',today(),-1)=period);
 do until(last.city);
  set have;
  by name city notsorted;
  if flag then output;
 end;
 drop flag;
run;
proc freq data=temp;
 tables name*city/list out=want;
run;


&lt;/PRE&gt;</description>
    <pubDate>Wed, 09 Nov 2016 05:44:13 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-11-09T05:44:13Z</dc:date>
    <item>
      <title>count recent matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310267#M66902</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have provided DATA and RESULT tables below. I want to check for how many months an individual has been living in his current city. for example: John has been living in miami&amp;nbsp;for last 4 months and Bob has been living in philadelphia&amp;nbsp;for last 2 months.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I code to get the RESULT for the DATA I have?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="220"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="56"&gt;DATA&lt;/TD&gt;
&lt;TD width="82"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="82"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Name&lt;/TD&gt;
&lt;TD&gt;City&lt;/TD&gt;
&lt;TD&gt;Month&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;philadelphia&lt;/TD&gt;
&lt;TD&gt;201601&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;philadelphia&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;201602&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;philadelphia&lt;/TD&gt;
&lt;TD&gt;201603&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;philadelphia&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;201604&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;philadelphia&lt;/TD&gt;
&lt;TD&gt;201605&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;philadelphia&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;201606&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;miami&lt;/TD&gt;
&lt;TD&gt;201607&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;miami&lt;/TD&gt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;TD&gt;201608&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;bob&lt;/TD&gt;
&lt;TD&gt;philadelphia&lt;/TD&gt;
&lt;TD&gt;201609&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;bob&lt;/TD&gt;
&lt;TD&gt;philadelphia&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;201610&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;RESULT&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Name&lt;/TD&gt;
&lt;TD&gt;City&lt;/TD&gt;
&lt;TD&gt;Time_in_current_city&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;john&lt;/TD&gt;
&lt;TD&gt;miami&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;bob&lt;/TD&gt;
&lt;TD&gt;philadelphia&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 05:00:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310267#M66902</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2016-11-09T05:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: count recent matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310277#M66907</link>
      <description>&lt;P&gt;You edited your question so the answer met the question?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's probably many ways, but using first/last is relatively straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
informat name $12. city $20. period yymm6.;
input Name 	City  	Period ;
cards;
john philadelphia 201601
john philadelphia 201602
john philadelphia 201603
john philadelphia 201604
john philadelphia 201605
john philadelphia 201606
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob philadelphia 201603
bob philadelphia 201604
bob chicago 201605
bob chicago 201606
bob philadelphia 201607
bob philadelphia 201608
bob philadelphia 201609
bob philadelphia 201610
;
run;

proc sort data=have;
by name descending period;
run;

data want;
set have;
by name city notsorted;
retain flag num_months;
if first.name and first.city then flag=1;
if first.city and not first.name then do; flag=0;num_months=0; end;
num_months+flag;

if last.city and flag=1;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 05:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310277#M66907</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-09T05:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: count recent matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310280#M66909</link>
      <description>&lt;PRE&gt;

data have;
informat name $12. city $20. period yymmn6.;
input Name 	City  	Period ;
format period yymmn6.;
cards;
john philadelphia 201601
john philadelphia 201602
john philadelphia 201603
john philadelphia 201604
john philadelphia 201605
john philadelphia 201606
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob philadelphia 201603
bob philadelphia 201604
bob chicago 201605
bob chicago 201606
bob philadelphia 201609
bob philadelphia 201610
;
run;
data temp;
 do until(last.city);
  set have;
  by name city notsorted;
 end;
 flag=(intnx('month',today(),-1)=period);
 do until(last.city);
  set have;
  by name city notsorted;
  if flag then output;
 end;
 drop flag;
run;
proc freq data=temp;
 tables name*city/list out=want;
run;


&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 05:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310280#M66909</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-09T05:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: count recent matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310310#M66919</link>
      <description>&lt;PRE&gt;

Maybe you want this :



data have;
informat name $12. city $20. period yymmn6.;
input Name 	City  	Period ;
format period yymmn6.;
cards;
john philadelphia 201601
john philadelphia 201602
john philadelphia 201603
john philadelphia 201604
john philadelphia 201605
john philadelphia 201606
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob philadelphia 201603
bob philadelphia 201604
bob chicago 201605
bob chicago 201606
bob philadelphia 201609
bob philadelphia 201610
;
run;
data temp;
 do until(last.city);
  set have;
  by name city notsorted;
 end;
 flag=last.name;
 do until(last.city);
  set have;
  by name city notsorted;
  if flag then output;
 end;
 drop flag;
run;
proc freq data=temp;
 tables name*city/list out=want;
run;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 08:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-recent-matches/m-p/310310#M66919</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-09T08:41:19Z</dc:date>
    </item>
  </channel>
</rss>

