Hi everyone,
I am a newer SAS user and am working on a project that requires me to create macro that creates a histogram with a variety of macros inside. I have created the following code, but although I get no errors or warnings in my log, I get no results. Can anyone help?
Thanks so much.
my code:
%MACRO mymacro ( DSNm = , Vbl = , Stats = N MEAN STDDEV ,
Ndec = 1);
TITLE1 "title1";
proc sgplot data = work.&DSNm;
histogram &Vbl/N MEAN STDDEV;
run;
%MEND title2;
%( DSNm = , Vbl = , Stats = N MEAN STDDEV , Ndec = 1);
my goal is to produce a histogram that looks like this:
The first thing you need to do: get the code to work correctly without any macro coding.
I got it all running, thanks for the feedback!
Here is one possible problem. Add spaces like below.
histogram &Vbl / N MEAN STDDEV; * Add spaces;
@rhaley1821 wrote:
Hi everyone,
I am a newer SAS user and am working on a project that requires me to create macro that creates a histogram with a variety of macros inside. I have created the following code, but although I get no errors or warnings in my log, I get no results. Can anyone help?
Thanks so much.
my code:
%MACRO mymacro ( DSNm = , Vbl = , Stats = N MEAN STDDEV ,
Ndec = 1);
TITLE1 "title1";
proc sgplot data = work.&DSNm;
histogram &Vbl/N MEAN STDDEV;
run;
%MEND title2;%( DSNm = , Vbl = , Stats = N MEAN STDDEV , Ndec = 1);
The code you show does not call the macro at all and I would expect you to get a number of errors from the statement:
%( DSNm = , Vbl = , Stats = N MEAN STDDEV , Ndec = 1);
Best practice when having any question about the results, or lack of results, is to copy the submitted code from the LOG and paste the copied text into a text box opened on the forum with the </> icon. Include the Code, notes, messages and errors for the section in doubt.
If you actually properly called the macro using
%mymacro( DSNm = , Vbl = , Stats = N MEAN STDDEV , Ndec = 1);
Then you did not provide a data set name or variable so the code created by this line
proc sgplot data = work.&DSNm;
would resolve to:
proc sgplot data = work. ;
with all sorts of "what data set to use" results.
The line
histogram &Vbl/N MEAN STDDEV;
Would resolve to
histogram /N MEAN STDDEV;
which if there were a data set available would plot ALL numeric variables.
The Parameter values are placed in the CODE where they macro variable names are used. If you don't provide a value then nothing appears in the code.
I think there is too much information being compiled in different statements.
Example:
"proc sgplot data = work.&DSNm;"
should be
"proc sgplot data = &DSNm;"
&DSNm is already going to specify a data set, so adding work. to it will mess things up.
"histogram &Vbl/N MEAN STDDEV;" is a mouthful of a histogram. A basic histogram shows one variable at a time.
Instead...
"histogram &Vbl;" should be sufficient.
Don't forget that even in a Macro you need to close off your titles and your subtitles. And you ask SAS to mend title 2 but Mend wouldn't apply to a regular title but to the macro name (i.e. MyMacro)
%MACRO mymacro ( DSNm = , Vbl = , Stats = N MEAN STDDEV ,
Ndec = 1);
TITLE1 "title1";
proc sgplot data = work.&DSNm;
histogram &Vbl/N MEAN STDDEV;
title;
run;
TITLE2 "Title2"
DATA_STEP_HERE
Title;
%MEND Mymacro
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!
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.