Data visualization with SAS programming

macro call function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

macro call function

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

 


Accepted Solutions
Solution
‎08-04-2017 04:08 AM
Super User
Super User
Posts: 7,988

Re: macro call function

Posted in reply to mukeshmehata1

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


All Replies
Super User
Super User
Posts: 7,988

Re: macro call function

Posted in reply to mukeshmehata1

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.");
Super User
Posts: 11,343

Re: macro call function

Posted in reply to mukeshmehata1

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.

Occasional Contributor
Posts: 11

Re: macro call function

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

Occasional Contributor
Posts: 11

healp for custmizing ylab in sgplot

Posted in reply to mukeshmehata1

 

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

SAS Super FREQ
Posts: 709

Re: healp for custmizing ylab in sgplot

Posted in reply to mukeshmehata1

Hi

 

Change your YAXIS statement like so:

 

yaxis label="Frequency, bins=%scan(&number_of_bins,&j)";
Solution
‎08-04-2017 04:08 AM
Super User
Super User
Posts: 7,988

Re: macro call function

Posted in reply to mukeshmehata1

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 .

Occasional Contributor
Posts: 11

Re: macro call function

thank you so much
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 286 views
  • 1 like
  • 4 in conversation