Good morning,
I have a database like this (see attached)
data Mydata;
input Nom $ Sexe $ classe_age $ Sejour Annee;
datalines;
Jean Masculin 0-4ans 30 2021
Marie Feminin 5-14ans 2 2021
Pierre Masculin 15-24ans 4 2022
J Feminin 0-4ans 31 2022
M Feminin 5-14ans 21 2020
Pe Masculin 0-4ans 41 2021
Je Masculin 15-24ans 38 2022
Ma Masculin 0-4ans 15 2020
Pire Feminin 15-24ans 0 2020
;
run;
I would like to obtain this figure (see attached image).
Can someone help me please.
I thank you in advance.
Gick,
/*
You want this ?
*/
data Mydata;
input Nom $ Sexe $ classe_age $ Annee;
datalines;
Jean Masculin 0-4ans 2021
Marie Feminin 5-14ans 2021
Pierre Masculin 15-24ans 2022
J Feminin 0-4ans 2022
M Feminin 5-14ans 2020
Pe Masculin 0-4ans 2021
Je Masculin 15-24ans 2022
Ma Masculin 0-4ans 2020
Pire Feminin 15-24ans 2020
;
/*
* calculate total
*/
proc sql;
create table total as
select annee,sexe,classe_age,count(*) as sejour from mydata
group by annee,sexe,classe_age
union
select annee,"total",classe_age,count(*) from mydata
group by annee,classe_age
;
quit;
proc sgpanel data=total pctlevel=group;
panelby annee / layout=columnlattice colheaderpos=bottom;
vbar sexe / group=classe_age response=sejour stat=percent name="vbar";
keylegend "vbar" / position=right across=1 noborder autoitemsize;
colaxis display=(nolabel) valuesrotate=vertical;
rowaxis display=(nolabel) grid gridattrs=(color=grey);
run;
Use PROC SGPLOT with the VBAR statement, using the GROUPDISPLAY=STACK and STAT=PERCENT options.
Proc SGPANEL will get you the graph, not pixel by pixel, but the content is right. The total need to be calculated and then combined with the original data. See code example below. The COLAXIS control the appearance of the "X" axis and the ROWAXIS controls the appearance of the "Y" axis. Give it a try.
data Mydata;
input Nom $ Sexe $ classe_age $ Sejour Annee;
datalines;
Jean Masculin 0-4ans 30 2021
Marie Feminin 5-14ans 2 2021
Pierre Masculin 15-24ans 4 2022
J Feminin 0-4ans 31 2022
M Feminin 5-14ans 21 2020
Pe Masculin 0-4ans 41 2021
Je Masculin 15-24ans 38 2022
Ma Masculin 0-4ans 15 2020
Pire Feminin 15-24ans 0 2020
;
/*
* calculate total
*/
proc sql;
create table total as
select
annee
, "total" as sexe
, classe_age
, sum(sejour) as sejour
from
mydata
group by
annee
, sexe
, classe_age
;
quit;
/*
* combine
*/
data have;
set mydata total;
run;
proc sgpanel data=have pctlevel=group;
panelby annee / layout=columnlattice colheaderpos=bottom;
vbar sexe / group=classe_age stat=percent name="vbar";
keylegend "vbar" / position=right across=1 noborder autoitemsize;
colaxis display=(nolabel) valuesrotate=vertical;
rowaxis display=(nolabel) grid gridattrs=(color=grey);
run;
Thank you so much.
that works.
/*
You want this ?
*/
data Mydata;
input Nom $ Sexe $ classe_age $ Annee;
datalines;
Jean Masculin 0-4ans 2021
Marie Feminin 5-14ans 2021
Pierre Masculin 15-24ans 2022
J Feminin 0-4ans 2022
M Feminin 5-14ans 2020
Pe Masculin 0-4ans 2021
Je Masculin 15-24ans 2022
Ma Masculin 0-4ans 2020
Pire Feminin 15-24ans 2020
;
/*
* calculate total
*/
proc sql;
create table total as
select annee,sexe,classe_age,count(*) as sejour from mydata
group by annee,sexe,classe_age
union
select annee,"total",classe_age,count(*) from mydata
group by annee,classe_age
;
quit;
proc sgpanel data=total pctlevel=group;
panelby annee / layout=columnlattice colheaderpos=bottom;
vbar sexe / group=classe_age response=sejour stat=percent name="vbar";
keylegend "vbar" / position=right across=1 noborder autoitemsize;
colaxis display=(nolabel) valuesrotate=vertical;
rowaxis display=(nolabel) grid gridattrs=(color=grey);
run;
The VBAR statement calculated the frequencies, number of times a given agegroup occurs within the sexe and annee by default.
You can just calculate the total differently. See below the changed code. I have added a variable N to the input data with a value of 1. For the total calculation I use the count(*) to count the number of rows in a given group. This new variable N is now used in the VBAR statement as the response variable. It should now give the proper values back. I have also added some options to the PANELBY
data Mydata;
input Nom $ Sexe $ classe_age $ Sejour Annee;
n = 1;
datalines;
Jean Masculin 0-4ans 30 2021
Marie Feminin 5-14ans 2 2021
Pierre Masculin 15-24ans 4 2022
J Feminin 0-4ans 31 2022
M Feminin 5-14ans 21 2020
Pe Masculin 0-4ans 41 2021
Je Masculin 15-24ans 38 2022
Ma Masculin 0-4ans 15 2020
Pire Feminin 15-24ans 0 2020
;
/*
* calculate total
*/
proc sql;
create table total as
select
annee
, "total" as sexe
, classe_age
, sum(sejour) as sejour
, count(*) as n
from
mydata
group by
annee
, sexe
, classe_age
;
quit;
/*
* combine
*/
data have;
set mydata total;
run;
proc sgpanel data=have pctlevel=group;
panelby annee / layout=columnlattice colheaderpos=bottom novarname noheaderborder;
vbar sexe / group=classe_age response=n stat=percent name="vbar";
keylegend "vbar" / position=right across=1 noborder autoitemsize;
colaxis display=(nolabel) valuesrotate=vertical;
rowaxis display=(nolabel) grid gridattrs=(color=grey);
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.