BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Khaladdin
Quartz | Level 8

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Khaladdin
Quartz | Level 8

Sorry,

 

I should define the question better. I found a way. Thanks!

View solution in original post

3 REPLIES 3
ballardw
Super User

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.

Khaladdin
Quartz | Level 8

Sorry,

 

I should define the question better. I found a way. Thanks!

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 891 views
  • 0 likes
  • 3 in conversation