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)

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 4 replies
  • 934 views
  • 3 likes
  • 3 in conversation