BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fdb00004
Fluorite | Level 6

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;
 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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).

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
fdb00004
Fluorite | Level 6

sorry the update column was a mistake only up_date column in dataset

Kurt_Bremser
Super User
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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 767 views
  • 2 likes
  • 3 in conversation