BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shradha1
Obsidian | Level 7

I have a list of customers, and against each customer I have their monthly Spend for 13 consecutive months from M1 to M12.

Now, I want to select only those customers who have had zero spends for at least 6 or more consecutive months(inactive customers), and also the month from when their spend started reappearing post inactivity. 

My dataset looks like this:

Customer M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12
A . 94 63 106 424 252 499 356 435 469 200 423
B . . . . . . . 13 137 440 75 99
C 67 118 364 . . . . . . 156 40 415
D 430 423 . . . . 54 165 26 477 129 411

 

So, from this dataset, I would select Customer B and C, as they have null spends for 6 or more consecutive months and for customer B, the month when spend restarted would be M8, and it will be M10 for Customer C.

So, my resultant dataset will look like this:

Customer Month
B M8

M10

 

How do I achieve this? Any help would be much appreciated.

Thanks a lot! 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input Customer	$ M1	M2	M3	M4	M5	M6	M7	M8	M9	M10	M11	M12;
cards;
A	.	94	63	106	424	252	499	356	435	469	200	423
B	.	.	.	.	.	.	.	13	137	440	75	99
C	67	118	364	.	.	.	.	.	.	156	40	415
D	430	423	.	.	.	.	54	165	26	477	129	411
;

data want;
set have;
array x{*} M: ;
count=0;
do i=1 to dim(x);
 if missing(x{i}) then count+1;
  else do;
         count=0;
		 if _count>5 then month=vname(x{i});
	   end;
  _count=count;
end;
drop count _count i;
run;

View solution in original post

3 REPLIES 3
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input Customer	$ M1	M2	M3	M4	M5	M6	M7	M8	M9	M10	M11	M12;
cards;
A	.	94	63	106	424	252	499	356	435	469	200	423
B	.	.	.	.	.	.	.	13	137	440	75	99
C	67	118	364	.	.	.	.	.	.	156	40	415
D	430	423	.	.	.	.	54	165	26	477	129	411
;

data want;
set have;
array x{*} M: ;
count=0;
do i=1 to dim(x);
 if missing(x{i}) then count+1;
  else do;
         count=0;
		 if _count>5 then month=vname(x{i});
	   end;
  _count=count;
end;
drop count _count i;
run;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Shradha1 

It is good practise to supply example input data not as a table, but as a data step that can be run to create a sas data set, so anybody answering your post has something to work on.

 

I think in this case the best way is to use an array to hold all months 1-12 for a given costomer and then traverse the array and count consecutive missing months. If there are at least 6 consecutive missing months, then store the the month number where the costomer reappears, if this happens.  

 

* Create test data ;
data have;
input Customer$ M1-M12;
cards;
A	.	94	63	106	424	252	499	356	435	469	200	423
B	.	.	.	.	.	.	.	13	137	440	75	99
C	67	118	364	.	.	.	.	.	.	156	40	415
D	430	423	.	.	.	.	54	165	26	477	129	411
;
run;

* Analyze;
data want (keep=Customer ReAppear); 
	set have;

	* Declare array containing 12 months and initiate counters;
	array spend M1-M12;
	Zerospend = 0;
	ReAppear = 0;

	* Loop over array, count consecutive missings and set reapprearing month;
	do i = 1 to 12;
		if missing(Spend{i}) then Zerospend = Zerospend + 1;
		else do;
			if Zerospend >= 6 then ReAppear = i;
			Zerospend = 0;
		end;
	end;

	* Output only if conditions are met;
	if ReAppear > 0 then output;
run;

The result is B 8 and C 10.

 

 

Shradha1
Obsidian | Level 7
Thank you for the response! This helped a lot

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 579 views
  • 0 likes
  • 3 in conversation