SMART HOME ENERGY CONSUMPTION ANALYSIS USING SAS | PROC SQL | PROC MEANS | PROC FREQ | PROC UNIVARIATE | PROC SGPLOT | PROC MACRO
/*Creating a unique and comprehensive dataset centered around "Smart Home Energy Consumption"*/
/*Data Generation in SAS*/
data smart_home_energy;
length appliance_type $15; /* Set the desired length */
format timestamp datetime20.;
array rooms[5] $10 _temporary_ ('LivingRoom', 'Kitchen', 'Bedroom1', 'Bedroom2', 'Bathroom');
array appliances[5] $15 _temporary_ ('HVAC', 'Lighting', 'Refrigerator', 'Washer', 'Dryer');
do i = 1 to 30;
timestamp = '01JAN2025:00:00:00'dt + intnx('hour', 0, i-1, 'same');
room_id = rooms[ceil(ranuni(0)*5)];
appliance_type = appliances[ceil(ranuni(0)*5)];
appliance_id = cats(appliance_type, '_', put(i, z5.));
energy_consumed = round(ranuni(0)*2, 0.01);
occupancy_status = ifc(ranuni(0) > 0.5, 'Occupied', 'Unoccupied');
temperature = round(20 + ranuni(0)*5, 0.1);
humidity = round(30 + ranuni(0)*50, 0.1);
output;
end;
drop i;
run;
proc print;run;
Output:
| Obs | appliance_type | timestamp | room_id | appliance_id | energy_consumed | occupancy_status | temperature | humidity |
|---|---|---|---|---|---|---|---|---|
| 1 | HVAC | 01JAN2025:00:00:00 | Kitchen | HVAC_00001 | 1.96 | Unoccupied | 21.3 | 72.9 |
| 2 | Lighting | 01JAN2025:01:00:00 | Kitchen | Lighting_00002 | 0.24 | Occupied | 22.6 | 63.3 |
| 3 | Dryer | 01JAN2025:02:00:00 | Bedroom2 | Dryer_00003 | 1.09 | Occupied | 20.7 | 66.3 |
| 4 | Dryer | 01JAN2025:03:00:00 | Bathroom | Dryer_00004 | 0.02 | Occupied | 22.0 | 54.9 |
| 5 | Washer | 01JAN2025:04:00:00 | Bedroom2 | Washer_00005 | 0.84 | Occupied | 21.0 | 79.8 |
| 6 | Refrigerator | 01JAN2025:05:00:00 | LivingRoom | Refrigerator_00006 | 1.65 | Occupied | 24.7 | 35.7 |
| 7 | Dryer | 01JAN2025:06:00:00 | LivingRoom | Dryer_00007 | 0.23 | Unoccupied | 20.6 | 74.4 |
| 8 | Lighting | 01JAN2025:07:00:00 | Kitchen | Lighting_00008 | 0.02 | Occupied | 22.6 | 63.5 |
| 9 | Refrigerator | 01JAN2025:08:00:00 | LivingRoom | Refrigerator_00009 | 0.51 | Occupied | 20.8 | 44.2 |
| 10 | Washer | 01JAN2025:09:00:00 | Bathroom | Washer_00010 | 0.02 | Unoccupied | 22.6 | 71.1 |
| 11 | Lighting | 01JAN2025:10:00:00 | Kitchen | Lighting_00011 | 1.95 | Occupied | 24.7 | 38.9 |
| 12 | Dryer | 01JAN2025:11:00:00 | Kitchen | Dryer_00012 | 0.05 | Occupied | 22.3 | 33.4 |
| 13 | Lighting | 01JAN2025:12:00:00 | Bathroom | Lighting_00013 | 0.18 | Occupied | 20.9 | 31.1 |
| 14 | Dryer | 01JAN2025:13:00:00 | Bedroom2 | Dryer_00014 | 0.03 | Occupied | 23.6 | 63.5 |
| 15 | Washer | 01JAN2025:14:00:00 | LivingRoom | Washer_00015 | 0.10 | Unoccupied | 23.4 | 55.9 |
| 16 | Lighting | 01JAN2025:15:00:00 | LivingRoom | Lighting_00016 | 1.67 | Unoccupied | 22.7 | 76.7 |
| 17 | HVAC | 01JAN2025:16:00:00 | Kitchen | HVAC_00017 | 0.36 | Unoccupied | 23.1 | 46.3 |
| 18 | Lighting | 01JAN2025:17:00:00 | Bedroom2 | Lighting_00018 | 1.95 | Occupied | 22.8 | 55.9 |
| 19 | Lighting | 01JAN2025:18:00:00 | Kitchen | Lighting_00019 | 1.83 | Unoccupied | 23.3 | 44.1 |
| 20 | HVAC | 01JAN2025:19:00:00 | Bedroom1 | HVAC_00020 | 0.90 | Occupied | 23.1 | 57.3 |
| 21 | Dryer | 01JAN2025:20:00:00 | Bathroom | Dryer_00021 | 1.64 | Occupied | 22.7 | 44.4 |
| 22 | Lighting | 01JAN2025:21:00:00 | Bedroom2 | Lighting_00022 | 1.76 | Occupied | 24.3 | 67.8 |
| 23 | Lighting | 01JAN2025:22:00:00 | Bedroom1 | Lighting_00023 | 1.35 | Occupied | 20.2 | 47.3 |
| 24 | Lighting | 01JAN2025:23:00:00 | Bathroom | Lighting_00024 | 1.08 | Unoccupied | 23.2 | 74.4 |
| 25 | Washer | 02JAN2025:00:00:00 | Kitchen | Washer_00025 | 0.47 | Occupied | 20.5 | 67.8 |
| 26 | Lighting | 02JAN2025:01:00:00 | Bedroom2 | Lighting_00026 | 1.23 | Unoccupied | 24.0 | 60.2 |
| 27 | Lighting | 02JAN2025:02:00:00 | Bedroom2 | Lighting_00027 | 1.68 | Occupied | 23.9 | 62.6 |
| 28 | HVAC | 02JAN2025:03:00:00 | Kitchen | HVAC_00028 | 0.37 | Occupied | 23.0 | 38.8 |
| 29 | HVAC | 02JAN2025:04:00:00 | Bedroom2 | HVAC_00029 | 0.29 | Unoccupied | 23.3 | 46.3 |
| 30 | Washer | 02JAN2025:05:00:00 | LivingRoom | Washer_00030 | 1.14 | Unoccupied | 21.0 | 67.8 |
/*Total Energy Consumption per Appliance Type*/
proc sql;
select appliance_type,
sum(energy_consumed) as total_energy format=8.2
from smart_home_energy
group by appliance_type;
quit;
Output:
| appliance_type | total_energy |
|---|---|
| Dryer | 3.06 |
| HVAC | 3.88 |
| Lighting | 14.94 |
| Refrigerator | 2.16 |
| Washer | 2.57 |
/*Average Temperature and Humidity per Room*/
proc sql;
select room_id,
avg(temperature) as avg_temp format=5.2,
avg(humidity) as avg_humidity format=5.2
from smart_home_energy
group by room_id;
quit;
Output:
| room_id | avg_temp | avg_humidity |
|---|---|---|
| Bathroom | 22.28 | 55.18 |
| Bedroom1 | 21.65 | 52.30 |
| Bedroom2 | 22.95 | 62.80 |
| Kitchen | 22.60 | 52.11 |
| LivingRoom | 22.20 | 59.12 |
/*Peak Energy Consumption Hours*/
proc sql;
select hour(timestamp) as hour_of_day,
sum(energy_consumed) as total_energy format=8.2
from smart_home_energy
group by hour_of_day
order by total_energy desc;
quit;
Output:
| hour_of_day | total_energy |
|---|---|
| 5 | 2.79 |
| 2 | 2.77 |
| 0 | 2.43 |
| 10 | 1.95 |
| 17 | 1.95 |
| 18 | 1.83 |
| 21 | 1.76 |
| 15 | 1.67 |
| 20 | 1.64 |
| 1 | 1.47 |
| 22 | 1.35 |
| 4 | 1.13 |
| 23 | 1.08 |
| 19 | 0.90 |
| 8 | 0.51 |
| 3 | 0.39 |
| 16 | 0.36 |
| 6 | 0.23 |
| 12 | 0.18 |
| 14 | 0.10 |
| 11 | 0.05 |
| 13 | 0.03 |
| 9 | 0.02 |
| 7 | 0.02 |
/*Macro to Analyze Energy Consumption for a Specific Appliance*/
%macro analyze_appliance(appliance=);
proc sql;
select appliance_type,
avg(energy_consumed) as avg_energy format=5.2,
max(energy_consumed) as max_energy format=5.2,
min(energy_consumed) as min_energy format=5.2
from smart_home_energy
where appliance_type = "&appliance"
group by appliance_type;
quit;
%mend analyze_appliance;
%analyze_appliance(appliance=HVAC);
Output:
| appliance_type | avg_energy | max_energy | min_energy |
|---|---|---|---|
| HVAC | 0.78 | 1.96 | 0.29 |
%analyze_appliance(appliance=Lighting);
Output:
| appliance_type | avg_energy | max_energy | min_energy |
|---|---|---|---|
| Lighting | 1.25 | 1.95 | 0.02 |
/*Macro to Generate Reports for Each Room*/
proc sql noprint;
select distinct room_id into :room1-:room5
from smart_home_energy;
quit;
%macro room_reports;
%do i = 1 %to 5;
proc sql;
title "Energy Report for &&room&i";
select appliance_type,
sum(energy_consumed) as total_energy format=8.2
from smart_home_energy
where room_id = "&&room&i"
group by appliance_type;
quit;
%end;
%mend room_reports;
%room_reports;
Output:
| Energy Report for Bathroom |
| appliance_type | total_energy |
|---|---|
| Dryer | 1.66 |
| Lighting | 1.26 |
| Washer | 0.02 |
| Energy Report for Bedroom1 |
| appliance_type | total_energy |
|---|---|
| HVAC | 0.90 |
| Lighting | 1.35 |
| Energy Report for Bedroom2 |
| appliance_type | total_energy |
|---|---|
| Dryer | 1.12 |
| HVAC | 0.29 |
| Lighting | 6.62 |
| Washer | 0.84 |
| Energy Report for Kitchen |
| appliance_type | total_energy |
|---|---|
| Dryer | 0.05 |
| HVAC | 2.69 |
| Lighting | 4.04 |
| Washer | 0.47 |
| Energy Report for LivingRoom |
| appliance_type | total_energy |
|---|---|
| Dryer | 0.23 |
| Lighting | 1.67 |
| Refrigerator | 2.16 |
| Washer | 1.24 |
/*Energy Consumption Over Time*/
proc sgplot data=smart_home_energy;
series x=timestamp y=energy_consumed / group=appliance_type;
xaxis label="Timestamp";
yaxis label="Energy Consumed (kWh)";
title "Energy Consumption Over Time by Appliance Type";
run;
Log:
NOTE: PROCEDURE SGPLOT used (Total process time):
real time 2.46 seconds
cpu time 0.51 seconds
NOTE: The column format DATETIME20 is replaced by an auto-generated format on the axis.
NOTE: The column format DATETIME20 is replaced by an auto-generated format on the axis.
NOTE: Listing image output written to SGPlot1.png.
NOTE: There were 30 observations read from the data set WORK.SMART_HOME_ENERGY.
/*Temperature vs. Humidity Scatter Plot*/
proc sgplot data=smart_home_energy;
scatter x=temperature y=humidity / group=occupancy_status;
xaxis label="Temperature (°C)";
yaxis label="Humidity (%)";
title "Temperature vs. Humidity Colored by Occupancy Status";
run;
No comments:
Post a Comment