Published on :

Understanding SAS Program Syntax

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© script serves as a didactic example for understanding the basic syntax of SAS© programs and common data manipulation and reporting tasks. It begins with comments on SAS© syntax. The DATA step named 'mycars' reads observations from the 'sashelp.cars' dataset, calculates the average miles per gallon ('AvgMPG') from city and highway MPG, and stores this information in a new dataset. Then, PROC PRINT is used to display specific variables ('make', 'model', 'type', 'avgmpg') for cars whose AvgMPG exceeds 35, with an appropriate title. Finally, PROC MEANS calculates the mean, minimum, and maximum of AvgMPG, grouped by car 'type', also with a title. The final TITLE; command resets global titles.
Data Analysis

Type : SASHELP


The script uses the 'cars' dataset from the 'SASHELP' library, which is an example dataset provided with SAS and accessible by default.

1 Code Block
DATA STEP Data
Explanation :
This DATA block creates a new dataset named 'mycars' by copying observations from the 'sashelp.cars' dataset. It then calculates a new variable 'AvgMPG' as the mean of 'mpg_city' and 'mpg_highway' for each observation.
Copied!
1DATA mycars;
2 SET sashelp.cars;
3 AvgMPG=mean(mpg_city, mpg_highway);
4RUN;
2 Code Block
PROC PRINT
Explanation :
This block uses PROC PRINT to display the contents of the 'mycars' dataset. It selects the variables 'make', 'model', 'type', and 'avgmpg' and filters observations to include only those where 'AvgMPG' is greater than 35. A title is also applied to the report.
Copied!
1title "Cars with Average MPG Over 35";
2PROC PRINT DATA=mycars;
3 var make model type avgmpg;
4 where AvgMPG > 35;
5RUN;
3 Code Block
PROC MEANS
Explanation :
This block uses PROC MEANS to calculate descriptive statistics (mean, minimum, maximum) for the 'avgmpg' variable from the 'mycars' dataset. The results are grouped by the 'type' variable and displayed with one decimal place. A title is applied to the report. The final TITLE; command resets global titles.
Copied!
1title "Average MPG by Car Type";
2PROC MEANS DATA=mycars
3 mean min max maxdec=1;
4 var avgmpg;
5 class type;
6RUN;
7TITLE;
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.