BookmarkSubscribeRSS Feed
yugeench
Calcite | Level 5

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")
4 REPLIES 4
ChrisHemedinger
Community Manager

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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
yugeench
Calcite | Level 5

Thank you!

Reeza
Super User

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")

 

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 876 views
  • 4 likes
  • 4 in conversation