Data visualization with SAS programming

Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

Hi there,

 

I'm trying to create a line graph with multiple lines for each product by year and quarter.  To display the year and quarter together, I simply concatenated the two variables and created a new year_qtr variable.  However, as you can see in the attached graph, it looks awfully crowded.  Is there a way to simply show the year once for the four quarters, as opposed to for each quarter?  And I'd still like to show the quarterly data points, so simply using the year variable doesn't help with that.  Any help would be greatly appreciated!

 

Code as generated by SAS below, but truncated.

 

%_eg_conditional_dropds(WORK.SORTTempTableSorted);
SYMBOL1
INTERPOL=JOIN
HEIGHT=10pt
VALUE=NONE
LINE=1
WIDTH=2

CV = _STYLE_
;
SYMBOL2
INTERPOL=JOIN
HEIGHT=10pt
VALUE=NONE
LINE=1
WIDTH=2

CV = _STYLE_
;
SYMBOL3
INTERPOL=JOIN
HEIGHT=10pt
VALUE=NONE
LINE=1
WIDTH=2

CV = _STYLE_
;
Legend1
FRAME
;
Axis1
STYLE=1
WIDTH=1
MINOR=NONE
label=('Number of Prescriptions');


Axis2
STYLE=1
WIDTH=1
MINOR=NONE
label=('Year, Quarter');


TITLE;
TITLE1 "Number of Prescriptions by Year/Quarter";
FOOTNOTE;
FOOTNOTE1 "Source: XXX Data, 2005-2015";

 

PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT statecd, hcv_rc ,year_qtr,
SUM(num_presc) AS num_presc_SUM
FROM HCV.HCVX

GROUP BY statecd,hcv_rc,year_qtr
ORDER BY statecd,hcv_rc,year_qtr
;
QUIT;
Legend1 label=(color=black height=1 'HCV Drugs') cborder=black;
PROC GPLOT DATA = WORK.SORTTempTableSorted; 
PLOT num_presc_SUM * year_qtr =hcv_rc
/
VAXIS=AXIS1

HAXIS=AXIS2

FRAME LEGEND=LEGEND1
;BY statecd;

RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
GOPTIONS RESET = SYMBOL;

 


SAS Graph.PNG

Accepted Solutions
Solution
‎05-23-2016 04:22 PM
Super User
Posts: 10,500

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

One way would be to create a custom format that only dislayed the year for the first quarter:

 

proc format;

value $Myyrqtr

'2011   Q01' = '2011 Q01'

'2011   Q02' = 'Q02'

'2011   Q03' = 'Q03'

'2011   Q04' = 'Q04'

etc. 

and add

Format year_qtr $Myyrqtr.;

to the gplot code.

 

Everything else I can think of involves creating actual date variables. Then you could use a format to display the quarter and either ANNOTATE to get the year value (GPLOT)

 

Or move over to using GTL, something similar to this: http://support.sas.com/kb/35/169.html again using actual dates, ignore the band, vector and scatter bits and use one Series statement with a GROUP variable.

View solution in original post


All Replies
Solution
‎05-23-2016 04:22 PM
Super User
Posts: 10,500

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

One way would be to create a custom format that only dislayed the year for the first quarter:

 

proc format;

value $Myyrqtr

'2011   Q01' = '2011 Q01'

'2011   Q02' = 'Q02'

'2011   Q03' = 'Q03'

'2011   Q04' = 'Q04'

etc. 

and add

Format year_qtr $Myyrqtr.;

to the gplot code.

 

Everything else I can think of involves creating actual date variables. Then you could use a format to display the quarter and either ANNOTATE to get the year value (GPLOT)

 

Or move over to using GTL, something similar to this: http://support.sas.com/kb/35/169.html again using actual dates, ignore the band, vector and scatter bits and use one Series statement with a GROUP variable.

Contributor
Posts: 65

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

Just tried using a format statement but for some reason it will only display the year now. :-(

Super User
Posts: 10,500

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

which format did you use?

The example I created may not work with your data as I am not sure how many spaces (or other not visible) characters may have appeared in your actual data.

 

Contributor
Posts: 65

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

 

Nevermind!  It worked! You were right, there was an extra space in between the year and quarter I didn't account for.  Thank you so much!

SAS Super FREQ
Posts: 1,106

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

This is relatively easy with SGPLOT.  Using YYQ4. format on X axis with date values, I get this graph.  The split is automatic.

Not sure I can get 'Q1' instead of '1', etc.  I will check.

 

YrQtr.png

Super User
Posts: 10,500

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

Maybe a custom format:

proc format library=work;
picture myqtr (default=7)   
low-high= '%Y Q%q' (datatype=date)
;
run; 

data junk;
   do date='05Jan2015'd, '10APR2015'd,'25JUL2015'd,'17OCT2015'd  ;
   output;
   end;
   format date myqtr.;
run;
Contributor
Posts: 65

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

I tried this but it wouldn't just show the year for the first quarter.  It shows the two digit year and then quarter (11Q1 11Q2, etc.).  Is there something else I need to specify in the proc sgplot for this to work?

Contributor
Posts: 65

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

Does this code not work if you move to proc sgplot?  I changed the code to proc sgplot and now this format doesn't work anymore.

Contributor
Posts: 65

Re: Edit the labels in the x axis of a graph when displaying two variables (Year and Quarter)

ah, I used the values and values display option and that seemed to solve the problem.

 

Thanks everyone!

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 438 views
  • 0 likes
  • 3 in conversation