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

I have a student with a minor code problem that we can't resolve, so I'm asking for help.  Her VBAR graphs have data value labels, except for three of them (CAPPUN, SPKHOMOY, COLHOMO). All are binary variables, but the graphs for these three will only label the bars with (1, 2) or (4, 5) rather than the assigned label for that value.  I am new to SAS and cannot identify the problem.  I am sure I am overlooking something obvious and appreciate the help.

DATA new; SET mydata.gss2021; 
 
/*Collapsing Data*/ 
If POLVIEWS=1 or POLVIEWS=2 or POLVIEWS=3 
Then POLSPECTRUM = "Liberal"; 
If POLVIEWS=4 
Then POLSPECTRUM = "Moderate"; 
If POLVIEWS=5 or POLVIEWS=6 or POLVIEWS=7 
Then POLSPECTRUM = "Conservative"; 
If EDUC=0 
Then EDUCLVL = "No Education"; 
If EDUC=1 or EDUC=2 or EDUC=3 or EDUC=4 or EDUC=5 or EDUC=6 or EDUC=7 or EDUC=8 or EDUC=9 or EDUC=10 or EDUC=11 
Then EDUCLVL = "Below Highschool Degree"; 
If EDUC=12 
Then EDUCLVL = "Highschool Degree"; 
If EDUC=13 or EDUC=14 or  EDUC=15 or EDUC=16 or EDUC=17 or EDUC=18 or EDUC=19 or EDUC=20 
Then EDUCLVL = "College"; 
/* label your data */ 
LABEL POLVIEWS ="Political views" 
     EDUC = "Education" 
     PRES16 = "Hillary or Donald" 
     IF16WHO = "If you did not vote, who would you vote" 
     RELIG = "Religion" 
     CAPPUN = "Death penalty" 
     SPKHOMOY = "Speak publicly" 
     COLHOMO = "College professors" 
     LIBHOMOY = "Gay books in libraries" 
     POLSPECTRUM = "Political Spectrum" 
     EDUCLVL = "Education Level"; 
RUN; 
/*Renaming numbers to words*/ 
PROC FORMAT; 
/*Explanotory*/ 
VALUE POLVIEWS 
1 = "Extremely Liberal" 
2 = "Liberal" 
3 = "Slightly Liberal" 
4 = "Moderate" 
5 = "Slightly Conservative" 
6 = "Conservative" 
7 = "Extremely Conservative"; 
 
VALUE EDUC 
0 = "No School" 
1 = "1st Grade" 
2 = "2nd Grade" 
3 = "3rd Grade" 
4 = "4th Grade" 
5 = "5th Grade" 
6 = "6th Grade" 
7 = "7th Grade" 
8 = "8th Grade" 
9 = "9th Grade" 
10 = "10th Grade" 
11 = "11th Grade" 
12 = "12th Grade" 
13 = "1 year of college" 
14 = "2 years of college" 
15 = "3 years of college" 
16 = "4 years of college" 
17 = "5 years of college" 
18 = "6 years of college" 
19 = "7 years of college" 
20 = "8 years of college"; 
 
/*Value PRES16 
1 = "Clinton" 
2 = "Trump" 
3 = "Other Candidate" 
4 = "Did Not Vote For President";*/ 
 
Value IF16WHO 
1 = "Clinton" 
2 = "Trump" 
3 = "Other Candidate"; 
 
Value RELIG 
1 = "Protestant" 
2 = "Catholic" 
3 = "Jewish" 
4 = "None" 
5 = "Other" 
6 = "Buhdhism" 
7 = "Hinduism" 
8 = "Other Eastern Religions" 
9 = "Muslim/Islam" 
10 = "Orthodox Christian" 
11 = "Christian" 
12 = "Native American" 
13 = "Inter-Nondenominational"; 
 
/* response*/ 
Value CAPPUN 
1 = "yes" 
2 = "no"; 
 
VALUE SPKHOMOY 
1 = "Yes Allowed" 
2 = "Not Allowed";  
 
VALUE COLHOMO 
4 = "Yes Allowed" 
5 = "Not Allowed"; 
run; 
 
/*Make univariate graphs*/ 
/*Explanatory*/ 
PROC SGPLOT; 
VBAR POlVIEWS/stat=percent datalabel; 
title height=20pt color=pink"Range of political views"; 
PROC SGPLOT; 
VBAR EDUC/stat=percent datalabel; 
title height=20pt color=pink"Education Level"; 
PROC SGPLOT; 
VBAR PRES16/stat=percent datalabel; 
title height=20pt color=pink"Did you vote Hillary or Donald?"; 
PROC SGPLOT; 
VBAR IF16WHO/stat=percent datalabel; 
title height=20pt color=pink"If you did not vote, would you vote Hillary or Donald?"; 
PROC SGPLOT; 
VBAR RELIG/stat=percent datalabel; 
title height=20pt color=pink"What religion are you?"; 
PROC SGPLOT; 
VBAR CAPPUN/stat=percent datalabel; 
title height=20pt color=pink"Are you for or against death penalty?"; 
 
/*Response*/ 
PROC SGPLOT; 
VBAR SPKHOMOY/stat=percent datalabel; 
title height=20pt color=pink"Should gay people be allowed to speak publicly?"; 
PROC SGPLOT; 
VBAR COLHOMO/stat=percent datalabel; 
title height=20pt color=pink"Should gay people be college professors?"; 
PROC SGPLOT; 
VBAR LIBHOMOY/stat=percent datalabel; 
title height=20pt color=pink"Should books that favor gay people be removed from libraries?"; 
/*bivariate*/ 
PROC SGPLOT pctlevel=Group; 
VBAR POLSPECTRUM/Group=SPKHOMOY Groupdisplay=cluster stat=percent seglabel; 
title 'Political views on if gay people should be allowed to speak publicly'; 
PROC SGPLOT pctlevel=Group; 
VBAR POLSPECTRUM/Group=COLHOMO Groupdisplay=cluster stat=percent seglabel; 
title 'political views on if gay people should be college proffesors'; 
PROC SGPLOT pctlevel=Group; 
VBAR POLSPECTRUM/Group=LIBHOMOY Groupdisplay=cluster stat=percent seglabel; 
title 'political views on if books about gay people should be removed from libraries or not'; 
PROC SGPLOT pctlevel=Group; 
VBAR EDUCLVL/Group=SPKHOMOY Groupdisplay=cluster stat=percent seglabel; 
title 'education level on if gay people should be allowed to speak publicly'; 
PROC SGPLOT pctlevel=Group; 
VBAR EDUCLVL/Group=COLHOMO Groupdisplay=cluster stat=percent seglabel; 
title 'education level on if gay people should be college professors'; 
PROC SGPLOT pctlevel=Group; 
VBAR EDUCLVL/Group=LIBHOMOY Groupdisplay=cluster stat=percent seglabel; 
title 'education level on if books about gay people should be removed from libraries'; 
proc freq; 
table polspectrum; 
run; 
/*chi-square test and post-hoc test*/ 
PROC SORT; By EDUCLVL; 
PROC FREQ; 
TABLE COLHOMO*POLSPECTRUM/CHISQ; 
By EDUCLVL; 
 
/* Conservatives with Liberals*/ 
Data conlib; SET NEW; 
If POLSPECTRUM="Conserv" OR POLSPECTRUM="Liberal"; 
/*PROC SORT; By ID;*/ 
PROC FREQ; 
Tables COLHOMO*POLSPECTRUM/CHISQ; 
/* Conservatives with Moderates*/ 
Data conmod; SET NEW; 
If POLSPECTRUM="Conserv" OR POLSPECTRUM="Moderat"; 
PROC SORT; By ID; 
PROC FREQ; 
Tables COLHOMO*POLSPECTRUM/CHISQ; 
/* Moderates with Liberals*/ 
Data modlib; SET NEW; 
If POLSPECTRUM="Moderat" OR POLSPECTRUM="Liberal"; 
PROC SORT; By ID; 
PROC FREQ; 
Tables COLHOMO*POLSPECTRUM/CHISQ; 
 
/* make frequency tables */ 
PROC FREQ; TABLES POLVIEWS EDUC PRES16 IF16WHO RELIG CAPPUN SPKHOMOY COLHOMO LIBHOMOY POLSPECTRUM EDUCLVL; 
RUN; 
 
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

We have none of your data.

I also do not see where you apply any of the formats you created to those variables.

Try adding

Format variablename formatname. ;

to your plots.

 

While procedures will use the last created data set for operations you really want to get into the habit of specifically naming the input data set.

View solution in original post

2 REPLIES 2
ballardw
Super User

We have none of your data.

I also do not see where you apply any of the formats you created to those variables.

Try adding

Format variablename formatname. ;

to your plots.

 

While procedures will use the last created data set for operations you really want to get into the habit of specifically naming the input data set.

RiverRun
Calcite | Level 5

Thank you.  Helpful.

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
  • 2 replies
  • 457 views
  • 0 likes
  • 2 in conversation