BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mukeshmehata1
Fluorite | Level 6

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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 .

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.");
ballardw
Super User

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.

mukeshmehata1
Fluorite | Level 6
Thank you for your information. However, I am getting like 1,2 and 3. Actually I need to show 12,36 and 60 bins number . 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)");

mukeshmehata1
Fluorite | Level 6

 

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

BrunoMueller
SAS Super FREQ

Hi

 

Change your YAXIS statement like so:

 

yaxis label="Frequency, bins=%scan(&number_of_bins,&j)";
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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 .

mukeshmehata1
Fluorite | Level 6
thank you so much

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 1289 views
  • 1 like
  • 4 in conversation