%macro HistAndStats (DSNM= , VBL= Stats=N MEAN STDDEV , NDEC =1);
TITLE "Histogram for '&VBL' from Data set '...' " ;
PROC SGPLOT DATA= &DSNM;
HISTOGRAM &VBL;
RUN;
TITLE "Summary Statistics for '&VBL'from Data Set '&DSNM' ";
PROC MEANS DATA= &.. &...
VAR &VBL;
RUN;
%MEND HistAndStats;
Hello SAS Communities,
I am working on a code but have some errors, below what I need to do:-
a macro program named ‘HistAndStats’ which may be used to summarize a numeric variable from any data set.
The macro should contain the following 4 parameters:
My Code looks like this
%macro HistAndStats (DSNM= , VBL= Stats=N MEAN STDDEV , NDEC =1);
TITLE "Histogram for '&VBL' from Data set '&DSNM' " ;
PROC SGPLOT DATA= ...;
HISTOGRAM &VBL;
RUN;
TITLE "Summary Statistics for '&VBL'from Data Set '&DSNM' ";
PROC MEANS DATA= ... &...
VAR &VBL;
RUN;
%MEND HistAndStats;
And I have attached the error on my LOG
Thank you so much!
The message in your picture indicates a problem with unbalanced quotes earlier in your session. Restart your session, and submit ONLY your macro code, then post the log as requested.
Please post the log by copy/pasting it into a window opened with the </> button. Use the "little running man" right next to it for posting code.
The message in your picture indicates a problem with unbalanced quotes earlier in your session. Restart your session, and submit ONLY your macro code, then post the log as requested.
Please show the code that worked without any macro elements.
If you don't have such, that is the first step in developing macro programming.
Many users here don't want to download Microsoft Office files because of virus potential, others have such things blocked by security software.
It is much better to copy text from the Log and paste into a code box opened on the forum using the </> icon.
Here are some good macro references for you:
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
And your code that should work (untested)
%macro HistAndStats (DSNM= ,
VBL= ,
Stats=N MEAN STDDEV ,
NDEC =1);
TITLE "Histogram for &VBL. from Data set &DSNM " ;
PROC SGPLOT DATA= &DSNM;
HISTOGRAM &VBL;
RUN;
TITLE "Summary Statistics for &VBL from Data Set &DSNM";
PROC MEANS DATA= &DSNM &STATS MaxDec = &NDEC;
VAR &VBL;
RUN;
%MEND HistAndStats;
@sulafa wrote:
Hello SAS Communities,
I am working on a code but have some errors, below what I need to do:-
a macro program named ‘HistAndStats’ which may be used to summarize a numeric variable from any data set.
The macro should contain the following 4 parameters:
- ‘DSNm’ to specify the data set name
- ‘Vbl’ to specify the numeric variable
- ‘Stats’ to specify summary statistics. Include default statistics of N, MEAN, and STDDEV.
- ‘NDec’ to specify the number of decimals. Include a default value of 1.
My Code looks like this
%macro HistAndStats (DSNM= , VBL= Stats=N MEAN STDDEV , NDEC =1);
TITLE "Histogram for '&VBL' from Data set '&DSNM' " ;
PROC SGPLOT DATA= &DSNM;
HISTOGRAM &VBL;
RUN;TITLE "Summary Statistics for '&VBL'from Data Set '&DSNM' ";
PROC MEANS DATA= &DSNM &STATS
VAR &VBL;
RUN;
%MEND HistAndStats;
And I have attached the error on my LOG
Thank you so much!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.