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;
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.
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.
Just tried using a format statement but for some reason it will only display the year now. 😞
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.
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!
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.
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;
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?
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.
ah, I used the values and values display option and that seemed to solve the problem.
Thanks everyone!
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.