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

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:
Bild 01.09.19 um 13.35.jpg

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:

Bild 01.09.19 um 13.40.jpg

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Lacona
Quartz | Level 8

HEUREKA!!!

 

Bild 01.09.19 um 19.48 (1).jpg

 

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!

View solution in original post

14 REPLIES 14
Doc_Duke
Rhodochrosite | Level 12

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.

Lacona
Quartz | Level 8

Just to be sure: after proc transpose my dataset should look like this?
Bild 01.09.19 um 15.33.jpg

PaigeMiller
Diamond | Level 26

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.

https://documentation.sas.com/?docsetId=proc&docsetTarget=p03avwj37ggkpkn1mzqzkmwvk1sl.htm&docsetVer...

--
Paige Miller
Lacona
Quartz | Level 8

proc transfer doesn't work:
Bild 01.09.19 um 15.54.jpg

I get this pop-up saying "the chart "WORK.DATA4" can't be opened, because it doesn't contain columns."

Bild 01.09.19 um 15.57.jpg

 

Simple transpose worked:

Bild 01.09.19 um 16.18 (1).jpgBild 01.09.19 um 16.18.jpg

Boxplot doesn't work as wished either:
Bild 01.09.19 um 16.28.jpgBild 01.09.19 um 16.27 (1).jpg

 

 

I'm so sorry that I can't run it, but I just started working with SAS last week.

 

Thank you!

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Lacona
Quartz | Level 8

Next try - didn't work:

Bild 01.09.19 um 16.48.jpgBild 01.09.19 um 16.47.jpg

 

sigh.

Lacona
Quartz | Level 8
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.

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Lacona
Quartz | Level 8
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!!!

Cynthia_sas
SAS Super FREQ

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

Lacona
Quartz | Level 8

Hey, 
I wanted 'income class' as title for the new column and I copied exactly the template from the SAS Help Center:

https://documentation.sas.com/?docsetId=proc&docsetTarget=p03avwj37ggkpkn1mzqzkmwvk1sl.htm&docsetVer...

I have no idea, why it didn't work.

Cynthia_sas
SAS Super FREQ
Hi: "income class" as a variable name is not allowed. you can have income_class as the name and then "Income Class" as the label. But a variable name cannot contain spaces except under very special settings and most of the time, having variable names with spaces turns out to be problematic. Much better to call it something like income_class and then assign a label in a LABEL statement:
label income_class='Income Class';

Cynthia
Lacona
Quartz | Level 8

Thank you! It worked out perfectly fine with income_class.
Now I'm trying to have more values shown on the y-axis.

Lacona
Quartz | Level 8

HEUREKA!!!

 

Bild 01.09.19 um 19.48 (1).jpg

 

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!

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!
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
  • 14 replies
  • 4612 views
  • 1 like
  • 4 in conversation