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

I coverted a regular code (below)  into a macro that will produce either the frequency report or the descriptive
statistics report based on a value of a parameter that is passed to the macro.*/;

 

Original code

/*frequency report for month*/;
proc means data=VIRGINIAKEY N ; var month; class month;run;
/*lists basic descriptive statistics (N, mean, standard deviation, minimum, and maximum) for air temperature*/;
proc means data=VIRGINIAKEY ; var  AirTemp; run;


Myy Current trial code:

%macro freq_Describe (MNTH=month, AIR_Temp=AirTemp );
/*frequency report for month*/;
proc means data=VIRGINIAKEY N ; var &MNTH.; class &MNTH.;run;
/*lists basic descriptive statistics (N, mean, standard deviation, minimum, and maximum) for air temperature*/;
proc means data=VIRGINIAKEY ; var &AIR_Temp.; run;
%mend freq_Describe;

%freq_Describe (MNTH=month , AIR_Temp=AirTemp);

 

 

Is there a way to

A) add a parameter to the macro that can control which variable is used for the report and

 

B) add titles that reflect the type of report (frequency or descriptive statistics) including a short description of the variable (specifying the description of the variable as a parameter for the macro)?

 

Any advice will be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What is the purpose of writing such convoluted code?

%macro rpt/parmbuff;
   %let dsname = %scan(&syspbuff,1,(,));
   %let title = %scan(&syspbuff,2,(,));
   %let proc = %scan(&syspbuff,3,(,));
   %let cls = %scan(&syspbuff,4,(,));
   %let var = %scan(&syspbuff,5,(,));
   %let stats = %scan(&syspbuff,6,(,));

Just define the parameters in the %MACRO statement itself.

%macro rpt(dsname,title,proc,cls,var,stats);

 

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Of course you can.

 

It looks like you already know how to add parameters to a macro.  Next, you need to decide which parameters you want to add ... what the names of them will be and what the values will be that can be assigned to these parameters.

 

Then you need to learn about %IF %THEN statements so the macro can use those parameters to control the results and titles.

 

Note that this is a good learning exercise.  However, it is a poor choice for anything more than that.  You are better off writing PROC MEANS as needed instead of trying to convert your code to a macro.  Macros do not add to your knowledge of SAS.  Just the opposite.  They reduce your chance to become more familiar with the software by writing the program for you.  At this point, you would learn more by writing PROC MEANS yourself and postponing learning about macros.

unison
Lapis Lazuli | Level 10

Here's an example of something you might do!

 

%macro rpt(dsname,title,proc,cls,var,stats);

   ods noproctitle;
   title &title.;
   options nolabel;
   
   %if &proc.="means" %then %do;
   proc means data=&dsname. nonobs &stats.;
   class &cls.;
   var &var.;
   run;
   %end;
   
   %else %if &proc.="freq" %then %do;
   proc freq data=&dsname.;
   tables &var.(&cls.) / &stats.;
   run;
   %end;
   
%mend rpt;
*(dataset_name,"title","means/freq",class_vars,summary_vars,statistics/options);
%rpt(sashelp.cars, "My Means Summary!", "means", make, mpg_city, n std)
%rpt(sashelp.cars, "My Freq Summary!", "freq", make, mpg_city, nocol norow)

 

-unison

-unison
Tom
Super User Tom
Super User

What is the purpose of writing such convoluted code?

%macro rpt/parmbuff;
   %let dsname = %scan(&syspbuff,1,(,));
   %let title = %scan(&syspbuff,2,(,));
   %let proc = %scan(&syspbuff,3,(,));
   %let cls = %scan(&syspbuff,4,(,));
   %let var = %scan(&syspbuff,5,(,));
   %let stats = %scan(&syspbuff,6,(,));

Just define the parameters in the %MACRO statement itself.

%macro rpt(dsname,title,proc,cls,var,stats);

 

unison
Lapis Lazuli | Level 10

@Tom - that definitely does simplify things. 

 

Thanks!

 

-unison

-unison

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 658 views
  • 6 likes
  • 4 in conversation