Hey,
I'm new to SAS and I try to create side-by-side box plots in SAS university, one for each variable - since days now.
My dataset "income" looks like this:
The assignment is: create side by side box plots for the income for income1, income2, and income3 and comment on this plot.
I guess output shall look like this:
but instead of alpha beta gamma: income1 income2 income3.
proc sgplot data=income;
vbox income1/category=income2;
run;
gives me not even the desired outcome for one boxplot...
After hours and hours of trying I still have no idea how to solve this.
I really hope, you can help me.
Thank you!
HEUREKA!!!
solution:
FILENAME REFFILE '/folders/myfolders/LAB/Income.csv'; PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.IMPORT1; GETNAMES=YES; RUN; PROC CONTENTS DATA=WORK.IMPORT1; RUN; proc sort data=import1; by name; run; proc transpose data=import1 out=boxplot; var income1 income2 income3; by name; run; proc sgplot data=boxplot; vbox col1/category=_name_; run;
It is necessary to proc sort before proc transpose can be run.
out=boxplot (rename=(col1=income class)); was not accepted and caused an error. I coudn't figure out, why...
Thank you so so so much, all! I am soooo happy right now!
Lacona,
To get the boxplot you are looking for, you need to transpose the data first to get 3 columns:
Name, IncomeClass (with values Income1, Income2, and Income3), and the value of the income.
The elegant way to do it is to use PROC TRANSPOSE. However, with this little data, you can just retype the data.
Just to be sure: after proc transpose my dataset should look like this?
Your variable INCOME2 is not a category variable, it is a continuous variable, and so it will not work in PROC SGPLOT as an x-axis category variable.
Instead, you need to re-arrange your data so that you have a category variable to put on the x-axis.
This PROC TRANSPOSE example does exactly what you want, it creates a category variable that you can use on your x-axis.
proc transfer doesn't work:
I get this pop-up saying "the chart "WORK.DATA4" can't be opened, because it doesn't contain columns."
Simple transpose worked:
Boxplot doesn't work as wished either:
I'm so sorry that I can't run it, but I just started working with SAS last week.
Thank you!
You need to use the OUT= option on PROC TRANSPOSE, as shown in the example.
Then you should be able to run PROC SGPLOT on the data set created by PROC TRANSPOSE.
If that doesn't work, show us the entire SAS log (not just the errors) including PROC TRANSPOSE and PROC SGPLOT as text (not as a screen capture) by clicking on the {i} icon and pasting the log into the window that appears. This preserves the formatting of the log and makes it more readable. Do not provide the log via any other method.
Next try - didn't work:
sigh.
FILENAME REFFILE '/folders/myfolders/LAB/Income.csv'; PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.Income; GETNAMES=YES; RUN; PROC CONTENTS DATA=WORK.income; RUN; proc print data=income;
run; run; proc transpose data=income out=boxplot (rename=(col1=income class)); var _income1 _income2 _income3; by name; run;
Thank you for the hint with the I-icon! It is ways easier.
This code gives me ERROR 79-322: expecting a =.
It is not possible to generate a transposed data set.
But this is not a LOG, this is your actual code. We need to see the LOG by pasting it into the {i} icon window. And you don't show the PROC SGPLOT, we need to see the LOG of that part as well.
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 72 73 proc transpose data=income out=boxplot (rename=(col1=income class)); _ 79 ERROR 79-322: Expecting a =. 74 var _income1-_income3; 75 by name; 76 run; NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.BOXPLOT may be incomplete. When this step was stopped there were 0 observations and 0 variables. WARNING: Datei WORK.BOXPLOT wurde nicht ersetzt, da da dieser Schritt angehalten wurde. NOTE: Verwendet wurde: PROZEDUR TRANSPOSE - (Gesamtverarbeitungszeit): real time 0.00 seconds cpu time 0.01 seconds 77 78 79 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 91
Oh! Sure! This is the log I got.
There is no sgplot to show, because the transpose didn't run?!
Without proper transpose no proc sgplot?
Thank you so much for your help!!!
Hi;
What is the purpose of CLASS in the RENAME= option? Why is the variable there. The RENAME= option expects pairs of variables...
(rename=(oldname=newname old2=new2 old3=new3)) ... your code does not follow that syntax rule.
Cynthia
Hey,
I wanted 'income class' as title for the new column and I copied exactly the template from the SAS Help Center:
I have no idea, why it didn't work.
Thank you! It worked out perfectly fine with income_class.
Now I'm trying to have more values shown on the y-axis.
HEUREKA!!!
solution:
FILENAME REFFILE '/folders/myfolders/LAB/Income.csv'; PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.IMPORT1; GETNAMES=YES; RUN; PROC CONTENTS DATA=WORK.IMPORT1; RUN; proc sort data=import1; by name; run; proc transpose data=import1 out=boxplot; var income1 income2 income3; by name; run; proc sgplot data=boxplot; vbox col1/category=_name_; run;
It is necessary to proc sort before proc transpose can be run.
out=boxplot (rename=(col1=income class)); was not accepted and caused an error. I coudn't figure out, why...
Thank you so so so much, all! I am soooo happy right now!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.