Data visualization with SAS programming

How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

Hello Experts,

how do we create a plot using the set of data like below:

Categoryelementvaluedate
cat1 a20Jan-10
cat1 a30Feb-10
cat1 a40Mar-10
cat1 a25Apr-10
cat1 b10Jan-10
cat1 b15Feb-10
cat1 b20Mar-10
cat1 b25Apr-10
cat2c30Jan-10
cat2c35Feb-10
cat2c25Mar-10
cat2c50Apr-10
cat2d45Jan-10
cat2d50Feb-10
cat2d60Mar-10
cat2d55Apr-10

From above data we can see that , category "cat1" have two elements "a and b" , category "cat2" have two elements "c and d" and for all of them we have "values" and "date".

Que is how do we create plot which looks like this:

Sabg.PNG

Motive is to see how category looks at an aggregated level along with how its elements are contributing. So above plot will be for "CAT1 and all its elements (here A and B)" and we will have same for "CAT2".

Thanks,

Harshad M.


Accepted Solutions
Solution
‎03-04-2015 06:38 AM
Trusted Advisor
Posts: 1,328

Re: How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

Something along these lines should work.

data t (index=(CD=(CAT DATE )));

input CAT $4.  EL : $1.  VAL : 2.  DATE : monyy6.;

format DATE date.;

cards;

cat1     a    20    Jan-10

cat1     a    30    Feb-10

cat1     a    40    Mar-10

cat1     a    25    Apr-10

cat1     b    10    Jan-10

cat1     b    15    Feb-10

cat1     b    20    Mar-10

cat1     b    25    Apr-10

cat2    c    30    Jan-10

cat2    c    35    Feb-10

cat2    c    25    Mar-10

cat2    c    50    Apr-10

cat2    d    45    Jan-10

cat2    d    50    Feb-10

cat2    d    60    Mar-10

cat2    d    55    Apr-10

run;

data T2 (drop=SUM);

  set T;

  by CAT DATE;

  SUM+VAL;

  if LAST.DATE then do;

    VALCAT=SUM;

    SUM=.;

  end;

run;

proc sort; by DATE;

proc gplot;

  plot  VAL*DATE=EL;

  plot2 VALCAT*DATE=CAT;

  run;

quit;

Sorry I can't test the code.

Use symbol statements to color the lines.

View solution in original post


All Replies
SAS Super FREQ
Posts: 1,044

Re: How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

It always helps to include the SAS release number.  It seems you want s SERIES plot of VALUE by DATE grouped by ELEMENT.  However, you want the series colored by CATEGORY.

this is very easy with SAS 9.4M2, where in the SERIES plot you can set GROUPLC=CATEGORY.  Now, each series is colored by CATEGORY.

With SAS 9.3, you can use the AttributeMap feature to get the same result.  Define an attribute map data set and set the color for each series group value explicitly.

Contributor
Posts: 37

Re: How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

Is there any way we can do it using Proc gplot in version 9.3.

Because to plot category and its constituents in same graph we need source dataset to be aggregated at category level as well as the element level so as to be able to plot them on same graph may be

using plot1 plot2 statements. Also there should be only two (CAT1 and CAT2 ) output files.

Apart from color, how do we plot data in above manner using basic gplot.

Thanks,

Harshad M.

Message was edited by: Harshad Madhmshettiwar

Solution
‎03-04-2015 06:38 AM
Trusted Advisor
Posts: 1,328

Re: How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

Something along these lines should work.

data t (index=(CD=(CAT DATE )));

input CAT $4.  EL : $1.  VAL : 2.  DATE : monyy6.;

format DATE date.;

cards;

cat1     a    20    Jan-10

cat1     a    30    Feb-10

cat1     a    40    Mar-10

cat1     a    25    Apr-10

cat1     b    10    Jan-10

cat1     b    15    Feb-10

cat1     b    20    Mar-10

cat1     b    25    Apr-10

cat2    c    30    Jan-10

cat2    c    35    Feb-10

cat2    c    25    Mar-10

cat2    c    50    Apr-10

cat2    d    45    Jan-10

cat2    d    50    Feb-10

cat2    d    60    Mar-10

cat2    d    55    Apr-10

run;

data T2 (drop=SUM);

  set T;

  by CAT DATE;

  SUM+VAL;

  if LAST.DATE then do;

    VALCAT=SUM;

    SUM=.;

  end;

run;

proc sort; by DATE;

proc gplot;

  plot  VAL*DATE=EL;

  plot2 VALCAT*DATE=CAT;

  run;

quit;

Sorry I can't test the code.

Use symbol statements to color the lines.

Contributor
Posts: 37

Re: How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

Bang on...!!!

This is what I wanted.

This code generates only one graph containing both categories, so I made a change by adding a BY group (Cat).

What is the purpose of creating a composite index in first step?

Thanks.

Harshad M.

Trusted Advisor
Posts: 1,328

Re: How to create a GRAPH like THIS(plotting a category and its constituents in same graph) using SAS|GRAPH

To use the BY statement is the data step.

We could also sort.

Post a Question
Discussion Stats
  • 5 replies
  • 441 views
  • 0 likes
  • 3 in conversation