Graphics Programming

Data visualization using SAS programming, including ODS Graphics and SAS/GRAPH. Charts, plots, maps, and more!
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
palolix
Pyrite | Level 9

Dear SAS Community,

 

I would like to add a significance "*" symbol to the datalabel in sgpanel. I was able to add an "*" to the Season=2021 but I don't know how to also add it in the other seasons too, because the significance varies from season to season. 

Here is my code and graphs:

 

This is how I would like to have the "*" symbols in all seasons:

 

palolix_0-1747958957215.png

 

 

data dlabel;
set one;
if Variety in('BL516') and Harvest in(1,2,6) and Season=2021 then dlabel="*";
else dlabel="";
run;

How could I also include

if Variety in('BL516') and Harvest in(8) and Season=2022 then dlabel="*" ?

 

title "DTR: BL516 vs Hass by Season and Harvest month";
proc sgpanel data=dlabel;
where Season in(2021, 2022, 2023) and Harvest in(1,2,3,4,5,6,8) and Variety in('BL516','Hass');
panelby Season/columns=3 onepanel ;
vline Harvest/response=DTR group=Variety stat=mean datalabel=dlabel ;
rowaxis  values=(0 to 12 by 2) ;
run;

 

palolix_1-1747959218864.png

 

I would greatly appreciate your help!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

That would be a lot of easy if you could post some real data  to test your code.

Anyway, here could give you a start.

 

data have;
call streaminit(123);
do s=1 to 3;
 do h=1 to 8;
  do g=1 to 2;
  v=rand('uniform');output;
  end;
 end;
end;
run;
proc sql;
create table have2 as
select *,max(v)+0.2 as max
 from have 
  group by s,h
   order by s,h
;
quit;

data have2;
set have2;
by s h;
call streaminit(123);
if first.h and mod(h,4)=rand('table',0.3,0.3) then label='*';
run;
proc sgpanel data=have2;
panelby s/onepanel rows=1;
series x=h y=v / group=g markers markerattrs=(symbol=circlefilled);
scatter x=h y=max/datalabel=label datalabelpos=top labelstrip datalabelattrs=(size=20 ) markerattrs=(size=0);
run;
 

Ksharp_0-1747962928933.png

 

View solution in original post

15 REPLIES 15
Ksharp
Super User

That would be a lot of easy if you could post some real data  to test your code.

Anyway, here could give you a start.

 

data have;
call streaminit(123);
do s=1 to 3;
 do h=1 to 8;
  do g=1 to 2;
  v=rand('uniform');output;
  end;
 end;
end;
run;
proc sql;
create table have2 as
select *,max(v)+0.2 as max
 from have 
  group by s,h
   order by s,h
;
quit;

data have2;
set have2;
by s h;
call streaminit(123);
if first.h and mod(h,4)=rand('table',0.3,0.3) then label='*';
run;
proc sgpanel data=have2;
panelby s/onepanel rows=1;
series x=h y=v / group=g markers markerattrs=(symbol=circlefilled);
scatter x=h y=max/datalabel=label datalabelpos=top labelstrip datalabelattrs=(size=20 ) markerattrs=(size=0);
run;
 

Ksharp_0-1747962928933.png

 

palolix
Pyrite | Level 9

Thank you very much Ksharp! I will try to work out that code with my data. 

If you don't mind, I have a another question:

I had to do some formatting in order to reorder the levels of the var 'Harvest' to (11,12,1,2,3,5) but the issue I am having is that even though the order in the graph is the order I want, the label is showing in the graph is 'tag' instead of 'Harvest'. How do I change the label?

 

I would very much appreciate it if you could let me know!

 

proc sort data=one out=one; by Harvest; run;
data one22;
set one;
where Variety in('464918_99','Hass');
if (Harvest eq 11) then tag=1;
else if (Harvest eq 12) then tag=2;
else if (Harvest eq 1) then tag=3;
else if (Harvest eq 2) then tag=4;
else if (Harvest eq 3) then tag=5;
else if (Harvest eq 5) then tag=6;
else tag=.;
run;
 
 
proc format;
value Harvest 1="11"
2="12"
3="1"
4="2"
5="3"
6="5";
run;
 
 
title "DTR: 464918_99 vs Hass by Season and Harvest month";
proc sgpanel data=one22;
where Season in(2021, 2022, 2024) and Harvest in(11,12,1,2,3,5) and Variety in('464918_99','Hass');
panelby Season/rows=1 onepanel ;
format tag Harvest.;
vline tag /response=DTR group=Variety markers markerattrs=(symbol=circlefilled) stat=mean  ;
rowaxis  values=(0 to 12 by 2) ;
run;
 

palolix_0-1748023368501.png

 

 

DanH_sas
SAS Super FREQ

The best way to set the label in this case would be to use the LABEL statement inside of the SGPANEL specification:

label tag="Harvest";

 

This temporarily sets the column label within the scope of the procedure. That way, your axis label, tip labels, etc. pick up the label. It is also possible to set label="Harvest" on the ROWAXIS statement, but that will affect only the axis label.

palolix
Pyrite | Level 9

It worked! Thank you so much!!

palolix
Pyrite | Level 9

Last question;

Is there a way to change the order of the line colors and the Variety legend so that GEM comes first in the legend and has the blue color instead? I have been trying different things but I still cannot figure it out.

Thank you so much!

 

palolix_0-1748042709352.png

title "DTR: GEM vs Hass by Season and Harvest month";
proc sgpanel data=one;
where Season in(2021, 2022, 2023) and Harvest in(1,3,4,5,6,8) and Variety in('GEM','Hass');
panelby Season/rows=1 onepanel ;
vline Harvest/response=DTR group=Variety markers markerattrs=(symbol=circlefilled) stat=mean ;
rowaxis  values=(0 to 12 by 2) ;
run;

Ksharp
Super User

Padding a blank before 'GEM' and PROC SORT it before PROC SGPANEL.

 

data have;
call streaminit(123);
do s=1 to 3;
 do h=1 to 8;
  do g='Hass' ,'GEM';
  v=rand('uniform');output;
  end;
 end;
end;
run;


data have;
 set have;
 if g='GEM' then g=' GEM';
run;
proc sort data=have;
by g;
run;


proc sgpanel data=have;
panelby s/onepanel rows=1;
vline h /response=v group=g markers markerattrs=(symbol=circlefilled) ;
run;

 

 

Ksharp_0-1748053512965.png

 

palolix
Pyrite | Level 9

Thank you very much Ksharp. I tried the code and I am getting the order and colors I want, but I'm getting different data (below 2 DTR). I wonder what am I doing wrong.

 

data one;
call streaminit(123);
do Season=2021 to 2023;
do Harvest=1 to 8;
do Variety='Hass' ,'GEM';
DTR=rand('uniform');output;
end;
end;
end;
run;


data one;
set one;
if Variety='GEM' then Variety=' GEM';
run;
proc sort data=one;
by Variety;
run;



proc sgpanel data=one;
/*where Season in(2021, 2022, 2023) and Harvest in(1,3,4,5,6,8) and Variety in('GEM','Hass');*/
panelby Season/rows=1 onepanel ;
vline Harvest/response=DTR group=Variety markers markerattrs=(symbol=circlefilled) stat=mean ;
rowaxis values=(0 to 12 by 2) ;
run;

 

palolix_0-1748212434958.png

This is the data I should get:

palolix_0-1748213330788.png

 

 

 

Ksharp
Super User

?? That is because you specify XAXIS value by "rowaxis values=(0 to 12 by 2) ;". You need to remove it if the real data don't fall in such range.

 

data one;
call streaminit(123);
do Season=2021 to 2023;
do Harvest=1 to 8;
do Variety='Hass' ,'GEM';
DTR=rand('uniform');output;
end;
end;
end;
run;


data one;
set one;
if Variety='GEM' then Variety=' GEM';
run;
proc sort data=one;
by Variety;
run;



proc sgpanel data=one;
/*where Season in(2021, 2022, 2023) and Harvest in(1,3,4,5,6,8) and Variety in('GEM','Hass');*/
panelby Season/rows=1 onepanel ;
vline Harvest/response=DTR group=Variety markers markerattrs=(symbol=circlefilled) stat=mean ;
/*rowaxis values=(0 to 12 by 2) ;*/
run;

Ksharp_0-1748221162340.png

 

If you want to put series in the middle of graph, you need to make sure the data value have such mean value.

data one;
call streaminit(123);
do Season=2021 to 2023;
do Harvest=1 to 8;
do Variety='Hass' ,'GEM';
DTR=rand('uniform')+6;output;
end;
end;
end;
run;


data one;
set one;
if Variety='GEM' then Variety=' GEM';
run;
proc sort data=one;
by Variety;
run;



proc sgpanel data=one;
/*where Season in(2021, 2022, 2023) and Harvest in(1,3,4,5,6,8) and Variety in('GEM','Hass');*/
panelby Season/rows=1 onepanel ;
vline Harvest/response=DTR group=Variety markers markerattrs=(symbol=circlefilled) stat=mean ;
rowaxis values=(0 to 12 by 2) ;
run;

Ksharp_0-1748221562303.png

 

palolix
Pyrite | Level 9

Thanks for your reply Ksharp. If I remove it I get values from 0 to 1, that is why I set up the rowaxis values going from 0 to 12. What I want is my data to take these values:

 

palolix_0-1748230698822.png

 

Ksharp
Super User
I don't understand . if you want graph to look like it, you need the mean of data should be 6 around.
That is reason I add 6 in my code.

DTR=rand('uniform')+6;output;
palolix
Pyrite | Level 9

I added the 6 but I get this:

palolix_0-1748234770947.png

The original means go from 4 to 10 like in this excel graph. Is there a way to get the same original means in the same range as in this excel graphs?

palolix_2-1748234827015.png

 

Ksharp
Super User
Sorry. The scatters just is plotted by the original value of your data,
if your data has real value like this , you would get this graph .
Otherwise,you need to post your real data and see how is going.
palolix
Pyrite | Level 9

But in the first graph I made in sas, the data was the same as the one in the excel graph (on a range from 4 to 10). I attached my original data in excel. Probably I made not myself clear, but the only thing I wanted to change from this graph was to change the colors of the Varieties (not the data) so that the lines are red for Hass  and blue for GEM (in case that is possible). So sorry for the confusion.

palolix_0-1748238162846.png

 

Ksharp
Super User

I did not get any problem. You need the help of option "grouporder=ascending".

 

proc import datafile='C:\Users\xiakeshan\Documents\Downloads\dtr.xlsx' out=have dbms=xlsx replace;
run;



data one;
set have;
if strip(Variety)='GEM' then Variety='  GEM';
DTR=input(DTR__days_to_ripe_,best.);
run;


proc sgpanel data=one ; 
where Season in(2021, 2022, 2023) and Harvest__month_ in(1,3,4,5,6,8) and  strip(Variety) in ('GEM','Hass');
panelby Season/rows=1 onepanel ;
vline Harvest__month_/response=DTR group=Variety grouporder=ascending markers markerattrs=(symbol=circlefilled) stat=mean ;
rowaxis values=(0 to 12 by 2) ;
run;

Ksharp_0-1748242297718.png

 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 15 replies
  • 1701 views
  • 6 likes
  • 3 in conversation