I want to covert 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);
usiing my trial code, I got several errors
NOTE: The SAS System stopped processing this step because of errors.NOTE: PROCEDURE MEANS used (Total process time):real time 0.00 secondscpu time 0.00 secondsNOTE: Line generated by the invoked macro "FREQ_DESCRIBE".81 proc means data=VIRGINIAKEY ; var &AIR_Temp.; run;_22200WARNING: Apparent symbolic reference AIR_TEMP not resolved.ERROR 22-322: Syntax error, expecting one of the following: a name, ;, /, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.ERROR 200-322: The symbol is not recognized and will be ignored.NOTE: The SAS System stopped processing this step because of errors.NOTE: PROCEDURE MEANS used (Total process time):real time 0.00 secondscpu time 0.01 secondsNOTE: Line generated by the macro variable "MNTH".81 month ; AIR_Temp=AirTemp________180NOTE: Line generated by the macro variable "MNTH".81 month ; AIR_Temp=AirTemp________180ERROR 180-322: Statement is not valid or it is used out of proper order.828384 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;96
Any advice will be greatly appreciated.
There are at least two problems with your code. You need commas between macro parameters in your MACRO statement and when you call it:
%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);
There are at least two problems with your code. You need commas between macro parameters in your MACRO statement and when you call it:
%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);
It works . Thanks a lot.
@mrahouma - I'm not sure what you mean by "add a parameter to the macro that can control which variable is used for the report". If you could post where this variable would go in your code, then that would make it a lot clearer. Here is an example of adding a title.
%macro freq_Describe (MNTH=month, AIR_Temp=AirTemp, MyTitle = );
/*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 ;
title1 "&MyTitle";
var &AIR_Temp.; run;
%mend freq_Describe;
%freq_Describe (MNTH=month, AIR_Temp=AirTemp, MyTitle = Air Temperatures);
Thanks a lot for your follow-up reply. Assuming after preparation of our macro , we need to get one of the two included variable (i.e. month or airTemp only ). Consequently we need to edit the results to convey that. I tried to see if the current macro can give the results of 1 variable eg month but it doesn't. Any advice will be greatly appreciated.
Please provide an example of the data you have and the expected output you want.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.