BOOK SALES & POPULARITY ANALYSIS USING SAS PROC SQL | PROC MEANS | PROC RANK | PROC FREQ | PROC SGPLOT | MACROS | DATE FUNCTIONS | ADDITIONAL SAS STATEMENTS
option nocenter;
1.BOOKS DATASET CREATION
data books;
length Book_Name $30 Author $25 Genre $15;
format Release_Date date9.;
input Book_Name $ Author $ Pages Sales Rating Genre $ Release_Year;
Release_Date = mdy(6,15,Release_Year);
datalines;
HarryPotter Rowling 500 120 9.2 Fantasy 1997
LOTR Tolkien 1178 150 9.5 Fantasy 1954
Alchemist Coelho 208 65 8.6 Fiction 1988
DaVinciCode Brown 689 80 8.4 Thriller 2003
Twilight Meyer 498 90 7.2 Romance 2005
HungerGames Collins 374 85 8.1 SciFi 2008
Sherlock Holmes 221 70 9.0 Mystery 1892
GameOfThrones Martin 835 75 9.3 Fantasy 1996
FaultInStars Green 313 60 8.0 Romance 2012
Inferno Brown 480 55 7.9 Thriller 2013
Dune Herbert 658 68 9.1 SciFi 1965
AtomicHabits Clear 320 95 8.8 SelfHelp 2018
;
run;
proc print data=books;
run;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year |
|---|---|---|---|---|---|---|---|---|
| 1 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 |
| 2 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 |
| 3 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 |
| 4 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 |
| 5 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 |
| 6 | HungerGames | Collins | SciFi | 15JUN2008 | 374 | 85 | 8.1 | 2008 |
| 7 | Sherlock | Holmes | Mystery | 15JUN1892 | 221 | 70 | 9.0 | 1892 |
| 8 | GameOfThrones | Martin | Fantasy | 15JUN1996 | 835 | 75 | 9.3 | 1996 |
| 9 | FaultInStars | Green | Romance | 15JUN2012 | 313 | 60 | 8.0 | 2012 |
| 10 | Inferno | Brown | Thriller | 15JUN2013 | 480 | 55 | 7.9 | 2013 |
| 11 | Dune | Herbert | SciFi | 15JUN1965 | 658 | 68 | 9.1 | 1965 |
| 12 | AtomicHabits | Clear | SelfHelp | 15JUN2018 | 320 | 95 | 8.8 | 2018 |
2.DATA VALIDATION & METADATA CHECKS
PROC CONTENTS
proc contents data=books;
run;
OUTPUT:
The CONTENTS Procedure
| Data Set Name | WORK.BOOKS | Observations | 12 |
|---|---|---|---|
| Member Type | DATA | Variables | 8 |
| Engine | V9 | Indexes | 0 |
| Created | 12/21/2025 07:36:37 | Observation Length | 112 |
| Last Modified | 12/21/2025 07:36:37 | Deleted Observations | 0 |
| Protection | Compressed | NO | |
| Data Set Type | Sorted | NO | |
| Label | |||
| Data Representation | SOLARIS_X86_64, LINUX_X86_64, ALPHA_TRU64, LINUX_IA64 | ||
| Encoding | utf-8 Unicode (UTF-8) |
| Engine/Host Dependent Information | |
|---|---|
| Data Set Page Size | 131072 |
| Number of Data Set Pages | 1 |
| First Data Page | 1 |
| Max Obs per Page | 1168 |
| Obs in First Data Page | 12 |
| Number of Data Set Repairs | 0 |
| Filename | /saswork/SAS_workEB80000016D2_odaws01-apse1-2.oda.sas.com/SAS_work1778000016D2_odaws01-apse1-2.oda.sas.com/books.sas7bdat |
| Release Created | 9.0401M8 |
| Host Created | Linux |
| Inode Number | 67123537 |
| Access Permission | rw-r--r-- |
| Owner Name | u63247146 |
| File Size | 256KB |
| File Size (bytes) | 262144 |
| Alphabetic List of Variables and Attributes | ||||
|---|---|---|---|---|
| # | Variable | Type | Len | Format |
| 2 | Author | Char | 25 | |
| 1 | Book_Name | Char | 30 | |
| 3 | Genre | Char | 15 | |
| 5 | Pages | Num | 8 | |
| 7 | Rating | Num | 8 | |
| 4 | Release_Date | Num | 8 | DATE9. |
| 8 | Release_Year | Num | 8 | |
| 6 | Sales | Num | 8 | |
PROC PRINT
proc print data=books(obs=5);
title "Sample Records from Books Dataset";
run;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year |
|---|---|---|---|---|---|---|---|---|
| 1 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 |
| 2 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 |
| 3 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 |
| 4 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 |
| 5 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 |
3.PROC SQL – BOOK SALES & RATING ANALYSIS
3.1 Average Sales and Ratings by Genre
proc sql;
create table genre_summary as
select Genre,
count(*) as Book_Count,
mean(Sales) as Avg_Sales format=8.2,
mean(Rating) as Avg_Rating format=5.2
from books
group by Genre;
quit;
proc print data=genre_summary;
run;
OUTPUT:
| Obs | Genre | Book_Count | Avg_Sales | Avg_Rating |
|---|---|---|---|---|
| 1 | Fantasy | 3 | 115.00 | 9.33 |
| 2 | Fiction | 1 | 65.00 | 8.60 |
| 3 | Mystery | 1 | 70.00 | 9.00 |
| 4 | Romance | 2 | 75.00 | 7.60 |
| 5 | SciFi | 2 | 76.50 | 8.60 |
| 6 | SelfHelp | 1 | 95.00 | 8.80 |
| 7 | Thriller | 2 | 67.50 | 8.15 |
3.2 Top-Selling Books
proc sql;
select Book_Name, Author, Sales
from books
where Sales > 90
order by Sales desc;
quit;
OUTPUT:
| Book_Name | Author | Sales |
|---|---|---|
| LOTR | Tolkien | 150 |
| HarryPotter | Rowling | 120 |
| AtomicHabits | Clear | 95 |
4.PROC MEANS – DESCRIPTIVE STATISTICS
proc means data=books n min max mean std;
var Pages Sales Rating;
title "Descriptive Statistics for Books Dataset";
run;
OUTPUT:
The MEANS Procedure
| Variable | N | Minimum | Maximum | Mean | Std Dev |
|---|---|---|---|---|---|
Pages Sales Rating | 12 12 12 | 208.0000000 55.0000000 7.2000000 | 1178.00 150.0000000 9.5000000 | 522.8333333 84.4166667 8.5916667 | 281.7303138 27.1944457 0.6881574 |
5.PROC RANK – POPULARITY RANKING
Ranking Books by Sales
proc rank data=books out=books_ranked descending;
var Sales;
ranks Sales_Rank;
run;
proc print data=books_ranked;
run;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year | Sales_Rank |
|---|---|---|---|---|---|---|---|---|---|
| 1 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 | 2 |
| 2 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 | 1 |
| 3 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 | 10 |
| 4 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 | 6 |
| 5 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 | 4 |
| 6 | HungerGames | Collins | SciFi | 15JUN2008 | 374 | 85 | 8.1 | 2008 | 5 |
| 7 | Sherlock | Holmes | Mystery | 15JUN1892 | 221 | 70 | 9.0 | 1892 | 8 |
| 8 | GameOfThrones | Martin | Fantasy | 15JUN1996 | 835 | 75 | 9.3 | 1996 | 7 |
| 9 | FaultInStars | Green | Romance | 15JUN2012 | 313 | 60 | 8.0 | 2012 | 11 |
| 10 | Inferno | Brown | Thriller | 15JUN2013 | 480 | 55 | 7.9 | 2013 | 12 |
| 11 | Dune | Herbert | SciFi | 15JUN1965 | 658 | 68 | 9.1 | 1965 | 9 |
| 12 | AtomicHabits | Clear | SelfHelp | 15JUN2018 | 320 | 95 | 8.8 | 2018 | 3 |
6.PROC FREQ – CATEGORICAL ANALYSIS
Genre Distribution
proc freq data=books;
tables Genre;
title "Distribution of Books by Genre";
run;
OUTPUT:
The FREQ Procedure
| Genre | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| Fantasy | 3 | 25.00 | 3 | 25.00 |
| Fiction | 1 | 8.33 | 4 | 33.33 |
| Mystery | 1 | 8.33 | 5 | 41.67 |
| Romance | 2 | 16.67 | 7 | 58.33 |
| SciFi | 2 | 16.67 | 9 | 75.00 |
| SelfHelp | 1 | 8.33 | 10 | 83.33 |
| Thriller | 2 | 16.67 | 12 | 100.00 |
Rating Categories (Using FORMAT)
proc format;
value ratinggrp
low-<7 = 'Low'
7-<8.5 = 'Medium'
8.5-high = 'High';
run;
LOG:
proc freq data=books;
tables Rating;
format Rating ratinggrp.;
run;
OUTPUT:
The FREQ Procedure
| Rating | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| Medium | 5 | 41.67 | 5 | 41.67 |
| High | 7 | 58.33 | 12 | 100.00 |
7.DATE FUNCTIONS – INTNX & INTCK
7.1 Calculate Book Age in Years
data books_dates;
set books;
Book_Age_Years = intck('year', Release_Date, today());
run;
proc print data=books_dates;
run;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year | Book_Age_Years |
|---|---|---|---|---|---|---|---|---|---|
| 1 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 | 28 |
| 2 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 | 71 |
| 3 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 | 37 |
| 4 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 | 22 |
| 5 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 | 20 |
| 6 | HungerGames | Collins | SciFi | 15JUN2008 | 374 | 85 | 8.1 | 2008 | 17 |
| 7 | Sherlock | Holmes | Mystery | 15JUN1892 | 221 | 70 | 9.0 | 1892 | 133 |
| 8 | GameOfThrones | Martin | Fantasy | 15JUN1996 | 835 | 75 | 9.3 | 1996 | 29 |
| 9 | FaultInStars | Green | Romance | 15JUN2012 | 313 | 60 | 8.0 | 2012 | 13 |
| 10 | Inferno | Brown | Thriller | 15JUN2013 | 480 | 55 | 7.9 | 2013 | 12 |
| 11 | Dune | Herbert | SciFi | 15JUN1965 | 658 | 68 | 9.1 | 1965 | 60 |
| 12 | AtomicHabits | Clear | SelfHelp | 15JUN2018 | 320 | 95 | 8.8 | 2018 | 7 |
7.2 Future Anniversary Date
data books_anniv;
set books;
Next_Anniversary = intnx('year', Release_Date, 50, 'same');
format Next_Anniversary date9.;
run;
proc print data=books_anniv;
run;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year | Next_Anniversary |
|---|---|---|---|---|---|---|---|---|---|
| 1 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 | 15JUN2047 |
| 2 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 | 15JUN2004 |
| 3 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 | 15JUN2038 |
| 4 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 | 15JUN2053 |
| 5 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 | 15JUN2055 |
| 6 | HungerGames | Collins | SciFi | 15JUN2008 | 374 | 85 | 8.1 | 2008 | 15JUN2058 |
| 7 | Sherlock | Holmes | Mystery | 15JUN1892 | 221 | 70 | 9.0 | 1892 | 15JUN1942 |
| 8 | GameOfThrones | Martin | Fantasy | 15JUN1996 | 835 | 75 | 9.3 | 1996 | 15JUN2046 |
| 9 | FaultInStars | Green | Romance | 15JUN2012 | 313 | 60 | 8.0 | 2012 | 15JUN2062 |
| 10 | Inferno | Brown | Thriller | 15JUN2013 | 480 | 55 | 7.9 | 2013 | 15JUN2063 |
| 11 | Dune | Herbert | SciFi | 15JUN1965 | 658 | 68 | 9.1 | 1965 | 15JUN2015 |
| 12 | AtomicHabits | Clear | SelfHelp | 15JUN2018 | 320 | 95 | 8.8 | 2018 | 15JUN2068 |
8.PROC SGPLOT – VISUAL ANALYSIS
Sales vs Rating Scatter Plot
proc sgplot data=books;
scatter x=Sales y=Rating / datalabel=Book_Name;
title "Sales vs Rating for Popular Books";
run;
OUTPUT:
Average Sales by Genre (Bar Chart)
proc sgplot data=genre_summary;
vbar Genre / response=Avg_Sales datalabel;
title "Average Sales by Genre";
run;
OUTPUT:
9.MACRO – POPULARITY CLASSIFICATION
Macro Definition
%macro popularity;
data books_popularity;
set books;
length Popularity $12;
if Sales >= 90 then Popularity='Blockbuster';
else if Sales >= 70 then Popularity='Popular';
else Popularity='Average';
run;
proc print data=books_popularity;
run;
%mend popularity;
%popularity;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year | Popularity |
|---|---|---|---|---|---|---|---|---|---|
| 1 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 | Blockbuster |
| 2 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 | Blockbuster |
| 3 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 | Average |
| 4 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 | Popular |
| 5 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 | Blockbuster |
| 6 | HungerGames | Collins | SciFi | 15JUN2008 | 374 | 85 | 8.1 | 2008 | Popular |
| 7 | Sherlock | Holmes | Mystery | 15JUN1892 | 221 | 70 | 9.0 | 1892 | Popular |
| 8 | GameOfThrones | Martin | Fantasy | 15JUN1996 | 835 | 75 | 9.3 | 1996 | Popular |
| 9 | FaultInStars | Green | Romance | 15JUN2012 | 313 | 60 | 8.0 | 2012 | Average |
| 10 | Inferno | Brown | Thriller | 15JUN2013 | 480 | 55 | 7.9 | 2013 | Average |
| 11 | Dune | Herbert | SciFi | 15JUN1965 | 658 | 68 | 9.1 | 1965 | Average |
| 12 | AtomicHabits | Clear | SelfHelp | 15JUN2018 | 320 | 95 | 8.8 | 2018 | Blockbuster |
10.SORTING STATEMENT
SORTING
proc sort data=books out=sortedbooks;
by descending Rating;
run;
proc print data=sortedbooks;
run;
OUTPUT:
| Obs | Book_Name | Author | Genre | Release_Date | Pages | Sales | Rating | Release_Year |
|---|---|---|---|---|---|---|---|---|
| 1 | LOTR | Tolkien | Fantasy | 15JUN1954 | 1178 | 150 | 9.5 | 1954 |
| 2 | GameOfThrones | Martin | Fantasy | 15JUN1996 | 835 | 75 | 9.3 | 1996 |
| 3 | HarryPotter | Rowling | Fantasy | 15JUN1997 | 500 | 120 | 9.2 | 1997 |
| 4 | Dune | Herbert | SciFi | 15JUN1965 | 658 | 68 | 9.1 | 1965 |
| 5 | Sherlock | Holmes | Mystery | 15JUN1892 | 221 | 70 | 9.0 | 1892 |
| 6 | AtomicHabits | Clear | SelfHelp | 15JUN2018 | 320 | 95 | 8.8 | 2018 |
| 7 | Alchemist | Coelho | Fiction | 15JUN1988 | 208 | 65 | 8.6 | 1988 |
| 8 | DaVinciCode | Brown | Thriller | 15JUN2003 | 689 | 80 | 8.4 | 2003 |
| 9 | HungerGames | Collins | SciFi | 15JUN2008 | 374 | 85 | 8.1 | 2008 |
| 10 | FaultInStars | Green | Romance | 15JUN2012 | 313 | 60 | 8.0 | 2012 |
| 11 | Inferno | Brown | Thriller | 15JUN2013 | 480 | 55 | 7.9 | 2013 |
| 12 | Twilight | Meyer | Romance | 15JUN2005 | 498 | 90 | 7.2 | 2005 |
No comments:
Post a Comment