223.ANALYZING NON-VEGETARIAN FOOD ITEMS IN THE USA USING PROCS | SQL | MACROS | OPTIONS AND FUNCTIONS IN SAS

ANALYZING NON-VEGETARIAN FOOD ITEMS IN THE USA USING PROCS | SQL | MACROS | OPTIONS AND FUNCTIONS IN SAS

/*Creating a dataset of non-vegetarian food items popular in different regions of the United States*/

Step 1: Set OPTIONS

options nodate nonumber ps=60 ls=80 nocenter;


Step 2: Create Dataset of Non-Veg Items

data NonVeg_USA;

  infile datalines dlm='|' dsd truncover;

  input Item_ID Item_Name : $30. Category : $15. Region : $15. Price_USD Calories

        Rating Availability : $10. Restaurant : $30.;

  datalines;

1|Grilled Chicken Sandwich|Chicken|South|7.99|500|4.3|Available|Chick-fil-A

2|Beef Burger|Beef|Midwest|8.49|650|4.6|Available|McDonald's

3|Lobster Roll|Seafood|Northeast|15.99|400|4.7|Limited|Red Lobster

4|BBQ Ribs|Pork|South|12.99|800|4.5|Available|Texas Roadhouse

5|Fried Catfish|Seafood|South|10.99|600|4.2|Available|Cracker Barrel

6|Buffalo Wings|Chicken|Midwest|9.49|700|4.4|Available|Buffalo Wild Wings

7|Turkey Sub|Turkey|East|6.99|550|4.1|Available|Subway

8|Clam Chowder|Seafood|Northeast|5.99|300|4.0|Limited|Legal Sea Foods

9|Sushi Roll|Seafood|West|13.99|450|4.8|Available|Sakura Sushi

10|Meat Lover's Pizza|Mixed|Midwest|11.99|900|4.6|Available|Pizza Hut

11|Roast Beef Sandwich|Beef|West|7.49|520|4.3|Available|Arby’s

12|Shrimp Tacos|Seafood|Southwest|8.99|430|4.5|Available|Taco Bell

13|Chicken Alfredo|Chicken|Northeast|12.49|650|4.4|Available|Olive Garden

14|Spicy Lamb Gyro|Lamb|East|9.99|580|4.2|Available|Gyro World

15|Bacon Cheeseburger|Pork|South|9.99|720|4.6|Available|Five Guys

16|Crab Cakes|Seafood|East|14.49|490|4.7|Limited|Joe’s Crab Shack

17|Duck Confit|Duck|West|18.99|620|4.9|Limited|Gourmet Bistro

;

run;

proc print;run;

Output:

Obs Item_ID Item_Name Category Region Price_USD Calories Rating Availability Restaurant
1 1 Grilled Chicken Sandwich Chicken South 7.99 500 4.3 Available Chick-fil-A
2 2 Beef Burger Beef Midwest 8.49 650 4.6 Available McDonald's
3 3 Lobster Roll Seafood Northeast 15.99 400 4.7 Limited Red Lobster
4 4 BBQ Ribs Pork South 12.99 800 4.5 Available Texas Roadhouse
5 5 Fried Catfish Seafood South 10.99 600 4.2 Available Cracker Barrel
6 6 Buffalo Wings Chicken Midwest 9.49 700 4.4 Available Buffalo Wild Wings
7 7 Turkey Sub Turkey East 6.99 550 4.1 Available Subway
8 8 Clam Chowder Seafood Northeast 5.99 300 4.0 Limited Legal Sea Foods
9 9 Sushi Roll Seafood West 13.99 450 4.8 Available Sakura Sushi
10 10 Meat Lover's Pizza Mixed Midwest 11.99 900 4.6 Available Pizza Hut
11 11 Roast Beef Sandwich Beef West 7.49 520 4.3 Available Arby’s
12 12 Shrimp Tacos Seafood Southwest 8.99 430 4.5 Available Taco Bell
13 13 Chicken Alfredo Chicken Northeast 12.49 650 4.4 Available Olive Garden
14 14 Spicy Lamb Gyro Lamb East 9.99 580 4.2 Available Gyro World
15 15 Bacon Cheeseburger Pork South 9.99 720 4.6 Available Five Guys
16 16 Crab Cakes Seafood East 14.49 490 4.7 Limited Joe’s Crab Shack
17 17 Duck Confit Duck West 18.99 620 4.9 Limited Gourmet Bistro


Step 3: PROC PRINT with Formatted Output

proc print data=NonVeg_USA label noobs;

  label Item_Name='Item'

        Price_USD='Price ($)'

        Rating='Popularity';

  var Item_ID Item_Name Category Region Price_USD Rating;

run;

Output:

Item_ID Item Category Region Price ($) Popularity
1 Grilled Chicken Sandwich Chicken South 7.99 4.3
2 Beef Burger Beef Midwest 8.49 4.6
3 Lobster Roll Seafood Northeast 15.99 4.7
4 BBQ Ribs Pork South 12.99 4.5
5 Fried Catfish Seafood South 10.99 4.2
6 Buffalo Wings Chicken Midwest 9.49 4.4
7 Turkey Sub Turkey East 6.99 4.1
8 Clam Chowder Seafood Northeast 5.99 4.0
9 Sushi Roll Seafood West 13.99 4.8
10 Meat Lover's Pizza Mixed Midwest 11.99 4.6
11 Roast Beef Sandwich Beef West 7.49 4.3
12 Shrimp Tacos Seafood Southwest 8.99 4.5
13 Chicken Alfredo Chicken Northeast 12.49 4.4
14 Spicy Lamb Gyro Lamb East 9.99 4.2
15 Bacon Cheeseburger Pork South 9.99 4.6
16 Crab Cakes Seafood East 14.49 4.7
17 Duck Confit Duck West 18.99 4.9


Step 4: PROC FORMAT for Readable Outputs

proc format;

  value $catfmt

    'Chicken'='Chicken-based'

    'Beef'='Beef-based'

    'Seafood'='Seafood-based'

    'Pork'='Pork-based'

    'Mixed'='Multiple meats'

    'Turkey'='Turkey-based'

    'Lamb'='Lamb-based'

    'Duck'='Duck-based';


  value $availfmt

    'Available'='In Stock'

    'Limited'='Limited';

run;


proc print data=NonVeg_USA;

  format Category $catfmt. Availability availfmt.;

run;

Output:

Obs Item_ID Item_Name Category Region Price_USD Calories Rating Availability Restaurant
1 1 Grilled Chicken Sandwich Chicken-based South 7.99 500 4.3 In Stock Chick-fil-A
2 2 Beef Burger Beef-based Midwest 8.49 650 4.6 In Stock McDonald's
3 3 Lobster Roll Seafood-based Northeast 15.99 400 4.7 Limited Red Lobster
4 4 BBQ Ribs Pork-based South 12.99 800 4.5 In Stock Texas Roadhouse
5 5 Fried Catfish Seafood-based South 10.99 600 4.2 In Stock Cracker Barrel
6 6 Buffalo Wings Chicken-based Midwest 9.49 700 4.4 In Stock Buffalo Wild Wings
7 7 Turkey Sub Turkey-based East 6.99 550 4.1 In Stock Subway
8 8 Clam Chowder Seafood-based Northeast 5.99 300 4.0 Limited Legal Sea Foods
9 9 Sushi Roll Seafood-based West 13.99 450 4.8 In Stock Sakura Sushi
10 10 Meat Lover's Pizza Multiple meats Midwest 11.99 900 4.6 In Stock Pizza Hut
11 11 Roast Beef Sandwich Beef-based West 7.49 520 4.3 In Stock Arby’s
12 12 Shrimp Tacos Seafood-based Southwest 8.99 430 4.5 In Stock Taco Bell
13 13 Chicken Alfredo Chicken-based Northeast 12.49 650 4.4 In Stock Olive Garden
14 14 Spicy Lamb Gyro Lamb-based East 9.99 580 4.2 In Stock Gyro World
15 15 Bacon Cheeseburger Pork-based South 9.99 720 4.6 In Stock Five Guys
16 16 Crab Cakes Seafood-based East 14.49 490 4.7 Limited Joe’s Crab Shack
17 17 Duck Confit Duck-based West 18.99 620 4.9 Limited Gourmet Bistro


Step 5: PROC MEANS for Price and Calorie Summary

proc means data=NonVeg_USA mean min max std maxdec=2;

  class Category;

  var Price_USD Calories;

run;

Output:

The MEANS Procedure

Category N Obs Variable Mean Minimum Maximum Std Dev
Beef 2
Price_USD
Calories
7.99
585.00
7.49
520.00
8.49
650.00
0.71
91.92
Chicken 3
Price_USD
Calories
9.99
616.67
7.99
500.00
12.49
700.00
2.29
104.08
Duck 1
Price_USD
Calories
18.99
620.00
18.99
620.00
18.99
620.00
.
.
Lamb 1
Price_USD
Calories
9.99
580.00
9.99
580.00
9.99
580.00
.
.
Mixed 1
Price_USD
Calories
11.99
900.00
11.99
900.00
11.99
900.00
.
.
Pork 2
Price_USD
Calories
11.49
760.00
9.99
720.00
12.99
800.00
2.12
56.57
Seafood 6
Price_USD
Calories
11.74
445.00
5.99
300.00
15.99
600.00
3.79
99.35
Turkey 1
Price_USD
Calories
6.99
550.00
6.99
550.00
6.99
550.00
.
.


Step 6: PROC FREQ for Region and Category Distributions

proc freq data=NonVeg_USA;

  tables Region*Category / nocum nocol nopercent;

run;

Output:

The FREQ Procedure

Frequency
Row Pct
Table of Region by Category
Region Category
Beef Chicken Duck Lamb Mixed Pork Seafood Turkey Total
East
0
0.00
0
0.00
0
0.00
1
33.33
0
0.00
0
0.00
1
33.33
1
33.33
3
 
Midwest
1
33.33
1
33.33
0
0.00
0
0.00
1
33.33
0
0.00
0
0.00
0
0.00
3
 
Northeast
0
0.00
1
33.33
0
0.00
0
0.00
0
0.00
0
0.00
2
66.67
0
0.00
3
 
South
0
0.00
1
25.00
0
0.00
0
0.00
0
0.00
2
50.00
1
25.00
0
0.00
4
 
Southwest
0
0.00
0
0.00
0
0.00
0
0.00
0
0.00
0
0.00
1
100.00
0
0.00
1
 
West
1
33.33
0
0.00
1
33.33
0
0.00
0
0.00
0
0.00
1
33.33
0
0.00
3
 
Total
2
3
1
1
1
2
6
1
17


Step 7: PROC SORT before reporting

proc sort data=NonVeg_USA out=Sorted_Items;

  by descending Rating;

run;

proc print;run;

Output:

Obs Item_ID Item_Name Category Region Price_USD Calories Rating Availability Restaurant
1 17 Duck Confit Duck West 18.99 620 4.9 Limited Gourmet Bistro
2 9 Sushi Roll Seafood West 13.99 450 4.8 Available Sakura Sushi
3 3 Lobster Roll Seafood Northeast 15.99 400 4.7 Limited Red Lobster
4 16 Crab Cakes Seafood East 14.49 490 4.7 Limited Joe’s Crab Shack
5 2 Beef Burger Beef Midwest 8.49 650 4.6 Available McDonald's
6 10 Meat Lover's Pizza Mixed Midwest 11.99 900 4.6 Available Pizza Hut
7 15 Bacon Cheeseburger Pork South 9.99 720 4.6 Available Five Guys
8 4 BBQ Ribs Pork South 12.99 800 4.5 Available Texas Roadhouse
9 12 Shrimp Tacos Seafood Southwest 8.99 430 4.5 Available Taco Bell
10 6 Buffalo Wings Chicken Midwest 9.49 700 4.4 Available Buffalo Wild Wings
11 13 Chicken Alfredo Chicken Northeast 12.49 650 4.4 Available Olive Garden
12 1 Grilled Chicken Sandwich Chicken South 7.99 500 4.3 Available Chick-fil-A
13 11 Roast Beef Sandwich Beef West 7.49 520 4.3 Available Arby’s
14 5 Fried Catfish Seafood South 10.99 600 4.2 Available Cracker Barrel
15 14 Spicy Lamb Gyro Lamb East 9.99 580 4.2 Available Gyro World
16 7 Turkey Sub Turkey East 6.99 550 4.1 Available Subway
17 8 Clam Chowder Seafood Northeast 5.99 300 4.0 Limited Legal Sea Foods


Step 8: PROC REPORT

proc report data=Sorted_Items nowd headline;

  column Item_Name Category Region Price_USD Rating;

  define Item_Name / display 'Item';

  define Category / group 'Type';

  define Region / group 'Region';

  define Price_USD / analysis mean format=8.2 'Avg Price ($)';

  define Rating / display 'Popularity';

run;

Output:

Item Type Region Avg Price ($) Popularity
Beef Burger Beef Midwest 8.49 4.6
Roast Beef Sandwich   West 7.49 4.3
Buffalo Wings Chicken Midwest 9.49 4.4
Chicken Alfredo   Northeast 12.49 4.4
Grilled Chicken Sandwich   South 7.99 4.3
Duck Confit Duck West 18.99 4.9
Spicy Lamb Gyro Lamb East 9.99 4.2
Meat Lover's Pizza Mixed Midwest 11.99 4.6
Bacon Cheeseburger Pork South 9.99 4.6
BBQ Ribs     12.99 4.5
Crab Cakes Seafood East 14.49 4.7
Lobster Roll   Northeast 15.99 4.7
Clam Chowder     5.99 4
Fried Catfish   South 10.99 4.2
Shrimp Tacos   Southwest 8.99 4.5
Sushi Roll   West 13.99 4.8
Turkey Sub Turkey East 6.99 4.1


Step 9: PROC SQL for Advanced Analysis

a) Average Price by Category

proc sql;

  select Category,

         count(*) as Total_Items,

         mean(Price_USD) as Avg_Price format=8.2

  from NonVeg_USA

  group by Category

  order by Avg_Price desc;

quit;

Output:

Category Total_Items Avg_Price
Duck 1 18.99
Mixed 1 11.99
Seafood 6 11.74
Pork 2 11.49
Chicken 3 9.99
Lamb 1 9.99
Beef 2 7.99
Turkey 1 6.99

b) Top 3 Popular Items

proc sql outobs=3;

  select Item_Name, Category, Rating

  from NonVeg_USA

  order by Rating desc;

quit;

Output:

Item_Name Category Rating
Duck Confit Duck 4.9
Sushi Roll Seafood 4.8
Crab Cakes Seafood 4.7


Step 10: MACRO to Filter by Category

%macro filter_by_category(cat);

  proc print data=NonVeg_USA;

    where upcase(Category) = "%upcase(&cat)";

    title "Items from Category: &cat";

  run;

%mend;


%filter_by_category(Seafood);

Output:

Items from Category: Seafood

Obs Item_ID Item_Name Category Region Price_USD Calories Rating Availability Restaurant
3 3 Lobster Roll Seafood Northeast 15.99 400 4.7 Limited Red Lobster
5 5 Fried Catfish Seafood South 10.99 600 4.2 Available Cracker Barrel
8 8 Clam Chowder Seafood Northeast 5.99 300 4.0 Limited Legal Sea Foods
9 9 Sushi Roll Seafood West 13.99 450 4.8 Available Sakura Sushi
12 12 Shrimp Tacos Seafood Southwest 8.99 430 4.5 Available Taco Bell
16 16 Crab Cakes Seafood East 14.49 490 4.7 Limited Joe’s Crab Shack

%filter_by_category(Chicken);

Output:

Items from Category: Chicken

Obs Item_ID Item_Name Category Region Price_USD Calories Rating Availability Restaurant
1 1 Grilled Chicken Sandwich Chicken South 7.99 500 4.3 Available Chick-fil-A
6 6 Buffalo Wings Chicken Midwest 9.49 700 4.4 Available Buffalo Wild Wings
13 13 Chicken Alfredo Chicken Northeast 12.49 650 4.4 Available Olive Garden


Step 11: Use of SAS FUNCTIONS

data Modified_NonVeg;

  set NonVeg_USA;

  Item_Code = catx("-", upcase(substr(Category,1,3)), Item_ID); /* Combining text */

  Rounded_Cals = round(Calories, 50); /* Rounding to nearest 50 */

  Price_Rating = round(Price_USD * Rating, 0.01);

run;


proc print data=Modified_NonVeg;

  var Item_Name Category Item_Code Rounded_Cals Price_Rating;

run;

Output:

Obs Item_Name Category Item_Code Rounded_Cals Price_Rating
1 Grilled Chicken Sandwich Chicken CHI-1 500 34.36
2 Beef Burger Beef BEE-2 650 39.05
3 Lobster Roll Seafood SEA-3 400 75.15
4 BBQ Ribs Pork POR-4 800 58.46
5 Fried Catfish Seafood SEA-5 600 46.16
6 Buffalo Wings Chicken CHI-6 700 41.76
7 Turkey Sub Turkey TUR-7 550 28.66
8 Clam Chowder Seafood SEA-8 300 23.96
9 Sushi Roll Seafood SEA-9 450 67.15
10 Meat Lover's Pizza Mixed MIX-10 900 55.15
11 Roast Beef Sandwich Beef BEE-11 500 32.21
12 Shrimp Tacos Seafood SEA-12 450 40.46
13 Chicken Alfredo Chicken CHI-13 650 54.96
14 Spicy Lamb Gyro Lamb LAM-14 600 41.96
15 Bacon Cheeseburger Pork POR-15 700 45.95
16 Crab Cakes Seafood SEA-16 500 68.10
17 Duck Confit Duck DUC-17 600 93.05


Step 12: PROC TRANSPOSE - Region-wise Item Count

proc sort data=NonVeg_USA; by Region; run;


proc transpose data=NonVeg_USA out=ItemCount prefix=Item_;

  by Region;

  id Category;

  var Item_ID;

run;


proc print data=ItemCount;

run;

Output:

Obs Region _NAME_ Item_Turkey Item_Lamb Item_Seafood Item_Beef Item_Chicken Item_Mixed Item_Pork Item_Duck
1 East Item_ID 7 14 16 . . . . .
2 Midwest Item_ID . . . 2 6 10 . .
3 Southwest Item_ID . . 12 . . . . .
4 West Item_ID . . 9 11 . . . 17


Step 13: PROC TABULATE - Multi-variable Summary

proc tabulate data=NonVeg_USA;

  class Region Category;

  var Price_USD Calories Rating;

  table Region, Category*(Price_USD Calories Rating)*(mean);

run;

Output:

Category
Beef Chicken Duck Lamb Mixed Pork Seafood Turkey
Price_USD Calories Rating Price_USD Calories Rating Price_USD Calories Rating Price_USD Calories Rating Price_USD Calories Rating Price_USD Calories Rating Price_USD Calories Rating Price_USD Calories Rating
Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean Mean
Region . . . . . . . . . 9.99 580.00 4.20 . . . . . . 14.49 490.00 4.70 6.99 550.00 4.10
East
Midwest 8.49 650.00 4.60 9.49 700.00 4.40 . . . . . . 11.99 900.00 4.60 . . . . . . . . .
Northeast . . . 12.49 650.00 4.40 . . . . . . . . . . . . 10.99 350.00 4.35 . . .
South . . . 7.99 500.00 4.30 . . . . . . . . . 11.49 760.00 4.55 10.99 600.00 4.20 . . .
Southwest . . . . . . . . . . . . . . . . . . 8.99 430.00 4.50 . . .
West 7.49 520.00 4.30 . . . 18.99 620.00 4.90 . . . . . . . . . 13.99 450.00 4.80 . . .


Step 14: PROC CHART - Category Frequency

proc chart data=NonVeg_USA;

  vbar Category / discrete;

run;

Log:

NOTE: Non-portable document will be produced. The current settings of FORMCHAR

      use nonstandard line-drawing characters and the resulting output file

      will not render correctly unless all readers of the document have the SAS

      Monospace font installed. To make your document portable, issue the

      following command:

      OPTIONS FORMCHAR="|----|+|---+=|-/\<>*";


NOTE: PROCEDURE CHART used (Total process time):

      real time           0.10 seconds

      cpu time            0.01 seconds


Step 15: Final Summary Report

proc sql;

  select Region, 

         count(*) as Total_Items,

         mean(Rating) as Avg_Rating format=4.2,

         mean(Price_USD) as Avg_Price format=6.2

  from NonVeg_USA

  group by Region;

quit;

Output:
Region Total_Items Avg_Rating Avg_Price
East 3 4.33 10.49
Midwest 3 4.53 9.99
Northeast 3 4.37 11.49
South 4 4.40 10.49
Southwest 1 4.50 8.99
West 3 4.67 13.49


To Visit My Previous Onilne Retail Dataset:Click Here
To Visit My Previous Assignment Statement:Click Here
To Visit My Previous Online Courses Dataset:Click Here
To Visit My Previous Advanced Clinical Trials:Click Here


--->PRACTICE AND COMMENT YOUR OUTPUT: 

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

PLEASE FOLLOW OUR TELEGRAM CHANNEL CLICK HERE

PLEASE FOLLOW OUR FACEBOOK PAGE  CLICK HERE

PLEASE FOLLOW OUR INSTAGRAM PAGE CLICK HERE

Comments