Hi all,
I hope you all have a good weekend!
I am using the following code to compute statistics that I need:
data want;
if _N_ = 1 then do;
dcl hash h(multidata : "Y", ordered : "Y");
h.definekey("d", "t");
h.definedata("d", "t", "v", "p");
h.definedone();
dcl hiter hi("h");
dcl hash hh();
hh.definekey("d", "t");
hh.definedata("s");
hh.definedone();
do until (z);
set have end = z;
h.add(key : date, key : time, data : date, data : time, data : volume, data : price);
if hh.find(key : date, key : Time) ne 0 then s = volume;
else s + volume;* will be used to compute vol-weig price ;
hh.replace(key : date, key : Time, data : s);
end;
end;
set have;
d = .; t = .; v = .; p = .; s = .;
if h.find(key : Date, key : Time + 10) = 0 then mt = t;
else do;
rc = hi.setcur(key : Date, key : Time);
do i = 1 by 1 while(hi.next() = 0 & Date = d);
if t - time > 10 then do;
if i = 1 then mt = .;
leave;
end;
if t > time then mt = t;
end;
end;
rc = hh.find(key: date, key : mt);
rc = h.reset_dup();
do while (h.do_over(key : date, key : mt) = 0);
pp = sum(pp, divide(v, s) * p);
end;
dif = (log(price) - log(pp))*10000; *bps;
run;
The issue is I need to apply this function by group. Is there an easy way of doing this?
Thanks!
What defines your "by group"?
Which "function" is to be applied? On what values.
Many processes may involve additional steps such a basic data set, possibly as created here and then a follow up to do group processing.
Sorry,
I should define the question better. I found a way. Thanks!
Why use hash objects here, for what seems like simple arithmetic and cumulative totals? All of that is done easily in a DATA step without resorting to hash objects.
Example:
proc sort data=sashelp.class out=class;
by sex;
run;
data want;
set class;
by sex;
if first.sex then cumulative_height=0;
cumulative_height+height;
run;
Whatever the exact function you are trying to get can be easily incorporated into this method.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.