My intention here is to calculate two averages.
1. Before a certain dates and another
2. After certain dates.
Using proc SQL in SAS. I have done this seperately and then merge. How can i simplify the code with just one Proc Sql step.
Thank you.
proc SQL;
create table AvgWindPressure as
select distinct MinPressure, avg(MaxwindMPH) as avgWind_before from work.Storm
where up_date < end_date
group by MinPressure, enddate;
quit;
proc SQL;
create table AvgWindpressure2 as
select distinct MinPressure, avg(MaxwindMPH) as avg_after
from MaxwindMPH
where up_date > enddate
group by MinPressure, enddate;
quit;
proc sql;
create table AvgWindPressure as
select
MinPressure,
avg(case when up_date < end_date then MaxwindMPH else . end) as avgWind_before,
avg(case when up_date >= end_date then MaxwindMPH else . end) as avgWind_after
from work.Storm
group by MinPressure
;
quit;
No DISTINCT clause is needed, GROUP BY will take care of that.
Do you really have a column update and a column up_date in your dataset?
To get good help fast, it is imperative to present your source data in a usable way (data step with datalines).
Here's an example using data set SASHELP.CLASS
proc sql;
create table want as select distinct sex,
mean(case when age<14 then height else . end) as mean_before,
mean(case when age>=14 then height else . end) as mean_after
from sashelp.class
group by sex;
quit;
It's also quite easy to do using PROC SUMMARY.
sorry the update column was a mistake only up_date column in dataset
proc sql;
create table AvgWindPressure as
select
MinPressure,
avg(case when up_date < end_date then MaxwindMPH else . end) as avgWind_before,
avg(case when up_date >= end_date then MaxwindMPH else . end) as avgWind_after
from work.Storm
group by MinPressure
;
quit;
No DISTINCT clause is needed, GROUP BY will take care of that.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.