I have a data set of events that occur over time. Each observation is an event with a date and I create two variables from the date: month and year. I use VLINE to plot the frequency of events that occur each month over several years.
I love how easy this is to implement in SGPLOT with VLINE but I would like to add one thing: in Dec 2018 there were no events (see blue line). Is there a way to have VLINE plot a zero for this month?
proc sgplot data=classes;
vline month / group=year markers;
run;
At the urging of @novinosrin I am adding notes.
My first thought and was to use the FREQ= option but I found that values with 0 are discarded. So I ended with the trick of plotting the sum.
The first step is something from nothing COMPLETETYPES. This creates obs with zero freq for AGE=16 sex=F.
Plotting the SUM of _FREQ_ is straight forward but you don't get a nice Y axis label but that can be fixed.
yaxis label='Frequency' values=(0 to 3);
ODS TRACE and ODS OUTPUT are not really needed but I want to see the data that SPLOT used.
proc summary data=sashelp.class nway missing completetypes;
class age sex;
output out=_4plot;
run;
proc print;
run;
ods trace on;
ods output SGPlot=SGPlot;
proc sgplot data=_4plot;
vline age / group=sex response=_freq_ stat=sum markers;
run;
ods trace off;
proc print;
run;
At the urging of @novinosrin I am adding notes.
My first thought and was to use the FREQ= option but I found that values with 0 are discarded. So I ended with the trick of plotting the sum.
The first step is something from nothing COMPLETETYPES. This creates obs with zero freq for AGE=16 sex=F.
Plotting the SUM of _FREQ_ is straight forward but you don't get a nice Y axis label but that can be fixed.
yaxis label='Frequency' values=(0 to 3);
ODS TRACE and ODS OUTPUT are not really needed but I want to see the data that SPLOT used.
proc summary data=sashelp.class nway missing completetypes;
class age sex;
output out=_4plot;
run;
proc print;
run;
ods trace on;
ods output SGPlot=SGPlot;
proc sgplot data=_4plot;
vline age / group=sex response=_freq_ stat=sum markers;
run;
ods trace off;
proc print;
run;
Guru @data_null__ Needless to say as usual yours has to be great. May i request you to add a couple of comments that addressed the solution appropriately so that people can diligently take notes? Only when you have a moment to spare. I understand!
@data_null__ has provided an excellent solution. I am annotating my original code to show how I modified it to add the zero frequencies and showing the resulting plot. The key is the completetypes option in proc summary, which adds zero frequencies automatically.
proc summary data=events nway completetypes;
class month year;
output out=_4plot;
run;
proc sgplot data=_4plot;
vline month / group=year markers response=_freq_;
yaxis label="Frequency";
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.