DATA Step, Macro, Functions and more

NEW in SAS macro

Reply
New Contributor
Posts: 2

NEW in SAS macro

hi ! I have a problem like this since I am a new in SAS I couldn't find the way to start the problem. The problem is:

Note: I have given the data named 'hidalgo'

Part 1

Write a macro that accepts a table name, a column name, a list of integers, a main axis label and an x axis label. This function should scan over each element in the list of integers and produce a histogram for each integer value, setting the bin count to the element in the input list, and labeling main and x-axis with the specified parameters. You should label the y-axis to read Frequency, bins = and the number of bins.

Part 2

Test your macro with the hidalgo data set (see below), using bin numbers 12, 36, and 60. You should be able to call your macro with something like

%plot_histograms(hidalgo, y, 12 36 60, main="1872 Hidalgo issue", xlabel="Thickness (mm)");

to plot three different histograms of the hidalgo data set.

Hint: Assume 20 40 60 resolve to a single macro parameter and use %scan. Your macro definition can look something like

%macro plot_histograms(table_name, column_name, number_of_bins, main="Main", xlabel="X Label")
Community Manager
Posts: 2,757

Re: NEW in SAS macro

Your questions looks a little bit like homework.  Experts in this forum don't mind helping with specific questions, but it's good to show an effort towards a solution.

 

Hint for macro development: develop from the inside out.  First, get a version of your program working for one case without using macro logic.  When it produces the result you want, then begin adding the macro wrapper and logic to generalize it for different data.  In this case, the SAS macro language is used for generating different versions of your program for different scenarios.  Set OPTIONS MPRiNT and MLOGIC to help debug.

 

If you get stuck, post your work so far and the log/results.  Try to be as informed as possible in your questions.

 

Chris

New Contributor
Posts: 2

Re: NEW in SAS macro

Thank you!

Super User
Posts: 17,780

Re: NEW in SAS macro

[ Edited ]

If you're new to SAS and starting off with learning macros, that's not a good idea. Learn basics first and then move towards macros. In a large portion of usages, macros are not even required. For example, for this question, modifying your data structure and using a BY statement will achieve the same output. You may be past that decision however, so if you're willing to dedicate the time, I would recommend the free SAS course as well as the UCLA tutorial. It's a quick read/review and you'll learn how to build a macro and then can apply it to your situation.

 

 

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

 

support.sas.com/training/tutorials

 

A list of sample macros in documentation

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 

Your question doesn't make a heck of a lot of sense to me in terms of a histogram by the way, so I'm hoping you've modified the question.  If you take a value, integer = 12, for example you'll find a frequency which will be a single value. Ie value 12 occurs 24 times? Not sure how you're planning to turn that into a histogram. 

 

 


yugeench wrote:

hi ! I have a problem like this since I am a new in SAS I couldn't find the way to start the problem. The problem is:

Note: I have given the data named 'hidalgo'

Part 1

Write a macro that accepts a table name, a column name, a list of integers, a main axis label and an x axis label. This function should scan over each element in the list of integers and produce a histogram for each integer value, setting the bin count to the element in the input list, and labeling main and x-axis with the specified parameters. You should label the y-axis to read Frequency, bins = and the number of bins.

Part 2

Test your macro with the hidalgo data set (see below), using bin numbers 12, 36, and 60. You should be able to call your macro with something like

%plot_histograms(hidalgo, y, 12 36 60, main="1872 Hidalgo issue", xlabel="Thickness (mm)");

to plot three different histograms of the hidalgo data set.

Hint: Assume 20 40 60 resolve to a single macro parameter and use %scan. Your macro definition can look something like

%macro plot_histograms(table_name, column_name, number_of_bins, main="Main", xlabel="X Label")

 

Super User
Posts: 10,483

Re: NEW in SAS macro

In addition to @ChrisHemedinger's excellent advice, when you start working with the macro code save your source code before every test run. There are common mistakes regarding missing semicolon's, quotes, parantheses and such that when used with the macro language can confuse SAS as to what you are doing and it will look like it is doing nothing because you have stacked up errors that SAS hasn't reached a place in processing to report the errors.

 

Also learn the options MPRINT SYMBOLGEN and MLOGIC to help debugging macro code.

Ask a Question
Discussion stats
  • 4 replies
  • 252 views
  • 4 likes
  • 4 in conversation