BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sulafa
Obsidian | Level 7
%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:

  • ‘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= ...;
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!

 

 

 
 
 

 

sulafa
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

10 REPLIES 10
Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

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.

sulafa
Obsidian | Level 7
Sorry, I am going to do that, thank you!
sulafa
ballardw
Super User

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.

 

sulafa
Obsidian | Level 7
Done, thank you!
sulafa
Reeza
Super User
  1. Forgot a comma on macro statement
  2. Missed the NDEC parameter
  3. Forgot a semicolon on your PROC MEANS statement

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!

 

 

 
 
 

 


 

muawia27
Calcite | Level 5
I just tested this one and it works it works .
Let us know if it works %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;
Reeza
Super User
Glad to hear it works. Please mark the question as solved.
sulafa
Obsidian | Level 7
Thank you so much!
sulafa
sulafa
Obsidian | Level 7
Thank you so much Reeza, I am sorry I missed your comment, that solves the problem !
sulafa

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 10 replies
  • 1070 views
  • 4 likes
  • 5 in conversation