Obsidian | Level 7

## Plot all variables of all groups in one graph

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
Tourmaline | Level 20

## Re: Plot all variables of all groups in one graph

What do you want the graph to look like?

Obsidian | Level 7

## Re: Plot all variables of all groups in one graph

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".
Tourmaline | Level 20

## Re: Plot all variables of all groups in one graph

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

Obsidian | Level 7

## Re: Plot all variables of all groups in one graph

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.

Tourmaline | Level 20

## Re: Plot all variables of all groups in one graph

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

## Re: Plot all variables of all groups in one graph

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

Tourmaline | Level 20

## Re: Plot all variables of all groups in one graph

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

Super User

## Re: Plot all variables of all groups in one graph

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.

Tourmaline | Level 20

## Re: Plot all variables of all groups in one graph

Good point @ballardw

Meteorite | Level 14

## Re: Plot all variables of all groups in one graph

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

Discussion stats
• 10 replies
• 885 views
• 2 likes
• 4 in conversation