Hi,
I have a large dataset with the variable p_sex.
I made a subset of data including only the sex variable (p_sex).
data sex; set output_data;
where redcap_repeat_instrument = "demographics";
keep p_sex;
run;
proc gchart data=sex;
vbar p_sex;
run;
I expected 2 columns (one for female and one for male),
However, I got a chart with a lot of empty columns in between 😕
The dataset called sex is as follows:
Is there any statement I am missing to make sure only to get two columns representing 1 and 2, respectively
data work.class;
LENGTH sexFM $ 6;
set sashelp.class;
p_sex = (sex='F'); /* Boolean expression */
p_sex = p_sex + 1; /* 1 for male ; 2 for female */
if p_sex = 1 then sexFM ='Male';
else if p_sex = 2 then sexFM ='Female';
else;
run;
proc sgplot data=work.class;
title "abc xyz";
vbar sexFM;
xaxis label='sexFM : Male and Female variable';
run;
title;
/* end of program */
Koen
Treat gender as a category variable (instead of what you are doing which seems to treat gender as continuous).
Use PROC SGPLOT with the VBAR statement. Example:
proc sgplot data=sashelp.class;
vbar sex;
run;
data work.class;
set sashelp.class;
p_sex = (sex='F'); /* Boolean expression */
p_sex = p_sex + 1; /* 1 for male ; 2 for female */
run;
/*
proc gchart data=work.class;
vbar p_sex;
run;
QUIT;
*/
proc sgplot data=work.class;
title "abc xyz";
vbar p_sex;
run;
title;
/* end of program */
Koen
Hi,
When I copy and apply this code directly to SAS, I get this output:
I would like 1 and 2 to be replaced - in stead female and male
Is that not possible
data work.class;
LENGTH sexFM $ 6;
set sashelp.class;
p_sex = (sex='F'); /* Boolean expression */
p_sex = p_sex + 1; /* 1 for male ; 2 for female */
if p_sex = 1 then sexFM ='Male';
else if p_sex = 2 then sexFM ='Female';
else;
run;
proc sgplot data=work.class;
title "abc xyz";
vbar sexFM;
xaxis label='sexFM : Male and Female variable';
run;
title;
/* end of program */
Koen
Perfect , thank you.
This worked.
I see the first part of the code is making a new column called sexFm with the replaced numbers.
I tried deleting row 4-5 and tried running the code. There were no differences, so I deleted those and still got it presentated as I wished.
Problem solved. Thanks a lot !
Hello @rookie21 ,
Just make sure that 1 and 2 are correctly mapped to Male and Female respectively.
But maybe you want it (or it is) 'the other way around'.
I do not know whether your 1 equals 'Male' and your 2 equals 'Female' (maybe it's the other way around).
Thanks,
Koen
@sbxkoenk wrote:
data work.class; LENGTH sexFM $ 6; set sashelp.class; p_sex = (sex='F'); /* Boolean expression */ p_sex = p_sex + 1; /* 1 for male ; 2 for female */ if p_sex = 1 then sexFM ='Male'; else if p_sex = 2 then sexFM ='Female'; else; run; proc sgplot data=work.class; title "abc xyz"; vbar sexFM; xaxis label='sexFM : Male and Female variable'; run; title; /* end of program */
Koen
I have a different solution for the original data set from @rookie21 , which I think is a lot simpler, and a lot less typing.
proc format;
value sexf 1='Male' 2='Female';
run;
proc sgplot data=sex;
vbar p_sex;
xaxis valuesformat=sexf.;
run;
Formats are an extremely powerful tool in SAS. You can display or perform different analysis for the same values just by changing the format.
For example I have formats for persons age that do 3-year , 5-year,10-year intervals plus specific age ranges such as 0-5, 6-13,14-18, or 18-24,25-44,45 and older, or over/under some specific age limit, 18, 21 for example.
The groups created by the formats are honored by analysis, reporting and graphing procedures. So just changing the format name in procedure like SGPLOT I change from showing the 5-year groups to the over/under 18.
Or if the boss wants more text instead of "Male" to "Adult Male" or "M" or "Male with Diabetes", create a new format with the different label on an axis or table header.
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.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.