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:
Do you have a solution please ?
Thanks you
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;
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:
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 obtained
2) Ideally, i want to have the graphic like graphic_wanted in the attached files
Thanks you for your help 🙂graphic wanted
Here are my data 🙂
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.
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 ?
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*
* arbitrarily using "green" and "red" for "&couleur_1." and "&couleur_2.", resp.
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.