I have a dataset (called data_customers) that is structured like this (with several more customers and companies) and I'm trying to make a scatter plot for each customer.
Date | Customer | Company | Value |
1/1/2022 | Customer A | Company A | 2 |
1/2/2022 | Customer A | Company A | 3 |
1/3/2022 | Customer A | Company A | 6 |
1/4/2022 | Customer A | Company B | 3 |
1/5/2022 | Customer A | Company C | 5 |
1/6/2022 | Customer A | Company C | 1 |
1/7/2022 | Customer A | Company C | 3 |
1/8/2022 | Customer A | Company D | 6 |
1/1/2022 | Customer B | Company D | 4 |
1/2/2022 | Customer B | Company D | 6 |
1/3/2022 | Customer B | Company D | 4 |
1/4/2022 | Customer B | Company D | 2 |
1/5/2022 | Customer B | Company D | 5 |
1/6/2022 | Customer B | Company D | 6 |
Since there are a ton of customers that I need to create these plots for, I feed the SGPLOT procedure through a macro loop for each one so that each dot in the final scatter represents the date and value with the color of the dot representing the color of the company. However, in the above example, since the plot for Customer B only contains one customer, the color picked for Company D ends up being the same as the color picked for Company A in the plot for Customer A. Is there a way to manually assign colors to each value in the legend so that these colors end up being consistent across charts?
proc sql;
select distinct customer
into :customers separated by "|"
from data_customers;
%let customer_count = &sqlobs.;
;quit;
%macro plot();
%do i = 1 %to &customer_count.;
%let current_customer = %scan(&customers., &i., "|");
ods graphics on / noborder width=10in height=5in;
proc sgplot data=data_customer (where=(customer = "¤t_customer."));
scatter x=date y=net_price / group=company filledoutlinedmarkers markerattrs=(symbol=circlefilled);
title font = "Times New Roman" "¤t_customer.";
yaxis max=7 min=0 label="Value";
xaxis display=(nolabel);
keylegend / position=n title="" noborder;
run;
%end;
%mend plot;
%plot();
It sounds like you may want a DATTRMAP data set to link the company value to graph characteristics like color and marker type, especially if you want the same company to have the same characteristics in different plots.
It is best to look up the DATTRMAP in the documentation because it requires specifically named variables. You would add an Attrid=<name of the Id> to each plot statement that would use the attributes.
You are likely going about this a bit of hard way with the whole macro approach though.
Sort your data BY Customer (and possibly date if you ever may want Series plots). Then in SGPLOT add a BY Customer; you will get one plot per customer without having to deal with sql and macro approach.
By default the output will have a Customer=<the value> in the output. You can customize titles using the #BYVAL automatic variable and suppress the default by line by using the option NOBYLINE; before the plot.
It sounds like you may want a DATTRMAP data set to link the company value to graph characteristics like color and marker type, especially if you want the same company to have the same characteristics in different plots.
It is best to look up the DATTRMAP in the documentation because it requires specifically named variables. You would add an Attrid=<name of the Id> to each plot statement that would use the attributes.
You are likely going about this a bit of hard way with the whole macro approach though.
Sort your data BY Customer (and possibly date if you ever may want Series plots). Then in SGPLOT add a BY Customer; you will get one plot per customer without having to deal with sql and macro approach.
By default the output will have a Customer=<the value> in the output. You can customize titles using the #BYVAL automatic variable and suppress the default by line by using the option NOBYLINE; before the plot.
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 25. 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.