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

Hello,

 

I have my dataset PYR_AGE_POP_4 (in an excel file joined) and i want to have the variable poids displayed inside each bar but i can't do it.

Do you have an idea please ?

There is below my currently code:

 
proc format;
picture posval low-high='000,009';
run;
 
legend label=(" ");
AXIS1  LABEL=none MAJOR=none MINOR=none STYLE=0;
axis2  label=none  order=('60 ans et plus' '51 - 60 ans' '41 - 50 ans' '31 - 40 ans' '21 - 30 ans' '0 - 20 ans');
pattern1 value=solid color="&couleur_1";
pattern2 value=solid color="&couleur_2";
 
 
PROC GCHART DATA = PYR_AGE_POP_4 ;
format pop posval.;
  HBAR DRS_TRANCHE_AGE / DISCRETE NOSTATS SUMVAR = pop  TYPE = SUM SUBGROUP = DRS_SEXE1 GSPACE=0 space=0
                   legend=legend
               RAXIS=axis1
               MAXIS=axis2 autoref 
                   ;
   
RUN ; QUIT ;

 

Do you have a solution please ?

 

Thanks you 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input poids	DRS_TRANCHE_AGE	& $40. DRS_SEXE $	pop	 DRS_SEXE1 $;
cards;
0.34	0 - 20 ans	  F	-557	Femmes
0.29	0 - 20 ans	  M	601	Hommes
0.24	21 - 30 ans  	M	490	Hommes
0.25	21 - 30 ans  	F	-402	Femmes
0.29	31 - 40 ans  	M	591	Hommes
0.28	31 - 40 ans  	F	-465	Femmes
0.13	41 - 50 ans	   M	264	Hommes
0.09	41 - 50 ans  	F	-154	Femmes
0.03	51 - 60 ans	   F	-52	Femmes
0.05	51 - 60 ans	   M	104	Hommes
0.01	60 ans et plus  	 F	-10	Femmes
0.01	60 ans et plus	  M	15	Hommes
;

proc format;
picture fmt
low-0='00000'
;
run;
proc sort data=have out=have2 ;
by DRS_SEXE1 pop;
run;
data have2;
 set have;
 if poids>=0.05 then x2=pop/2;
  else x3=pop+ifn(pop>0,50,-50);
run;
proc sgplot data=have2;
styleattrs datacolors=(MediumVioletRed navy);
hbarparm category=DRS_TRANCHE_AGE response=pop/group=DRS_SEXE1 nooutline name='x';
scatter x=x2 y=DRS_TRANCHE_AGE/markerchar=poids labelstrip markercharattrs=(color=white size=12);
scatter x=x3 y=DRS_TRANCHE_AGE/markerchar=poids labelstrip markercharattrs=(color=black size=12);

yaxis reverse label=' ';
xaxis values=(-700 to 700 by 100);
keylegend 'x'/title='';
format pop fmt. poids percent8.0;
run;

Ksharp_0-1707466427075.png

 

View solution in original post

8 REPLIES 8
Reeza
Super User
Please show a sample of your data format and desired graph.

GCHART still has some uses, but typically the recommendation is to use SGPLOT instead.

An HBAR statement + a TEXT statement may be what you need in SGPLOT.
Mathassens
Fluorite | Level 6

Hello,

 

Thanks you Reeza for your answer.

 

I am now trying to use the sgplot procedure to get what i want.

Here is my code:

PROC SGPLOT DATA=PYR_AGE_POP_4 noborder nowall noautolegend dattrmap=essai1;
format pop posval. poids percentn8.;
HBARparm category=DRS_TRANCHE_AGE  response=pop / barwidth=0.4  group=DRS_SEXE1  datalabel GROUPDISPLAY=CLUSTER COLORMODEL=("&couleur_1." "&couleur_2.") colorresponse=pop
           fillattrs=(color="&couleur_2.") legendlabel="" name='hbar';
  text text=poids x=position_x y=DRS_TRANCHE_AGE   /
   strip position=center /*backfill fillattrs=(color=grey  transparency=.3)*/
   textattrs=(size=8 color=grey weight=bold family=arial ) GROUPDISPLAY=CLUSTER/*legendlabel="poids"*/ legendlabel="poids" splitpolicy=splitalways;
XAXIS label="Effectif par sexe" ;
YAXIS label="" /*offsetmin=0*/;
  KEYLEGEND 'hbar'/ location=outside POSITION=bottom NOBORDER ;
RUN ;

But i have somme issues:

1) the color of my legend aren't the same than in my graphic. (graphic_obtained in the attached files)

graphic obtainedgraphic obtained

2) Ideally, i want to have  the graphic like graphic_wanted in the attached files   

 

 

 

Thanks you for your help 🙂graphic wantedgraphic wanted

Mathassens
Fluorite | Level 6

Here are my data 🙂

Reeza
Super User

Check out this post, which is far beyond what you want (an animated population pyramid, not static) but it gives you the code and example you need.

 

https://blogs.sas.com/content/graphicallyspeaking/2021/06/02/improving-a-population-pyramid-animatio...

Mathassens
Fluorite | Level 6

Thanks you Reeza

 

I have now the following code and it's really  close of my aim, but i don't understand why the squares in my legend are not in the same colors than in my bars.

In the legend, the suare are not filled with the colours of my bars 😞 

Could you help me please ?

 

PROC SGPLOT DATA=PYR_AGE_POP_4 noborder nowall noautolegend dattrmap=essai1;
format pop posval. poids percentn8.;
HBARparm category=DRS_TRANCHE_AGE  response=pop /  group=DRS_SEXE1   
         datalabel GROUPDISPLAY=stack COLORMODEL=("&couleur_1." "&couleur_2.") colorresponse=pop name='hbar';
  text text=poids x=position_x y=DRS_TRANCHE_AGE   /
   strip position=center 
   textattrs=(size=8 color=white weight=bold family=arial ) legendlabel="poids" splitpolicy=splitalways;
 
  XAXIS display=(nolabel);
  YAXIS display=(nolabel noline noticks) ;
 
  KEYLEGEND 'hbar'/ location=outside POSITION=bottom NOBORDER ;
 
RUN ;

 

FreelanceReinh
Jade | Level 19

Hello @Mathassens,

 


@Mathassens wrote:

... i don't understand why the squares in my legend are not in the same colors than in my bars.


I think this is because you are using the COLORRESPONSE= option, which corresponds to a gradient legend (displaying a range of colors) rather than a key legend (with only two colors). It appears that SAS uses neutral colors in the two squares of the key legend as they can't accommodate the color gradient. So, you may want to either omit the COLORRESPONSE= option and thus use only two colors -- then the key legend should show these two colors -- or use a GRADLEGEND statement instead of the KEYLEGEND statement and maybe a trick to label the left and right half of the gradient legend with "Femmes" and "Hommes", respectively:

  label pop='Femmes                                       Hommes';
  gradlegend 'hbar' / position=bottom;

Resulting legend*

gradlegend.png

 

* arbitrarily using "green" and "red" for "&couleur_1."  and "&couleur_2.", resp.

Ksharp
Super User
data have;
infile cards expandtabs truncover;
input poids	DRS_TRANCHE_AGE	& $40. DRS_SEXE $	pop	 DRS_SEXE1 $;
cards;
0.34	0 - 20 ans	  F	-557	Femmes
0.29	0 - 20 ans	  M	601	Hommes
0.24	21 - 30 ans  	M	490	Hommes
0.25	21 - 30 ans  	F	-402	Femmes
0.29	31 - 40 ans  	M	591	Hommes
0.28	31 - 40 ans  	F	-465	Femmes
0.13	41 - 50 ans	   M	264	Hommes
0.09	41 - 50 ans  	F	-154	Femmes
0.03	51 - 60 ans	   F	-52	Femmes
0.05	51 - 60 ans	   M	104	Hommes
0.01	60 ans et plus  	 F	-10	Femmes
0.01	60 ans et plus	  M	15	Hommes
;

proc format;
picture fmt
low-0='00000'
;
run;
proc sort data=have out=have2 ;
by DRS_SEXE1 pop;
run;
data have2;
 set have;
 if poids>=0.05 then x2=pop/2;
  else x3=pop+ifn(pop>0,50,-50);
run;
proc sgplot data=have2;
styleattrs datacolors=(MediumVioletRed navy);
hbarparm category=DRS_TRANCHE_AGE response=pop/group=DRS_SEXE1 nooutline name='x';
scatter x=x2 y=DRS_TRANCHE_AGE/markerchar=poids labelstrip markercharattrs=(color=white size=12);
scatter x=x3 y=DRS_TRANCHE_AGE/markerchar=poids labelstrip markercharattrs=(color=black size=12);

yaxis reverse label=' ';
xaxis values=(-700 to 700 by 100);
keylegend 'x'/title='';
format pop fmt. poids percent8.0;
run;

Ksharp_0-1707466427075.png

 

Mathassens
Fluorite | Level 6
Thanks you Ksharp and FreelanceReinh.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1502 views
  • 5 likes
  • 4 in conversation