Macros -- %do & print ; Output procedure

Reply
Senior User
Posts: 1

Macros -- %do & print ; Output procedure

This question is from SAS Macro Language 1; 

 

Question : 

Reminder: Make sure you've defined the Orion library.

  1. Copy and paste the following macro into the editor. This macro creates a data set named Customer_Freq, which summarizes the variable Total_Retail_Price by Customer_ID. The data is sorted in descending order by the value of Sum. The call SYMPUTX routine creates a series of variables named Top1 through Topx, where x is the value of the Obs parameter.
    title;
    footnote;
    %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);
       run;
    %mend tops;
  2. Modify the macro to print a list of the top x customers from the orion.customer_dim data set. Display the variables Customer_ID, Customer_Name, and Customer_Type. Use a macro loop to dynamically generate values for the WHERE statement based on the macro variables Top1 through Topx.

  3. Call the macro with a null parameter value and view the results.

 

my code:

 


title;
footnote;
%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);

run;


%do i = 1 %to &obs ;
proc print data = orion.customer_dim;
where Customer_ID = &&top&i ;

run;
%end ;

%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);

run;


%do i = 1 %to &obs ;
proc print data = orion.customer_dim;
where Customer_ID = &&top&i ;

run;
%end ;

 

%mend tops;

%tops();

 

My output is in a different format. Can someone help me to get the output in this format. 

 

Top 3 Customers

 

Customer_ID Customer_Name Customer_Type
10Karen BallingerOrion Club members high activity
16Ulrich HeydeInternet/Catalog Customers
45Dianne PatchinOrion Club Gold members low activity

 

 
SAS Super FREQ
Posts: 9,033

Re: Macros -- %do & print ; Output procedure

Posted in reply to chitturiratna

Hi:

  The exercises in the e-learning classes have the solution provided to you. Just click the Show Solution button at the bottom of the screen and you should see the solution inside the code that is showing on a blue background, as shown below.

 

cynthia

topx_macro_solution.png

Ask a Question
Discussion stats
  • 1 reply
  • 113 views
  • 0 likes
  • 2 in conversation