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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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