Hi
I have created a macro function to accept a table name, a column name, a list of integers, a main axis label and an x axis label. With this function I have produced 3 plots. The problem is that I need to y-axis to read Frequency, bins =
and the number of bins.
How can I include different bins number for different histo plot? The logout put and histo plot are attached. Thanks
Something like:
%macro plot_histograms(table_name=,column_name=,number_of_bins=,main="Main",xlabel="X Label"); %do j = 1 %to 3; proc sgplot data=&table_name.; histogram &column_name. / nbins=%scan(&number_of_bins.,&j.); title &main.; label &column_name.=&xlabel.; yaxis label="Frequency, bins=%scan(&number_of_bins.,&j.)"; run; %end; %mend; option mprint; %plot_histograms(table_name=hidalgo,column_name=y,number_of_bins=12 36 60,main="1872 Hidalgo issue",xlabel="Thickness (mm)");
Note the use of named parameters, the code window (its the {i} above the post area to retain formatting) and finishing macro variables with a .
Always post any information in the body of your post using the code window (its the {i} above the post), I for one will not download any files of the net.
From what I can gather from your text you have a few options for passing though the inforamtion, or you could work it out in the macro. Either way you would need to calculate the bin information then include it in the label so maybe:
proc sql; select count(bins) into :b from have; quit; %g (yaxis="Frequency, bins = &B.");
I would recommend moving from Proc Gchart to Proc SGplot.
The SGPlot freq has many more types of graphs and combinations are much easier than with Gchart.
For instance, Proc SGPLOT has a HISTOGRAM statement and one of the parameters is NBINS to set the number of bins to use. Then the histogram algorithm will assign values to the number requested. When you have a macro variable holding the number of bins
yaxis label="Frequency, bins= &var";
Which might be your &J if I read your code correctly.
I am using macro function to create 3 histo plots at once with diffrent bins value. I am getting in ylab as like 1,2 and 3 from my code . Actually I need to show 12,36 and 60 bins number respectively for 3 histo . My code is here below. Can you please help me to obtain 12,36 and 60 instead of 1, 2 and 3 in y axis.
%macro plot_histograms(table_name, column_name, number_of_bins, main="Main", xlabel="X Label");
%do j = 1 %to 3;
proc sgplot data=&table_name;
histogram &column_name / nbins=%scan(&number_of_bins,&j) ;
* histogram &column_name / nbins=%scan(&number_of_bins,2) ;
* histogram &column_name / nbins=%scan(&number_of_bins,3) ;
title &main;
label &column_name=&xlabel;
yaxis label="Frequency, bins= ";
run;
%end;
%mend;
option mprint;
%plot_histograms(hidalgo, y, 12 36 60 , main="1872 Hidalgo issue", xlabel="Thickness (mm)");
Hi
Change your YAXIS statement like so:
yaxis label="Frequency, bins=%scan(&number_of_bins,&j)";
Something like:
%macro plot_histograms(table_name=,column_name=,number_of_bins=,main="Main",xlabel="X Label"); %do j = 1 %to 3; proc sgplot data=&table_name.; histogram &column_name. / nbins=%scan(&number_of_bins.,&j.); title &main.; label &column_name.=&xlabel.; yaxis label="Frequency, bins=%scan(&number_of_bins.,&j.)"; run; %end; %mend; option mprint; %plot_histograms(table_name=hidalgo,column_name=y,number_of_bins=12 36 60,main="1872 Hidalgo issue",xlabel="Thickness (mm)");
Note the use of named parameters, the code window (its the {i} above the post area to retain formatting) and finishing macro variables with a .
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.