244.Analyzing Glass Composition with SAS: From Data Creation to Insightful Visualization Using DATA Step | PROC PRINT | PROC MEANS | PROC FREQ | PROC SGPLOT | PROC SQL | MACROS

Analyzing Glass Composition with SAS: From Data Creation to Insightful Visualization Using DATA Step | PROC PRINT | PROC MEANS | PROC FREQ | PROC SGPLOT | PROC SQL | MACROS

*/ Creating differnet types of glass dataset */

/* Step 1: Create a dataset with different types of glass and their chemical properties */

Options Nocenter;

data glass_data;

    input Glass_Type $&20. 

          RI Na Mg Al Si K Ca Ba Fe;

    datalines;

"Building Float"         1.521 13.64 4.49 1.10 71.78 0.06 8.75 0.00 0.00

"Building NonFloat"      1.517 13.89 3.60 1.36 72.73 0.48 7.83 0.00 0.00

"Container"              1.522 14.20 3.34 1.47 72.00 0.32 8.58 0.00 0.00

"Vehicle Float"          1.515 13.53 3.89 1.28 73.21 0.33 7.90 0.00 0.01

"Headlamp"               1.514 14.56 2.87 1.30 72.19 0.38 7.78 0.00 0.00

"Tableware"              1.517 12.79 3.87 1.41 73.12 0.24 9.04 0.00 0.00

"Building Float"         1.523 13.21 3.51 1.12 71.99 0.20 8.55 0.00 0.01

"Building NonFloat"      1.517 13.30 3.60 1.24 73.30 0.30 8.45 0.00 0.01

"Container"              1.519 14.00 3.20 1.60 72.10 0.40 8.90 0.00 0.00

"Vehicle Float"          1.520 13.45 3.55 1.30 72.90 0.31 8.20 0.00 0.02

"Headlamp"               1.514 14.70 3.10 1.15 72.80 0.45 7.90 0.00 0.00

"Tableware"              1.516 13.00 3.95 1.35 73.25 0.28 9.12 0.00 0.00

"Building NonFloat"      1.518 13.60 3.70 1.25 73.10 0.35 8.30 0.00 0.01

"Building Float"         1.522 13.40 3.80 1.10 72.00 0.22 8.70 0.00 0.00

"Container"              1.521 14.10 3.40 1.50 72.20 0.31 8.80 0.00 0.00

"Vehicle Float"          1.515 13.55 3.80 1.22 73.05 0.37 8.00 0.00 0.01

"Headlamp"               1.514 14.60 3.15 1.18 72.85 0.42 7.85 0.00 0.00

"Tableware"              1.517 12.85 3.90 1.42 73.00 0.26 9.00 0.00 0.00

"Building Float"         1.523 13.35 3.60 1.15 71.90 0.23 8.60 0.00 0.01

"Building NonFloat"      1.517 13.70 3.65 1.28 73.15 0.34 8.35 0.00 0.01

"Container"              1.519 14.30 3.30 1.55 72.00 0.39 8.95 0.00 0.00

"Vehicle Float"          1.516 13.60 3.85 1.26 73.10 0.36 8.10 0.00 0.01

"Headlamp"               1.514 14.65 3.05 1.20 72.90 0.43 7.88 0.00 0.00

"Tableware"              1.516 12.90 3.92 1.38 73.10 0.27 9.10 0.00 0.00

"Building NonFloat"      1.518 13.80 3.75 1.30 73.00 0.36 8.25 0.00 0.01

;

run;

proc print;run;

Output:

Obs

Glass_Type

RI

Na

Mg

Al

Si

K

Ca

Ba

Fe

1

"Building Float"

1.521

13.64

4.49

1.10

71.78

0.06

8.75

0

0.00

2

"Building NonFloat"

1.517

13.89

3.60

1.36

72.73

0.48

7.83

0

0.00

3

"Container"

1.522

14.20

3.34

1.47

72.00

0.32

8.58

0

0.00

4

"Vehicle Float"

1.515

13.53

3.89

1.28

73.21

0.33

7.90

0

0.01

5

"Headlamp"

1.514

14.56

2.87

1.30

72.19

0.38

7.78

0

0.00

6

"Tableware"

1.517

12.79

3.87

1.41

73.12

0.24

9.04

0

0.00

7

"Building Float"

1.523

13.21

3.51

1.12

71.99

0.20

8.55

0

0.01

8

"Building NonFloat"

1.517

13.30

3.60

1.24

73.30

0.30

8.45

0

0.01

9

"Container"

1.519

14.00

3.20

1.60

72.10

0.40

8.90

0

0.00

10

"Vehicle Float"

1.520

13.45

3.55

1.30

72.90

0.31

8.20

0

0.02

11

"Headlamp"

1.514

14.70

3.10

1.15

72.80

0.45

7.90

0

0.00

12

"Tableware"

1.516

13.00

3.95

1.35

73.25

0.28

9.12

0

0.00

13

"Building NonFloat"

1.518

13.60

3.70

1.25

73.10

0.35

8.30

0

0.01

14

"Building Float"

1.522

13.40

3.80

1.10

72.00

0.22

8.70

0

0.00

15

"Container"

1.521

14.10

3.40

1.50

72.20

0.31

8.80

0

0.00

16

"Vehicle Float"

1.515

13.55

3.80

1.22

73.05

0.37

8.00

0

0.01

17

"Headlamp"

1.514

14.60

3.15

1.18

72.85

0.42

7.85

0

0.00

18

"Tableware"

1.517

12.85

3.90

1.42

73.00

0.26

9.00

0

0.00

19

"Building Float"

1.523

13.35

3.60

1.15

71.90

0.23

8.60

0

0.01

20

"Building NonFloat"

1.517

13.70

3.65

1.28

73.15

0.34

8.35

0

0.01

21

"Container"

1.519

14.30

3.30

1.55

72.00

0.39

8.95

0

0.00

22

"Vehicle Float"

1.516

13.60

3.85

1.26

73.10

0.36

8.10

0

0.01

23

"Headlamp"

1.514

14.65

3.05

1.20

72.90

0.43

7.88

0

0.00

24

"Tableware"

1.516

12.90

3.92

1.38

73.10

0.27

9.10

0

0.00

25

"Building NonFloat"

1.518

13.80

3.75

1.30

73.00

0.36

8.25

0

0.01

/* Step 2: Print the dataset */

proc print data=glass_data(obs=10);

    title "Sample of Glass Dataset (First 10 Observations)";

run;

Output:

Sample of Glass Dataset (First 10 Observations)

ObsGlass_TypeRINaMgAlSiKCaBaFe
1"Building Float"1.52113.644.491.1071.780.068.7500.00
2"Building NonFloat"1.51713.893.601.3672.730.487.8300.00
3"Container"1.52214.203.341.4772.000.328.5800.00
4"Vehicle Float"1.51513.533.891.2873.210.337.9000.01
5"Headlamp"1.51414.562.871.3072.190.387.7800.00
6"Tableware"1.51712.793.871.4173.120.249.0400.00
7"Building Float"1.52313.213.511.1271.990.208.5500.01
8"Building NonFloat"1.51713.303.601.2473.300.308.4500.01
9"Container"1.51914.003.201.6072.100.408.9000.00
10"Vehicle Float"1.52013.453.551.3072.900.318.2000.02

/* Step 3: Get summary statistics */

proc means data=glass_data n mean std min max;

    var RI Na Mg Al Si K Ca Ba Fe;

    title "Summary Statistics of Glass Features";

run;

Output:

Summary Statistics of Glass Features

The MEANS Procedure

VariableNMeanStd DevMinimumMaximum
RI
Na
Mg
Al
Si
K
Ca
Ba
Fe
25
25
25
25
25
25
25
25
25
1.5178000
13.7068000
3.5936000
1.2988000
72.6688000
0.3224000
8.4352000
0
0.0044000
0.0029155
0.5696397
0.3591991
0.1380012
0.5205632
0.0915733
0.4507690
0
0.0058310
1.5140000
12.7900000
2.8700000
1.1000000
71.7800000
0.0600000
7.7800000
0
0
1.5230000
14.7000000
4.4900000
1.6000000
73.3000000
0.4800000
9.1200000
0
0.0200000

/* Step 4: Frequency distribution of Glass Types */

proc freq data=glass_data;

    tables Glass_Type / nocum nopercent;

    title "Frequency of Each Glass Type";

run;

Output:

Frequency of Each Glass Type

The FREQ Procedure

Glass_TypeFrequency
"Building Float"4
"Building NonFloat"5
"Container"4
"Headlamp"4
"Tableware"4
"Vehicle Float"4

/* Step 5: Visualize with boxplot using SGPLOT */

proc sgplot data=glass_data;

    vbox Na / category=Glass_Type;

    title "Boxplot of Sodium (Na) by Glass Type";

run;

Output:




/* Step 6: Use PROC SQL to perform complex filtering */

proc sql;

    create table High_Sodium as

    select * from glass_data

    where Na > 14

    order by Na desc;

    

    title "Glass Samples with Sodium (Na) Greater Than 14";

    select * from High_Sodium;

quit;

Output:

Glass Samples with Sodium (Na) Greater Than 14

Glass_TypeRINaMgAlSiKCaBaFe
"Headlamp"1.51414.73.11.1572.80.457.900
"Headlamp"1.51414.653.051.272.90.437.8800
"Headlamp"1.51414.63.151.1872.850.427.8500
"Headlamp"1.51414.562.871.372.190.387.7800
"Container"1.51914.33.31.55720.398.9500
"Container"1.52214.23.341.47720.328.5800
"Container"1.52114.13.41.572.20.318.800

/* Step 7: Use PROC SQL to calculate group means */

proc sql;

    create table GroupMeans as

    select Glass_Type,

           mean(Na) as Avg_Na,

           mean(Mg) as Avg_Mg,

           mean(Ca) as Avg_Ca

    from glass_data

    group by Glass_Type;

    

    title "Average Sodium, Magnesium, and Calcium by Glass Type";

    select * from GroupMeans;

quit;

Output:

Average Sodium, Magnesium, and Calcium by Glass Type

Glass_TypeAvg_NaAvg_MgAvg_Ca
"Building Float"13.43.858.65
"Building NonFloat"13.6583.668.236
"Container"14.153.318.8075
"Headlamp"14.62753.04257.8525
"Tableware"12.8853.919.065
"Vehicle Float"13.53253.77258.05

/* Step 8: Define a macro to print summary for any variable */

%macro summary(var);

    proc means data=glass_data n mean std min max;

        var &var;

        title "Summary Statistics for &var";

    run;

%mend summary;


%summary(Na);

Output:

Summary Statistics for Na

The MEANS Procedure

Analysis Variable : Na
NMeanStd DevMinimumMaximum
2513.70680000.569639712.790000014.7000000

%summary(Mg);

Output:

Summary Statistics for Mg

The MEANS Procedure

Analysis Variable : Mg
NMeanStd DevMinimumMaximum
253.59360000.35919912.87000004.4900000

%summary(Si);

Output:

Summary Statistics for Si

The MEANS Procedure

Analysis Variable : Si
NMeanStd DevMinimumMaximum
2572.66880000.520563271.780000073.3000000
/* Step 9: Create a bar chart using PROC SGPLOT */

proc sgplot data=GroupMeans;

    vbar Glass_Type / response=Avg_Na stat=mean datalabel;

    title "Average Sodium Content by Glass Type";

run;

Output:





To Visit My Previous Sas Questions Datasets:Click Here
To Visit My Previous Home Energy Consumption Dataset:Click Here
To Visit My Previous Wildlife Migration Dataset:Click Here
To Visit My Previous Endangered Species Dataset: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