BookmarkSubscribeRSS Feed
justbuen
Calcite | Level 5

I am trying to recreate the figure below in Excel to a figure using SGPLOT. I am having a hard time including both the year and month labels on the x axis. I've created separate variables for both the month and year but cannot seem to get the plot to come out via SGPLOT. Any assistance is much appreciated!

 

Proc SGPlot data=sample;
Series x = month y = blueline;
Series x = month y = redline;
Series x = month y = greenline;
YAXIS label = "Percent";
XAXIS label = "Month and Year";
Run;

 

justbuen_0-1683160995451.png

 

8 REPLIES 8
ballardw
Super User

@justbuen wrote:

I am trying to recreate the figure below in Excel to a figure using SGPLOT. I am having a hard time including both the year and month labels on the x axis. I've created separate variables for both the month and year but cannot seem to get the plot to come out via SGPLOT. Any assistance is much appreciated!

 

Proc SGPlot data=sample;
Series x = month y = blueline;
Series x = month y = redline;
Series x = month y = greenline;
YAXIS label = "Percent";
XAXIS label = "Month and Year";
Run;

 

justbuen_0-1683160995451.png

 


The highlighted text in your question suggest that the likely most flexible solution is to use Month and year values to create a DATE value. Then apply the correct format to the new variable to appear as desired.

You do not say whether your "month" is numeric value or a character value. If the year and month are numeric values then you could use the MDY function in a data step to add the variable. If they are character, or a mix of character month and numeric year then other steps are needed.

 

Do you have a day of month variable (implied in your example graph but not clearly stated)? Is your Month character or Numeric?

 

 

justbuen
Calcite | Level 5

Hello and thanks for your response! My date variable is by MMWR week and I created separate numeric variables for month and year:

 

Proc Format;
Value weeks 22045-22051 = "May 10-16 2020"
22052-22058 = "May 17-23 2020"
22059-22065 = "May 24-30 2020"
22066-22072 = "May 31-Jun 6 2020"
22073-22079 = "Jun 7-13 2020"
22080-22086 = "Jun 14-20 2020"
22087-22093 = "Jun 21-27 2020"
22094-22100 = "Jun 28-Jul 4 2020"
22101-22107 = "Jul 5-11 2020"
22108-22114 = "Jul 12-18 2020"
22115-22121 = "Jul 19-25 2020"
22122-22128 = "Jul 26-Aug 1 2020"
22129-22135 = "Aug 2-8 2020"
22136-22142 = "Aug 9-15 2020"
22143-22149 = "Aug 16-22 2020"
22150-22156 = "Aug 23-29 2020"
22157-22163 = "Aug 30-Sep 5 2020"
22164-22170 = "Sep 6-12 2020"
22171-22177 = "Sep 13-19 2020"
22178-22184 = "Sep 20-26 2020"
22185-22191 = "Sep 27-Oct 3 2020"
22192-22198 = "Oct 4-10 2020"
22199-22205 = "Oct 11-17 2020"
etc...;

value monthfmt 1 = "January"
2 = "February"
3 = "March"
4 = "April"
5 = "May"
6 = "June"
7 = "July"
8 = "August"
9 = "September"
10 = "October"
11 = "November"
12 = "December";

run;

. . .

if start in (22045:22280) then year=2020;
else if start in (22281:22645) then year=2021;
else if start in (22646:23012) then year=2022;
else if start gt 23012 then year=2023;

 

if start in (22281:22311 , 22645:22675 , 23012:23042) then month=1;
else if start in (22312:22339 , 22676:22703 , 23043:23070) then month=2;
else if start in (22340:22370 , 22704:22734 , 23071:23101) then month=3;
else if start in (22371:22400 , 22735:22764 , 23102:23131) then month=4;
etc...;

 

LMK if this helps answer your question. The three lines are three separate calculate numeric variables.

Ksharp
Super User
data have;
 set sashelp.stocks;
 if year(date) in (2003:2005);
 keep stock date  close;
 format date monname.;
run;
proc sort data=have;by stock date;run;


%sganno
data sganno;
%SGTEXT( LABEL="2003",DRAWSPACE="LAYOUTPERCENT",X1=30,Y1=-2)
%SGTEXT( LABEL="2004",DRAWSPACE="LAYOUTPERCENT",X1=60,Y1=-2)
%SGTEXT( LABEL="2005",DRAWSPACE="LAYOUTPERCENT",X1=90,Y1=-2)
run;
title;footnote;
proc sgplot data = have sganno=sganno pad=(bottom=0.25in);
  series x=date y=close/group=stock;
  xaxis valuesformat=monname. type=linear values=('01jan2003'd to '01dec2005'd by month)
  fitpolicy=rotate VALUESROTATE=VERTICAL display=(nolabel);
  keylegend /location=inside across=1;
run;


Ksharp_0-1683180107081.png

 

Ksharp
Super User
data have;
 set sashelp.stocks;
 if year(date) in (2003:2005);
 keep stock date  close;
 format date monname.;
run;
proc sort data=have;by stock date;run;



footnote j=c '           2003                                     2004                                        2005';

proc sgplot data = have ;
  series x=date y=close/group=stock;
  xaxis valuesformat=monname. type=linear values=('01jan2003'd to '01dec2005'd by month)
  fitpolicy=rotate VALUESROTATE=VERTICAL display=(nolabel);
  keylegend /location=inside across=1;
run;


Ksharp_0-1683180281548.png

 

justbuen
Calcite | Level 5

Thanks for this! Is there any way to edit the code where the three lines are three separate variables? The current graph has the lines to be three distinct categories of the "stock" variable.

ballardw
Super User

@justbuen wrote:

Thanks for this! Is there any way to edit the code where the three lines are three separate variables? The current graph has the lines to be three distinct categories of the "stock" variable.


That would be a single plot statement like the example but one for each Y variable. No Group= variable. Series options like LINEATTRS to change line color or pattern would be used to differentiate them visibly.

Ksharp
Super User
/*
As @ ballardw said transpose your data into my data structure.
or just plot three lines.
*/
data have;
 set sashelp.stocks;
 if year(date) in (2003:2005);
 keep stock date  close;
 format date monname.;
run;
proc sort data=have;by  date stock;run;
proc transpose data=have out=have2 ;
by date;
id stock;
var close;
run;


footnote j=c '           2003                                     2004                                        2005';

proc sgplot data = have2 ;
  series x=date y=IBM/lineattrs=(color=red);
  series x=date y=Intel/lineattrs=(color=blue);
  series x=date y=Microsoft/lineattrs=(color=green);

  refline '01jan2004'd '01jan2005'd/axis=x;
  xaxis valuesformat=monname. type=linear values=('01jan2003'd to '01dec2005'd by month)
  fitpolicy=rotate VALUESROTATE=VERTICAL display=(nolabel);
  keylegend /location=inside across=1;
run;

Ksharp_0-1683254082992.png

 

BrunoMueller
SAS Super FREQ

Building on what others already suggest, adding a BLOCK statement to visually highlight the year, one does need a new variable (yearblock). 

 


data have;
  set sashelp.stocks;
  where date between "01apr2003"d and "01aug2005"d;
  yearblock = year(date);
  date = mdy(month(date), 1, year(date));
  keep stock yearblock date  close;
  format date monname.;
run;

proc sort data=have;
  by  yearblock date;
run;

proc transpose data=have out=have2;
  by yearblock date;
  id stock;
  var close;
run;
title;
footnote;
proc sgplot data = have2;
  series x=date y=IBM / lineattrs=(color=red);
  series x=date y=Intel / lineattrs=(color=blue);
  series x=date y=Microsoft / lineattrs=(color=green) markers;
  block x=date block=yearblock / 
    nolabel position=bottom filltype=alternate
  ;
  refline '01jan2004'd '01jan2005'd / axis=x;
  
  xaxis type=linear
    values=('01apr2003'd to '01aug2005'd by month)
    fitpolicy=rotatealways valuesrotate=vertical display=(nolabel)
  ;
  yaxis display=(nolabel);
  keylegend / location=inside position=right across=1;
run;

Bruno_SAS_0-1683552499164.png

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1928 views
  • 6 likes
  • 4 in conversation