Hello,
I am looking for a solution to display x-axis values for missing data. Here is my current code and display. You can see that Month/Years are missing because there are not any cases during that month in the data set. I would still like to show month/year in the x-axis but there would be no bar and display 0 for that month/Year.
proc sgplot data=state2;
title 'Cases: 2017-2019';
format Stage $syp. eventdate monyy7.;
vbar eventdate / response=count group=Stage stat=sum datalabel;
xaxis display=(nolabel);
yaxis grid label='Cases' max=55;
run;
Missing x-axis Values: Sep2017, Jan2018, Apr2018, Jul2018, Jan2019
There might be a better more-elegant way to do this programmatically, but here's the first "brute force" solution that comes to mind. You could hard-code all the desired months along the xaxis.
Here's an example, using sashelp sample data. First I remove the February data (so my data is similar to yours, with no data points during a certain month).
data my_data; set sashelp.stocks (where=(year(date) in (2005) and put(date,monname3.)^='Feb'));
run;
And then I generate the plot, hard-coding all the months I desire in the xaxis:
title "Cases for 2005";
proc sgplot data=my_data;
format date monyy7.;
format close comma8.0;
vbar date / response=close stat=sum group=stock datalabel;
xaxis display=(nolabel)
values=('Jan2005' 'Feb2005' 'Mar2005' 'Apr2005' 'May2005' 'Jun2005'
'Jul2005' 'Aug2005' 'Sep2005' 'Oct2005' 'Nov2005' 'Dec2005');
yaxis grid label='Cases' max=200;
run;
Have you tried setting the X axis to a date type? What happens then?
Edit: seems to work for me - your date variable needs to be numeric whit a date format for this to work correctly.
proc sgplot data=sashelp.stocks;
where year(date) ne 1991;
vbar date / response = open stat=sum group=stock;
xaxis type=time;
run;
@mary_mcneill wrote:
Hello,
I am looking for a solution to display x-axis values for missing data. Here is my current code and display. You can see that Month/Years are missing because there are not any cases during that month in the data set. I would still like to show month/year in the x-axis but there would be no bar and display 0 for that month/Year.
proc sgplot data=state2;
title 'Cases: 2017-2019';
format Stage $syp. eventdate monyy7.;
vbar eventdate / response=count group=Stage stat=sum datalabel;
xaxis display=(nolabel);
yaxis grid label='Cases' max=55;run;
Missing x-axis Values: Sep2017, Jan2018, Apr2018, Jul2018, Jan2019
I tried that but I receive the following error: WARNING: BARCHARTPARM statement has a conflict with the axis type. The plot will not be drawn
The variable eventdate is numeric and has the date format MONYY7.
@mary_mcneill wrote:
I tried that but I receive the following error: WARNING: BARCHARTPARM statement has a conflict with the axis type. The plot will not be drawn
The variable eventdate is numeric and has the date format MONYY7.
You should post the code that generated that. Does the code I show run correctly and appear with a gap? It worked on my version of SAS as expected.
Running the below code with the xaxis type=time did not produce any graphs. I verified in SAS proc contents that event date was in fact numeric with a date format and it was. Perhaps I was missing something?
proc sgplot data=state2;
title 'Cases: 2017-2019';
format Stage $syp. eventdate monyy7.;
vbar eventdate / response=count group=Stage stat=sum datalabel;
xaxis type=time;
yaxis grid label='Cases' max=55;
run;
There might be a better more-elegant way to do this programmatically, but here's the first "brute force" solution that comes to mind. You could hard-code all the desired months along the xaxis.
Here's an example, using sashelp sample data. First I remove the February data (so my data is similar to yours, with no data points during a certain month).
data my_data; set sashelp.stocks (where=(year(date) in (2005) and put(date,monname3.)^='Feb'));
run;
And then I generate the plot, hard-coding all the months I desire in the xaxis:
title "Cases for 2005";
proc sgplot data=my_data;
format date monyy7.;
format close comma8.0;
vbar date / response=close stat=sum group=stock datalabel;
xaxis display=(nolabel)
values=('Jan2005' 'Feb2005' 'Mar2005' 'Apr2005' 'May2005' 'Jun2005'
'Jul2005' 'Aug2005' 'Sep2005' 'Oct2005' 'Nov2005' 'Dec2005');
yaxis grid label='Cases' max=200;
run;
Thank you so much! I had seen that option but since this report is done monthly, I was trying to see if there was another way to have them display without having to update the months/years every time the SAS program is ran, but this fixes my problem so I am going to use it!
In the long-run, you might want to use Proc SGpanel to get the effect of a monthly bar chart "grouped" by year.
Here's an example:
http://robslink.com/SAS/ods4/it_job_openings_in_nc.htm
http://robslink.com/SAS/ods4/it_job_openings_in_nc_info.htm
@mary_mcneill wrote:
Hello,
I am looking for a solution to display x-axis values for missing data. Here is my current code and display. You can see that Month/Years are missing because there are not any cases during that month in the data set. I would still like to show month/year in the x-axis but there would be no bar and display 0 for that month/Year.
proc sgplot data=state2;
title 'Cases: 2017-2019';
format Stage $syp. eventdate monyy7.;
vbar eventdate / response=count group=Stage stat=sum datalabel;
xaxis display=(nolabel);
yaxis grid label='Cases' max=55;run;
Missing x-axis Values: Sep2017, Jan2018, Apr2018, Jul2018, Jan2019
When your axis values are SAS date values you can use date intervals for the tick marks in the VALUES= of XAXIS to accomplish what I think you want:
proc sgplot data=sashelp.stocks; where year(date) in (2002 2003) and month(date) ne 6; vbar date / response = open stat=sum group=stock; xaxis values = ('01JAN2002'd to '01DEC2003'd by month); format date monyy7.; run;
Note that I just picked a couple of months to exclude from the data to demonstrate missing data. The specific example data set has summaries on the first of the month and if your dates are not such there may be some different treatment of the ticks as midpoints but the basic principal is the same. And shorter code than listing specific values.
Thanks! This works perfectly as well. I was also able to use a macro I already had in my program to reference the start and end dates 🙂
%let sdate=01SEP17;
%let edate=28FEB19;
proc sgplot data=state2;
title 'Cases: 2017-2019';
format Stage $syp. eventdate monyy7.;
vbar eventdate / response=count group=Stage stat=sum datalabel;
xaxis display=(nolabel)
values=("&sdate"d to "&edate"d by month);
yaxis grid label='Cases' max=55;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.