BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rookie21
Obsidian | Level 7

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  😕 

 

JT9_0-1638616206193.png

 

The dataset called sex is as follows: 

JT9_1-1638616281699.png

 

Is there any statement I am missing to make sure only to get two columns representing 1 and 2, respectively 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ
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

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
rookie21
Obsidian | Level 7
Thank you so much.
I tried to apply your code without changing anything.

proc sgplot data=output_data;
vbar p_sex;
run;

This helped. Got only 2 bars. THANK YOU

Can I label in the code or rename the row name before running the code:
- 1 for male
- 2 for female ?
sbxkoenk
SAS Super FREQ
 
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

rookie21
Obsidian | Level 7

Hi, 

When I copy and apply this code directly to SAS, I get this output: 

JT9_0-1638623394262.png

 

I would like 1 and 2 to be replaced - in stead female and male 

 

Is that not possible 

sbxkoenk
SAS Super FREQ
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

rookie21
Obsidian | Level 7

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 ! 

sbxkoenk
SAS Super FREQ

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

rookie21
Obsidian | Level 7
Yes, I will double check, thx
PaigeMiller
Diamond | Level 26

@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;

 

--
Paige Miller
rookie21
Obsidian | Level 7
Thank you, the shorter, the better 🙂
ballardw
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 1047 views
  • 9 likes
  • 4 in conversation