Hi. I have a time series (monthly) data of 2 variables (cash, asset) for different groups (categorized by country - suburb - firm).
Now I would like to plot a line graph of these 2 variables for each group, and these lines need to be put in one graph only.
Could you please advise me on how I should do it? Thank you very much.
My dataset is like this:
data graphdata;
input country $ suburb $ firm $ month $ cash asset;
datalines;
A A1 A2 Jan 25 32
A A1 A2 Feb 20 35
A A1 A2 Mar 11 12
A A1 A2 Apr 14 15
A A1 A3 Jan 1 2
A A1 A3 Feb 3 4
A A1 A3 Mar 5 6
A A1 A3 Apr 9 10
B B1 B2 Jan 11 12
B B1 B2 Feb 13 14
B B1 B2 Mar 15 16
B B1 B2 Apr 17 18
;
run;
What do you want the graph to look like?
Do you want all three lines for Cash to be the same color? Or should each of the six lines have a different color?
Yeah, all 3 lines for cash should be the same color as I do not need to differentiate across groups. I just need to differentiate "cash" and "asset".
Also, the full dataset has thousands of groups, so it is no use to differentiate between groups.
This should get you started
data graphdata;
input country $ suburb $ firm $ month $ cash asset;
datalines;
A A1 A2 Jan 25 32
A A1 A2 Feb 20 35
A A1 A2 Mar 11 12
A A1 A2 Apr 14 15
A A1 A3 Jan 1 2
A A1 A3 Feb 3 4
A A1 A3 Mar 5 6
A A1 A3 Apr 9 10
B B1 B2 Jan 11 12
B B1 B2 Feb 13 14
B B1 B2 Mar 15 16
B B1 B2 Apr 17 18
;
data graphview / view=graphview;
set graphdata;
csf = catx(" ", country, suburb, firm);
run;
proc sgplot data=graphview noautolegend;
series x=month y=cash / group=csf lineattrs=(color='red');
series x=month y=asset / group=csf lineattrs=(color='blue');
run;
Here's the output from draycut's code, for those who aren't in a situation where you can run the code easily:
Thanks @GraphGuy! And thank you for all of your posts at Graphically Speaking. I learn so much from you 🙂
First thing, prepare to be disappointed as your "month" is not going to be sorted properly. Your first x value would be Apr , then Feb, Jan, Mar because that is the sort order for character values.
If you want date-like values to sort properly then use an actual SAS date value and then display with an appropriate format.
Good point @ballardw
@ballardw - I agree in general, that the best/proper way to place the month names along the xasis it to use numeric or real-date values (to control the order), and then a format to make them 'appear' as the text month names ... but in this particular situation, the simple text month names seem to appear in the proper order (I assume they're appearing in data-order). So, as long as the data is arranged carefully (as in the sample data @trungcva112 posted), it might be ok to just use simple text. 🙂
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.