DATA Step, Macro, Functions and more

How do I select a customer with a pattern in Score using Macro?

Reply
New Contributor
Posts: 2

How do I select a customer with a pattern in Score using Macro?

Hello All,

 

I need find out which CUSTOMER has given a "5" score immediately followed by a missing score (among the data shown above, it is C).

 

 

*Q10;
data feedback;
infile datalines missover;
input Customer $
	  Score 
	  Comment $30. ;
datalines;
A	3	The is no parking
A	5	The food is expensive
B	.	I like the food
C	5	It tastes good
C	.	 
C	3	I like the drink
D	4	The dessert is tasty
D	2	I don't like the services
;
run;
proc print data = feedback;
run;

proc sql;
select count(distinct Customer) into :n from feedback;
select distinct Customer into :cust1 - from feedback;
run;

%macro main();
%do i=1 %to &n.;
 data feedback_&&cust&i; set feedback;
 where Customer = "&&cust&i";
run;
%feedback_mn(cust=&&cust&i);
%end;
%mend;

%macro feedback_mn(cust=);
	data have;
	set feedback;
	where Customer = "&&cust&i";
 %if %length(Score)= 0 and lag1(Score) = 5 %then output;
 run;

 proc print data  = have;
 run;
%mend;
	
%main();

 

 

Any help is highly appreciated. please check the code, I am getting this error,

 

A character operand was found in the %EVAL function or %IF condition where a numeric operand
is required. The condition was: %length(Score)= 0 and lag1(Score) = 5
ERROR: The macro FEEDBACK_MN will stop executing.

Super User
Posts: 10,849

Re: How do I select a customer with a pattern in Score using Macro?

Posted in reply to arunvaibhav21
data feedback;
infile datalines missover;
input Customer $
	  Score 
	  Comment $30. ;
datalines;
A	3	The is no parking
A	5	The food is expensive
B	.	I like the food
C	5	It tastes good
C	.	 
C	3	I like the drink
D	4	The dessert is tasty
D	2	I don't like the services
;
run;
data _null_;
 set feedback;
 if customer=lag(customer) and score=. and lag(score)=5 then put (_all_) (:);
run;
New Contributor
Posts: 2

Re: How do I select a customer with a pattern in Score using Macro?

Hello,

 

Thanks for the answer. But I dont see the output?

 

Thanks,

Arun

Super User
Posts: 10,849

Re: How do I select a customer with a pattern in Score using Macro?

Posted in reply to arunvaibhav21

If you want a table .

 

data want;
 set feedback;
 if customer=lag(customer) and score=. and lag(score)=5 ;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 105 views
  • 0 likes
  • 2 in conversation