## count recent matches

Solved
Frequent Contributor
Posts: 87

# count recent matches

[ Edited ]

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 for last 4 months and Bob has been living in philadelphia for last 2 months.

How do I code to get the RESULT for the DATA I have?

Accepted Solutions
Solution
‎11-09-2016 10:58 AM
Super User
Posts: 10,395

## Re: count recent matches

```
Maybe you want this :

data have;
informat name \$12. city \$20. period yymmn6.;
input Name 	City  	Period ;
format period yymmn6.;
cards;
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob chicago 201605
bob chicago 201606
;
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;

```

All Replies
Super User
Posts: 21,936

## Re: count recent matches

There's probably many ways, but using first/last is relatively straightforward.

``````data have;
informat name \$12. city \$20. period yymm6.;
input Name 	City  	Period ;
cards;
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob chicago 201605
bob chicago 201606
;
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;``````
Super User
Posts: 10,395

## Re: count recent matches

```
data have;
informat name \$12. city \$20. period yymmn6.;
input Name 	City  	Period ;
format period yymmn6.;
cards;
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob chicago 201605
bob chicago 201606
;
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;

```
Solution
‎11-09-2016 10:58 AM
Super User
Posts: 10,395

## Re: count recent matches

```
Maybe you want this :

data have;
informat name \$12. city \$20. period yymmn6.;
input Name 	City  	Period ;
format period yymmn6.;
cards;
john miami   201607
john miami   201608
john miami   201609
john miami   201610
bob chicago 201601
bob chicago 201602
bob chicago 201605
bob chicago 201606
;
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;

```
☑ This topic is solved.