BookmarkSubscribeRSS Feed
Moony
Calcite | Level 5

Create a macro that implement this functionality: by specifying a dataset name and a variable name in the dataset, draw a histogram of the variable if the variable is a numerical variable, and draw a bar chart of the variable if the variable is a categorical variable Then use the macro to draw a histogram of age at death as well as a bar chart of cause of death for the SASHELP.Heart dataset. You may want to use the VARTYPE function.

8 REPLIES 8
Moony
Calcite | Level 5

It's really homework. But I'm sorry I don't have a clue. I'm not familiar with macro at all.

Moony
Calcite | Level 5

Ummm...This is not a SAS course. The use of SAS was only introduced in class. But the homework involves this problem, I can't solve it by myself

Kurt_Bremser
Super User

Then we need to absolutely start with basics: SAS programs work in steps, and you can use your SAS interface (either Enterprise Guide or SAS Studio) to assist you in creating such steps.

Further, SAS provides a preprocessor to create code dynamically, the macro preprocessor.

A macro looks like this:

%macro mymac(dataset,variable);
/* code to be created, and macro statements helping in this */
%mend;

You already see the basic structure of what you need to achieve in the first line.

Now, let's see the code we need to implement in there, so we use SAS Studio to create code for the histogram. In SAS Studio (in University Edition), I opened Tasks and Utilties, opened Diagram, and selected Histogram. Then I selected SASHELP.HEART as data source, and AgeAtDeath as analysis variable. SAS Studio then created this code for me:

ods graphics / reset width=6.4in height=4.8in imagemap;

proc sgplot data=SASHELP.HEART;
	histogram AgeAtDeath /;
	yaxis grid;
run;

ods graphics / reset;

If we wrap this into the macro definition from above, we need to replace the relevant parts with our macro parameters:

%macro mymac(dataset,variable);

ods graphics / reset width=6.4in height=4.8in imagemap;

proc sgplot data=&dataset.;
	histogram &variable. /;
	yaxis grid;
run;

ods graphics / reset;

%mend;

That is the macro for a numeric variable; you can create the code for a bar chart in the same way.

What is left is how to determine the type of the variable in question, and make our two code parts conditional.

We can use the vartype function in macro code, but this is actually quite tricky:

%let dsid = %sysfunc(open(sashelp.heart,i));
%let num = %sysfunc(varnum(&dsid,ageatdeath));
%let type = %sysfunc(vartype(&dsid,&num));

Let us incorporate this into our macro:

%macro mymac(dataset,variable);

%local dsid num type;
%let dsid = %sysfunc(open(&dataset.,i));
%let num = %sysfunc(varnum(&dsid.,&variable.));
%let type = %sysfunc(vartype(&dsid.,&num.));

%if &type. = N %then %do;

ods graphics / reset width=6.4in height=4.8in imagemap;

proc sgplot data=&dataset.;
	histogram &variable. /;
	yaxis grid;
run;

ods graphics / reset;

%end;
%else %do;
/* insert the bar chart code here, and insert the macro variables where appropriate */
%end;

%mend;

You then call the macro

%mymac(sashelp.heart,ageatdeath)

If you study your resulting macro code, you will see that most of the two codes is similar, and you need only make a single line conditional. I leave this as an exercise.

Moony
Calcite | Level 5

Wow, thank you very much! This has helped me a lot.Smiley Very Happy

Kurt_Bremser
Super User

I'm still surprised you were given a task involving the VARTYPE function; such functions were not covered in the first two weeks of SAS Programming training in Heidelberg ...

Moony
Calcite | Level 5

Thank you again for answering my questions!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 8 replies
  • 955 views
  • 3 likes
  • 2 in conversation