Hello Experts,
how do we create a plot using the set of data like below:
Category | element | value | date |
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 |
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:
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.
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.
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.
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
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.
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.
To use the BY statement is the data step.
We could also sort.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: