175.TOYS DATASET ANALYSIS USING PROC PRINT | PROC CONTENTS | PROC FREQ | PROC MEANS | PROC SORT | PROC SQL | PROC EXPORT | PROC SGPLOT | MACROS

TOYS DATASET ANALYSIS USING PROC PRINT | PROC CONTENTS | PROC FREQ | PROC MEANS | PROC SORT | PROC SQL | PROC EXPORT | PROC SGPLOT | MACROS

 /*Creation of a unique toy dataset, application of various SAS procedures*/

/*Creating a Unique Toy Dataset*/

data toys_dataset;

    length ToyID $5 ToyName $30 Category $15 Country $15;

    input ToyID $ ToyName $ Category $ Price UnitsSold ManufactureDate :date9. Country $;

    format ManufactureDate date9.;

    datalines;

T001 TeddyBear Plush 15.99 120 01JAN2021 USA

T002 RaceCar Vehicle 9.99 200 15FEB2021 China

T003 Dollhouse Doll 49.99 75 10MAR2021 Germany

T004 PuzzleGame Puzzle 12.49 150 20APR2021 USA

T005 ActionFigure Action 19.99 180 05MAY2021 Japan

T006 BuildingBlocks Blocks 29.99 130 12JUN2021 Denmark

T007 RemoteDrone Drone 89.99 60 18JUL2021 China

T008 ColoringSet Art 8.99 210 25AUG2021 USA

T009 BoardGame Game 24.99 90 30SEP2021 Germany

T010 ScienceKit Educational 34.99 110 10OCT2021 USA

;

run;

proc print data=toys_dataset;

run;

Output:

Obs ToyID ToyName Category Country Price UnitsSold ManufactureDate
1 T001 TeddyBear Plush USA 15.99 120 01JAN2021
2 T002 RaceCar Vehicle China 9.99 200 15FEB2021
3 T003 Dollhouse Doll Germany 49.99 75 10MAR2021
4 T004 PuzzleGame Puzzle USA 12.49 150 20APR2021
5 T005 ActionFigure Action Japan 19.99 180 05MAY2021
6 T006 BuildingBlocks Blocks Denmark 29.99 130 12JUN2021
7 T007 RemoteDrone Drone China 89.99 60 18JUL2021
8 T008 ColoringSet Art USA 8.99 210 25AUG2021
9 T009 BoardGame Game Germany 24.99 90 30SEP2021
10 T010 ScienceKit Educational USA 34.99 110 10OCT2021


/*Exploring the Dataset*/

proc contents data=toys_dataset;

    title "Dataset Structure";

run;

Output:

                                                                   Dataset Structure 

                                                          The CONTENTS Procedure

Data Set Name WORK.TOYS_DATASET Observations 10
Member Type DATA Variables 7
Engine V9 Indexes 0
Created 14/09/2015 00:19:19 Observation Length 96
Last Modified 14/09/2015 00:19:19 Deleted Observations 0
Protection   Compressed NO
Data Set Type   Sorted NO
Label      
Data Representation WINDOWS_64    
Encoding wlatin1 Western (Windows)    


Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 681
Obs in First Data Page 10
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Users\Lenovo\AppData\Local\Temp\SAS Temporary Files\_TD10280_DESKTOP-QFAA4KV_\toys_dataset.sas7bdat
Release Created 9.0401M2
Host Created X64_8HOME


Alphabetic List of Variables and Attributes
# Variable Type Len Format
3 Category Char 15  
4 Country Char 15  
7 ManufactureDate Num 8 DATE9.
5 Price Num 8  
1 ToyID Char 5  
2 ToyName Char 30  
6 UnitsSold Num 8  


/*Frequency Analysis*/

proc freq data=toys_dataset;

    tables Category / nocum nopercent;

    title "Frequency of Toys by Category";

run;

Output:

                                                        Frequency of Toys by Category 

                                                                The FREQ Procedure

Category Frequency
Action 1
Art 1
Blocks 1
Doll 1
Drone 1
Educational 1
Game 1
Plush 1
Puzzle 1
Vehicle 1


/*Descriptive Statistics*/

proc means data=toys_dataset mean median min max std;

    var Price UnitsSold;

    title "Descriptive Statistics for Price and Units Sold";

run;

Output:

                                          Descriptive Statistics for Price and Units Sold

                                                          The MEANS Procedure

Variable Mean Median Minimum Maximum Std Dev
Price
UnitsSold
29.7400000
132.5000000
22.4900000
125.0000000
8.9900000
60.0000000
89.9900000
210.0000000
24.7040820
51.8143052


/*Sorting the Dataset*/

proc sort data=toys_dataset out=sorted_toys;

    by descending Price;

run;


proc print data=sorted_toys;

    title "Toys Sorted by Price (Descending)";

run;

Output:

                                                       Toys Sorted by Price (Descending)
 
Obs ToyID ToyName Category Country Price UnitsSold ManufactureDate
1 T007 RemoteDrone Drone China 89.99 60 18JUL2021
2 T003 Dollhouse Doll Germany 49.99 75 10MAR2021
3 T010 ScienceKit Educational USA 34.99 110 10OCT2021
4 T006 BuildingBlocks Blocks Denmark 29.99 130 12JUN2021
5 T009 BoardGame Game Germany 24.99 90 30SEP2021
6 T005 ActionFigure Action Japan 19.99 180 05MAY2021
7 T001 TeddyBear Plush USA 15.99 120 01JAN2021
8 T004 PuzzleGame Puzzle USA 12.49 150 20APR2021
9 T002 RaceCar Vehicle China 9.99 200 15FEB2021
10 T008 ColoringSet Art USA 8.99 210 25AUG2021


/*Creating a Summary Table*/

proc sql;

    create table category_revenue as

    select Category,

           sum(Price * UnitsSold) as TotalRevenue format=dollar12.2

    from toys_dataset

    group by Category;

quit;


proc print data=category_revenue;

    title "Total Revenue per Category";

run;

Output:

                                                            Total Revenue per Category 

Obs Category TotalRevenue
1 Action $3,598.20
2 Art $1,887.90
3 Blocks $3,898.70
4 Doll $3,749.25
5 Drone $5,399.40
6 Educational $3,848.90
7 Game $2,249.10
8 Plush $1,918.80
9 Puzzle $1,873.50
10 Vehicle $1,998.00


/*Identifying Top-Selling Toys*/

proc sql outobs=3;

    select ToyName, UnitsSold

    from toys_dataset

    order by UnitsSold desc;

quit;

Output:

ToyName UnitsSold
ColoringSet 210
RaceCar 200
ActionFigure 180


/*Joining Datasets*/

data manufacturer_info;

    input Country $ Manufacturer $;

    datalines;

USA Hasbro

China Mattel

Germany Playmobil

Japan Bandai

Denmark Lego

;

run;

proc print;run;

Output:

Obs Country Manufacturer
1 USA Hasbro
2 China Mattel
3 Germany Playmobi
4 Japan Bandai
5 Denmark Lego

proc sql;

    create table toys_with_manufacturer as

    select a.*, b.Manufacturer

    from toys_dataset as a

    left join manufacturer_info as b

    on a.Country = b.Country;

quit;


proc print data=toys_with_manufacturer;

    title "Toys with Manufacturer Information";

run;

Output:

                                                                 Toys with Manufacturer Information
 
Obs ToyID ToyName Category Country Price UnitsSold ManufactureDate Manufacturer
1 T007 RemoteDrone Drone China 89.99 60 18JUL2021 Mattel
2 T002 RaceCar Vehicle China 9.99 200 15FEB2021 Mattel
3 T006 BuildingBlocks Blocks Denmark 29.99 130 12JUN2021 Lego
4 T003 Dollhouse Doll Germany 49.99 75 10MAR2021 Playmobi
5 T009 BoardGame Game Germany 24.99 90 30SEP2021 Playmobi
6 T005 ActionFigure Action Japan 19.99 180 05MAY2021 Bandai
7 T010 ScienceKit Educational USA 34.99 110 10OCT2021 Hasbro
8 T001 TeddyBear Plush USA 15.99 120 01JAN2021 Hasbro
9 T004 PuzzleGame Puzzle USA 12.49 150 20APR2021 Hasbro
10 T008 ColoringSet Art USA 8.99 210 25AUG2021 Hasbro


/*Defining a Macro to Generate Category Reports*/

%macro category_report(cat);

    proc sql;

        create table &cat._report as

        select ToyName, Price, UnitsSold, (Price * UnitsSold) as Revenue format=dollar12.2

        from toys_dataset

        where Category = "&cat";

    quit;


    proc print data=&cat._report;

        title "Report for Category: &cat";

    run;

%mend category_report;

%category_report(Plush);

Output:

                                                              Report for Category: Plush

Obs ToyName Price UnitsSold Revenue
1 TeddyBear 15.99 120 $1,918.80

%category_report(Vehicle);

Output:

                                                        Report for Category: Vehicle
 
Obs ToyName Price UnitsSold Revenue
1 RaceCar 9.99 200 $1,998.00


%category_report(Doll);

Output:

                                                              Report for Category: Doll

Obs ToyName Price UnitsSold Revenue
1 Dollhouse 49.99 75 $3,749.25


/*Advanced Macro: Automating Reports for All Categories*/

proc sql noprint;

    select distinct Category into :cat1-:cat999

    from toys_dataset;

    %let cat_count = &sqlobs;

quit;


%macro all_category_reports;

    %do i = 1 %to &cat_count;

        %let current_cat = &&cat&i;

        %category_report(&current_cat);

    %end;

%mend all_category_reports;


%all_category_reports;

Output:

                                                          Report for Category: Action

Obs ToyName Price UnitsSold Revenue
1 ActionFigure 19.99 180 $3,598.20

 
                                                         Report for Category: Art

Obs ToyName Price UnitsSold Revenue
1 ColoringSet 8.99 210 $1,887.90

 
                                                         Report for Category: Blocks

Obs ToyName Price UnitsSold Revenue
1 BuildingBlocks 29.99 130 $3,898.70

 
                                                     Report for Category: Doll

Obs ToyName Price UnitsSold Revenue
1 Dollhouse 49.99 75 $3,749.25


   
                                                       Report for Category: Drone

Obs ToyName Price UnitsSold Revenue
1 RemoteDrone 89.99 60 $5,399.40


 
                                                     Report for Category: Educational

Obs ToyName Price UnitsSold Revenue
1 ScienceKit 34.99 110 $3,848.90


 
                                                       Report for Category: Game

Obs ToyName Price UnitsSold Revenue
1 BoardGame 24.99 90 $2,249.10

 
                                                      Report for Category: Plush

Obs ToyName Price UnitsSold Revenue
1 TeddyBear 15.99 120 $1,918.80


 
                                                      Report for Category: Puzzle

Obs ToyName Price UnitsSold Revenue
1 PuzzleGame 12.49 150 $1,873.50

 
                                                       Report for Category: Vehicle

Obs ToyName Price UnitsSold Revenue
1 RaceCar 9.99 200 $1,998.00

/*Bar Chart of Units Sold by Category*/

proc sgplot data=toys_dataset;

    vbar Category / response=UnitsSold stat=sum;

    title "Total Units Sold by Category";

run;

Log:

NOTE: PROCEDURE SGPLOT used (Total process time):

      real time           2.56 seconds

      cpu time            0.50 seconds


NOTE: Listing image output written to SGPlot1.png.

NOTE: There were 10 observations read from the data set WORK.TOYS_DATASET.


/*Scatter Plot of Price vs. Units Sold*/

proc sgplot data=toys_dataset;

    scatter x=Price y=UnitsSold / datalabel=ToyName;

    title "Price vs. Units Sold";

run;

Log:

NOTE: PROCEDURE SGPLOT used (Total process time):

      real time           0.53 seconds

      cpu time            0.07 seconds


NOTE: Listing image output written to SGPlot3.png.

NOTE: There were 10 observations read from the data set WORK.TOYS_DATASET.


/*Exporting Results*/

proc export data=category_revenue

    outfile="C:\SAS\category_revenue.csv"

    dbms=csv

    replace;

run;



Find A Mistake and Comment It:

proc sql noprint;

    select distinct Category into :cat1- cat999

    from toys_dataset;

    %let cat_count = &sqlobs;

quit;



PRACTICE AND COMMENT YOUR CODE: 

--->PLEASE FOLLOW OUR BLOG FOR MORE UPDATES.

PLEASE FOLLOW OUR TELEGRAM CHANNEL CLICK HERE

PLEASE FOLLOW OUR FACEBOOK PAGE  CLICK HERE

Comments

Popular posts from this blog

397.If a satellite has excellent signal strength but very high latency, can it still deliver good quality communication? Why or why not?A Sas Study

401.How Efficient Are Global Data Centers? A Complete SAS Analytics Study

383.Which Pharma Distributors Are High-Risk? Can SAS Detect Delays, Temperature Violations, and Fraud?