BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ani7
Obsidian | Level 7

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 = "&current_customer."));
			scatter x=date y=net_price / group=company filledoutlinedmarkers markerattrs=(symbol=circlefilled);
			title font = "Times New Roman" "&current_customer.";
			yaxis max=7 min=0 label="Value";
			xaxis display=(nolabel);
			keylegend / position=n title="" noborder;
		run;

	%end;
%mend plot;

%plot();

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

1 REPLY 1
ballardw
Super User

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.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1814 views
  • 1 like
  • 2 in conversation