BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
LisaZ1
Obsidian | Level 7

hi, I have dataset and I'm trying to get a plot from it. The dataset is something like below(not complete). Basically, the dataset I have contains 5 years of data and for each year, there are 12 months, and for each month, there are 3 types and each type has a spending. So one year has 36 rows. What I want for the plot is that the x-axis is the month of each year, the y-axis is the spending, but since there are three types, I also want separate lines for each type.  

I'm trying to use proc sgplot, but I don't know how to make all five years spending show in one plot. I insert the plot I got from the code below. I know it is not correct since I intentionally make each type in both year gradually increase as month increases. In the final result, I only want the each year to show up instead of month like the photo I inserted.

Any suggestions would be very helpful. Thank you!

 

data have;
	input year month type spending;
	datalines;
	2015 1 1 22
	2015 1 2 33
	2015 1 3 34
	2015 2 1 23
	2015 2 2 34
	2015 2 3 35
	2015 3 1 24
	2015 3 2 35
	2015 3 3 36
	2015 4 1 25
	2015 4 2 36
	2015 4 3 37
	2016 1 1 18
	2016 1 2 30
	2016 1 3 40
	2016 2 1 19
	2016 2 2 31
	2016 2 3 41
	2016 3 1 20
	2016 3 2 32
	2016 3 3 42
	2016 4 1 21
	2016 4 2 33
	2016 4 3 43
	;
run;

proc sgplot data=have;
	series x=month y=spending / group=type;
run;

 Screen Shot 2022-08-08 at 1.28.31 AM.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Create an actual date value. The MDY function with a numeric month and year just requires a day of month and you can do that easily by placing a 1 in the Day position for the function. Then use a date type format to label the tick marks. Note that 5 years of monthly values means you likely have more values than makes for an attractive set of labels. You may end up wanting to sort your data by Type and the Date variable as some orders of data may get ugly. There are many formats to choose for displaying the date plus you can create a custom one with proc format if desirable.

data have;
	input year month type spending;
   date = mdy(month,1,year);
   format date date9.;
	datalines;
	2015 1 1 22
	2015 1 2 33
	2015 1 3 34
	2015 2 1 23
	2015 2 2 34
	2015 2 3 35
	2015 3 1 24
	2015 3 2 35
	2015 3 3 36
	2015 4 1 25
	2015 4 2 36
	2015 4 3 37
	2016 1 1 18
	2016 1 2 30
	2016 1 3 40
	2016 2 1 19
	2016 2 2 31
	2016 2 3 41
	2016 3 1 20
	2016 3 2 32
	2016 3 3 42
	2016 4 1 21
	2016 4 2 33
	2016 4 3 43
	;
run;

proc sgplot data=have;
   series x=date y=spending /group=type;
   format date yymon7.;
run;

You will find that in SAS it much better to go ahead and create an actual date, time or datetime value than to try to use bits or incomplete values like 201201 (for January of 2012). Reasons: There are functions that work with dates that you cannot use, or at least not get valid results with a number like 201201, Formats which besides making nice text create groups that are honored by report, analysis and most graphs.

Another thing related to graphs is you can use Values=( ) lists that have a start and end value and use "By Month" or "By Quarter" or other calendar interval to specify the desired tickmarks and labels.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

View solution in original post

2 REPLIES 2
ballardw
Super User

Create an actual date value. The MDY function with a numeric month and year just requires a day of month and you can do that easily by placing a 1 in the Day position for the function. Then use a date type format to label the tick marks. Note that 5 years of monthly values means you likely have more values than makes for an attractive set of labels. You may end up wanting to sort your data by Type and the Date variable as some orders of data may get ugly. There are many formats to choose for displaying the date plus you can create a custom one with proc format if desirable.

data have;
	input year month type spending;
   date = mdy(month,1,year);
   format date date9.;
	datalines;
	2015 1 1 22
	2015 1 2 33
	2015 1 3 34
	2015 2 1 23
	2015 2 2 34
	2015 2 3 35
	2015 3 1 24
	2015 3 2 35
	2015 3 3 36
	2015 4 1 25
	2015 4 2 36
	2015 4 3 37
	2016 1 1 18
	2016 1 2 30
	2016 1 3 40
	2016 2 1 19
	2016 2 2 31
	2016 2 3 41
	2016 3 1 20
	2016 3 2 32
	2016 3 3 42
	2016 4 1 21
	2016 4 2 33
	2016 4 3 43
	;
run;

proc sgplot data=have;
   series x=date y=spending /group=type;
   format date yymon7.;
run;

You will find that in SAS it much better to go ahead and create an actual date, time or datetime value than to try to use bits or incomplete values like 201201 (for January of 2012). Reasons: There are functions that work with dates that you cannot use, or at least not get valid results with a number like 201201, Formats which besides making nice text create groups that are honored by report, analysis and most graphs.

Another thing related to graphs is you can use Values=( ) lists that have a start and end value and use "By Month" or "By Quarter" or other calendar interval to specify the desired tickmarks and labels.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

LisaZ1
Obsidian | Level 7
thank you so much! I added the date variable and it worked perfectly!

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 583 views
  • 1 like
  • 2 in conversation