Help using Base SAS procedures

Need a code check on Data-Dependent Macros

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

Need a code check on Data-Dependent Macros

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


Accepted Solutions
Solution
‎03-31-2012 09:21 PM
Respected Advisor
Posts: 3,156

Need a code check on Data-Dependent Macros

Posted in reply to InfoAlisaA

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


All Replies
Solution
‎03-31-2012 09:21 PM
Respected Advisor
Posts: 3,156

Need a code check on Data-Dependent Macros

Posted in reply to InfoAlisaA

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

Super Contributor
Posts: 1,636

Need a code check on Data-Dependent Macros

title needs double quote:

change   'Top &numcustid Customers';

to

     "Top &numcustid Customers";

Frequent Contributor
Posts: 90

Need a code check on Data-Dependent Macros

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

Super Contributor
Posts: 1,636

Need a code check on Data-Dependent Macros

Posted in reply to InfoAlisaA

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)

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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