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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

4 REPLIES 4
Haikuo
Onyx | Level 15

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

Linlin
Lapis Lazuli | Level 10

title needs double quote:

change   'Top &numcustid Customers';

to

     "Top &numcustid Customers";

InfoAlisaA
Calcite | Level 5

Thanks! This solved my problem with the title. Smiley Happy

Linlin
Lapis Lazuli | Level 10

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)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1618 views
  • 3 likes
  • 3 in conversation