BookmarkSubscribeRSS Feed
trungcva112
Obsidian | Level 7

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;
10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

What do you want the graph to look like?

trungcva112
Obsidian | Level 7
Hi. The graph should have 6 lines (3 lines for the variable "cash", 3 lines for "asset"), with "month" as x-axis. I just need the lines for variable "cash" to be in different color from variable "asset".
PeterClemmensen
Tourmaline | Level 20

Do you want all three lines for Cash to be the same color? Or should each of the six lines have a different color?

trungcva112
Obsidian | Level 7

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.

PeterClemmensen
Tourmaline | Level 20

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;
GraphGuy
Meteorite | Level 14

Here's the output from draycut's code, for those who aren't in a situation where you can run the code easily:

 

cash.png

PeterClemmensen
Tourmaline | Level 20

Thanks @GraphGuy! And thank you for all of your posts at Graphically Speaking. I learn so much from you 🙂

ballardw
Super User

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.

GraphGuy
Meteorite | Level 14

@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. 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 10 replies
  • 1051 views
  • 2 likes
  • 4 in conversation