BookmarkSubscribeRSS Feed
chitturiratna
Calcite | Level 5

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

 

 
1 REPLY 1
Cynthia_sas
SAS Super FREQ

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 819 views
  • 0 likes
  • 2 in conversation