Hello Everyone,
So I have the following code:
%macro tops(obs=3);
proc means data=orion.order_fact sum nway noprint;
var Total_Retail_Price;
class Customer_ID;
output out=customer_freq sum=sum;
run;
proc sort data=customer_freq;
by descending sum;
run;
data _null_;
set customer_freq(obs=&obs);
call symputx('top'||left(_n_), Customer_ID);
if no_more then call symputx('numcustid',_n_);
run;
%do i=1 %to &obs;
proc print data=orion.customer_dim noobs;
where Customer_ID in (&&top&i);
var Customer_ID Customer_Name Customer_Type;
title 'Top &numcustid Customers';
%end;
%mend tops;
%tops()
%tops(obs=5)
The problem that I am coming across is that I need to have my log output look like this:
MPRINT(TOPS): proc print data=orion.customer_dim noobs;
MPRINT(TOPS): where Customer_ID in ( 16 10 45);
MPRINT(TOPS): var Customer_ID Customer_Name Customer_Type;
MPRINT(TOPS): title "Top 3 Customers";
MPRINT(TOPS): run;
The code that I have above is the only way I could get anything to work, but I know that my log output does not match it at all.
Does anyone out there have any suggestions on how I can improve my code in order to make it look like the log output above?
Thanks!
Alisa
for this part of macro:
%do i=1 %to &obs;
proc print data=orion.customer_dim noobs;
where Customer_ID in (&&top&i);
var Customer_ID Customer_Name Customer_Type;
title 'Top &numcustid Customers';
%end;
move the loop inside, then you will get the log you want to see:
proc print data=orion.customer_dim noobs;
where Customer_ID in (%do i=1 %to &obs; &&top&i %end; );
var Customer_ID Customer_Name Customer_Type;
title 'Top &numcustid Customers';
Haikuo
for this part of macro:
%do i=1 %to &obs;
proc print data=orion.customer_dim noobs;
where Customer_ID in (&&top&i);
var Customer_ID Customer_Name Customer_Type;
title 'Top &numcustid Customers';
%end;
move the loop inside, then you will get the log you want to see:
proc print data=orion.customer_dim noobs;
where Customer_ID in (%do i=1 %to &obs; &&top&i %end; );
var Customer_ID Customer_Name Customer_Type;
title 'Top &numcustid Customers';
Haikuo
title needs double quote:
change 'Top &numcustid Customers';
to
"Top &numcustid Customers";
Thanks! This solved my problem with the title.
or
%macro tops(obs);
proc means data=orion.order_fact sum nway noprint;
var Total_Retail_Price;
class Customer_ID;
output out=customer_freq sum=sum;
run;
proc sort data=customer_freq;
by descending sum;
run;
data _null_;
set customer_freq(obs=&obs) end=last;
retain top;
top=catx(',',top,Customer_ID);
if last then call symputx('top',top);
run;
proc print data=orion.customer_dim noobs;
where Customer_ID in (&top);
var Customer_ID Customer_Name Customer_Type;
title "Top &obs Customers";
run;
%mend tops;
%tops(3)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.