Published on :

Introduction to SAS Studio Examples

This code is also available in: Deutsch Español Français
Awaiting validation
The script demonstrates the use of PROC PRINT to display data (complete or selected) from the SASHELP.CARS dataset. It also uses PROC SQL to create a new table (WORK.query) by filtering and ordering data from SASHELP.CARS. PROC DATASETS is employed to extract metadata from this new table into WORK.details, which is then displayed by PROC PRINT. Descriptive statistics are calculated on the 'Weight' variable of the SASHELP.CARS dataset using PROC MEANS. Finally, PROC SGPLOT is used to generate a bar chart visualizing population by region from SASHELP.DEMOGRAPHICS, with ODS Graphics options for output.
Data Analysis

Type : SASHELP


Primary source data comes from SASHELP libraries, specifically SASHELP.CARS and SASHELP.DEMOGRAPHICS. Intermediate work tables (WORK.query, WORK.details) are created and used throughout the script from this source data.

1 Code Block
PROC PRINT
Explanation :
Displays the entire SASHELP.CARS dataset in the log or default output, providing a quick overview of the raw data.
Copied!
1PROC PRINT DATA=sashelp.cars;
2RUN;
2 Code Block
PROC PRINT
Explanation :
Displays a specific selection of variables (Make, Model, Type, etc.) from the SASHELP.CARS dataset. The 'by Make' clause organizes the output by manufacturer, allowing observations to be grouped for better readability.
Copied!
1PROC PRINT DATA=sashelp.cars;
2 BY Make;
3 var Make Model Type DriveTrain EngineSize Cylinders Horsepower MPG_City MPG_Highway;
4RUN;
3 Code Block
PROC SQL Data
Explanation :
Creates a new temporary SAS table named 'WORK.query' using an SQL query. This query selects several columns from the SASHELP.CARS dataset, filters cars with horsepower greater than or equal to 400, and sorts the results by descending horsepower to identify the most powerful vehicles.
Copied!
1PROC SQL;
2CREATE TABLE WORK.query AS
3SELECT Make , Model , 'Type'n , Origin , DriveTrain , EngineSize , Cylinders , Horsepower , MPG_City , MPG_Highway , Weight , Wheelbase FROM SASHELP.CARS WHERE Horsepower >=400 ORDER BY Horsepower DESCENDING;
4RUN;
5QUIT;
4 Code Block
PROC DATASETS Data
Explanation :
Uses PROC DATASETS to generate a detailed content report (metadata) for the previously created 'WORK.query' dataset. The result is saved into a new SAS table, 'WORK.details', rather than being displayed directly in the log. The NOLIST and NODETAILS options suppress procedure output to focus solely on creating the metadata table.
Copied!
1 
2PROC DATASETS NOLIST NODETAILS;
3CONTENTS
4DATA=WORK.query OUT=WORK.details;
5RUN;
6 
5 Code Block
PROC PRINT
Explanation :
Displays the content of the 'WORK.details' table, which contains the metadata (variable names, types, lengths, formats, etc.) of the 'WORK.query' dataset. This allows for examining the structure of the intermediate table.
Copied!
1PROC PRINT DATA=WORK.details;
2RUN;
6 Code Block
PROC MEANS
Explanation :
Calculates descriptive statistics (mean, standard deviation, minimum, maximum, number of observations) for the 'Weight' variable of the SASHELP.CARS dataset. The 'chartype' option is used for the data type of the analysis variable, and 'vardef=df' specifies that the divisor for standard deviation and variance should be based on degrees of freedom.
Copied!
1 
2PROC MEANS
3DATA=SASHELP.CARS chartype mean std min max n vardef=df;
4var Weight;
5RUN;
6 
7 Code Block
PROC SGPLOT
Explanation :
Generates a bar chart visualizing 'Population by Region' from the SASHELP.DEMOGRAPHICS dataset. ODS Graphics options are first reset and configured. The bar chart uses the 'region' variable for categories and 'pop' (population) as the response variable, displaying the average population by region. Specific fill and style attributes are applied for better presentation. Finally, ODS Graphics options and the title are reset after the procedure to prevent them from affecting subsequent outputs.
Copied!
1ods graphics / reset imagemap;
2 
3/*--SGPLOT proc statement--*/
4PROC SGPLOT DATA=SASHELP.DEMOGRAPHICS;
5 /*--TITLE and FOOTNOTE--*/
6 title 'Population by Region';
7 
8 /*--Bar chart settings--*/
9 vbar region / response=pop fillattrs=(color=CX0a66f1) stat=Mean dataskin=Crisp
10 name='Bar';
11 
12 /*--Response Axis--*/
13 yaxis grid;
14RUN;
15 
16ods graphics / reset;
17title;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Getting Started with SAS Studio; 10/28/2015; http://support.sas.com/training/tutorial/studio/get-started.html