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

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
Graph.png

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

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;

 

SGPlot16.png

View solution in original post

9 REPLIES 9
Reeza
Super User

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
Graph.png


 

mary_mcneill
Obsidian | Level 7

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.

 
Reeza
Super User

@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.

 

mary_mcneill
Obsidian | Level 7

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;

GraphGuy
Meteorite | Level 14

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;

 

SGPlot16.png

mary_mcneill
Obsidian | Level 7

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!

GraphGuy
Meteorite | Level 14

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

 

 

ballardw
Super User

@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
Graph.png


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.

mary_mcneill
Obsidian | Level 7

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;

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 6465 views
  • 3 likes
  • 4 in conversation