<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />Hi Folks,
I have a dataset that includes daily snow depth data for a 20 year time period. I need to determine the date by year when the peak, or maximum, snow depth value occurred. Any suggestions on how to accomplish this? Thanks.
you may get many dates which share the maximum depth... This is how to get them:
proc sql;
create table peaks as
select year(date) as year, date, depth
from myData
group by calculated year
having depth = max(depth);
select * fron peaks;
quit;
PG
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />There would be many ways to tackle your problem. To start, let 's take sashelp.class as an example, using 'sex' as the equavalent to your 'year', 'height' to your 'snow depth':
1. SQL solution:
proc sql;
create table want as
select a.* from sashelp.class a,
(select sex, max(height) as max from sashelp.class group by sex) b
where a.sex=b.sex and a.height=b.max
;
quit;
2. Data step;
proc sort data=sashelp.class out=tem;
by sex;
run;
data want (drop=_h);
do until (last.sex);
set tem;
by sex;
_h=max(_h,height);
end;
do until (last.sex);
set tem;
by sex;
if height=_h then output;
end;
run;
And of course, there are many other ways , such as proc summary etc, worth looking into:
proc summary data=sashelp.class nway;
class sex;
var height;
output out=want (drop=_:) maxid(height(name) height(age) height(weight))=name age weight max= ;
run;
Regards,
Haikuo
The approach outlined by mpb is excellent for adding the maximum depth to the original table. I've modified my original example to print out the maximum depth and date from the table. I'm assuming that you have the fields Snow Date (date type) and Snow Depth (number). I've added a column formula for a year column.
dt = New Table( "Snowfall",
Add Rows( 20 ),
New Column( "Snow Depth", Numeric, Continuous, Format( "Best", 12 ),
Set Values(
[34, 45, 53, 56, 99, 67, 12, 78, 53, 89, 77, 90, 48, 12, 73, 23, 12, 34,
6, 45] ) ),
New Column( "Snow Date", Numeric, Continuous, Format( "ddMonyyyy", 10 ),
Input Format( "ddMonyyyy" ),
Set Values(
[1767225600, 1771113600, 1805155200, 1809129600, 1843084800, 1838160000,
1889049600, 1893283200, 1896220800, 1900281600, 1950566400, 1955491200,
1957651200, 1964217600, 1990396800, 1995926400, 2023488000, 2029449600,
2051481600, 2056752000] ) ),
New Column( "Snow Year", Numeric, Continuous, Format( "Best", 12 ),
Formula( Year( :Snow Date ) ),
Lock( 1 )
)
);
// Get the highest snowfall for each year
summarize(year_list=by(:Snow Year), max_depth_list=max(:Snow Depth));
// Now get the dates of the highest snowfall
for (i = 1, i <= nrows(max_depth_list), i++,
one_year = year_list[i];
one_depth = max_depth_list[i];
// Could have the same maximum snow depth on different days
one_year_max_rows = dt << get rows where(:Snow Year == num(one_year) &
:Snow Depth == one_depth);
for (k = 1, k <= nrows(one_year_max_rows), k++,
m = one_year_max_rows[k];
snow_date = :Snow Date[m];
print("Maximum snowfall of " || char(one_depth) || " on " ||
format(snow_date, "ddMonyyyy"));
);
);