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

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.image.png

I thank you in advance.

Gick,

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/*
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;

Ksharp_0-1694001828119.png

 

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Use PROC SGPLOT with the VBAR statement, using the GROUPDISPLAY=STACK and STAT=PERCENT options.

--
Paige Miller
BrunoMueller
SAS Super FREQ

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;

 

Gick
Pyrite | Level 9

Thank you so much.

 

that works.

Gick
Pyrite | Level 9
On the other hand, I just noticed that you used the Stay variable for the total. While the total must correspond to the number of times an age group is used.

Thanks for your help
Gick
Pyrite | Level 9
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
; run; can someone help me make this graph with this data please

Thanks in advance.

Gick
Ksharp
Super User
/*
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;

Ksharp_0-1694001828119.png

 

BrunoMueller
SAS Super FREQ

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;

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 7 replies
  • 632 views
  • 0 likes
  • 4 in conversation