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

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; 

image.png

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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;

Capture.PNG

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

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;

Capture.PNG

novinosrin
Tourmaline | Level 20

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!

aknez
Calcite | Level 5

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

image.png

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1007 views
  • 3 likes
  • 3 in conversation