BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aminkarimid
Lapis Lazuli | Level 10

Hello everybody;

I want to calculate a division which the nominator is intraday variable and the denominator is the aggregate of daily values of two days before and after that specific date and name. This new variable is called adjusted as shown in table below;

 

table1:

namedatetimeIntradaydailyadjusted
A12/1/20139:30:001  
A12/1/201310:00:0023 
A12/2/201310:30:003  
A12/2/201311:00:0025 
A12/3/201310:00:002 0.0952381
A12/3/201310:30:001 0.04761905
A12/3/201311:00:00140.04761905
A12/4/201311:30:001 0.04347826
A12/4/201312:00:003 0.13043478
A12/4/201312:30:00370.13043478
A12/7/20139:30:001 0.05
A12/7/201310:00:00120.05
A12/8/201310:30:002 0.11111111
A12/8/201311:00:00350.16666667
A12/17/201311:30:001 0.07692308
A12/17/201312:00:00120.07692308
A12/18/201312:30:0022 
A12/28/20139:30:001  
A12/28/201310:00:0012 
B12/8/201310:30:002  
B12/8/201311:30:002  
B12/8/201312:30:0015 
B12/14/20139:30:001  
B12/14/201310:30:0012 
B12/15/201312:00:002 0.13333333
B12/15/201312:30:00130.06666667
B12/24/20139:30:00110.08333333
B12/25/201310:00:002 0.125
B12/25/201310:30:00240.125
B12/26/201312:00:00220.125
B12/30/20139:30:003  
B12/30/201310:00:002  
B12/30/201310:30:0016 
B1/3/201411:00:001  
B1/3/201411:30:001  
B1/13/201412:00:0013 

 

Now, let me give you an example. the value '0.0952381' at date '12/3/2013' in adjusted column is (2/(3+5+4+7+2)). the value '2' in nominator is intraday value at '12/3/2013' and the denominator is the sum of daily values for two days before and after plus daily volume of that specific day. For another instance, the adjusted value '0.04347826' at date '12/4/2013' is (1/(3+5+4+7+2)).

 

How can I create the variable adjusted in table 1?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

For an ordinary bloke like me, that explanation doesn't quite suffice. But here you, my code works for your sample:

 

data have;

infile datalines expandtabs truncover ;

input name $    date :mmddyy10. time : time10.  Intraday   daily ;

format date mmddyy10. time time10.;

datalines;

A    12/1/2013  9:30:00         1   

A    12/1/2013  10:00:00   2    3

A    12/2/2013  10:30:00   3   

A    12/2/2013  11:00:00   2    5

A    12/3/2013  10:00:00   2   

A    12/3/2013  10:30:00   1   

A    12/3/2013  11:00:00   1    4

A    12/4/2013  11:30:00   1   

A    12/4/2013  12:00:00   3   

A    12/4/2013  12:30:00   3    7

A    12/7/2013  9:30:00         1   

A    12/7/2013  10:00:00   1    2

A    12/8/2013  10:30:00   2   

A    12/8/2013  11:00:00   3    5

A    12/17/2013 11:30:00   1   

A    12/17/2013 12:00:00   1    2

A    12/18/2013 12:30:00   2    2

A    12/28/2013 9:30:00         1   

A    12/28/2013 10:00:00   1    2

B    12/8/2013  10:30:00   2   

B    12/8/2013  11:30:00   2   

B    12/8/2013  12:30:00   1    5

B    12/14/2013 9:30:00         1   

B    12/14/2013 10:30:00   1    2

B    12/15/2013 12:00:00   2   

B    12/15/2013 12:30:00   1    3

B    12/24/2013 9:30:00         1    1

B    12/25/2013 10:00:00   2   

B    12/25/2013 10:30:00   2    4

B    12/26/2013 12:00:00   2    2

B    12/30/2013 9:30:00         3   

B    12/30/2013 10:00:00   2   

B    12/30/2013 10:30:00   1    6

B    1/3/2014   11:00:00   1   

B    1/3/2014   11:30:00   1   

B    1/13/2014  12:00:00   1    3

;

 

 

 

proc sort data= have(where=(daily ne .)) out=_have nodupkey;

by name date;

run;

 

 

data want;

if (_n_ = 1) then do;

    declare hash h(dataset: "_have", multidata: "yes");

    h.definekey('name','date');

    h.definedone();

end;

call missing(max_date);

do n=1 by 1 until(last.name);

     set _have;

     by name date;

     max_date=max(date,max_date);

end;

array dt(*) prev_date1  prev_date2  after_date1  after_date2;

do n1=1 by 1 until(last.name);

     set _have;

     by name date;

     if first.name then call missing(of dt(*));

     curr_date=date;

     curr_date_daily=daily;

     prev_date1=lag1(date);

     prev_date2=lag2(date);

     if n1>2 and n1<=n-2 then

           do;

                if not missing(prev_date1) and not missing(prev_date2) then

                     do;

                           c=0;

                           do t=date+1 to max_date;

                           rc=h.check(key:name, key:t);

                           if rc=0 then

                                do;

                                     c+1;

                                     if c=1 then after_date1=t;

                                     else if c=2 then

                                           do;

                                                after_date2=t;

                                                leave;

                                           end;

                                end;

                               

                           end;

                          

                     end;

                if cmiss(of dt(*))=0 then output;

               

           end;

    

end;

format t max_date prev_date1 prev_date2 after_date1 after_date2 curr_date mmddyy10.;

keep  name date prev_date1  prev_date2  after_date1  after_date2 curr_date_daily;

run;

 

data final_WANT;

keep name _date  time intraday _daily adjusted;

retain  name _date  time intraday _daily adjusted;

if (_n_ = 1) then do;

     if 0 then set have;

     if 0 then set want;

    declare hash h(dataset: "have(where=(daily ne .))", multidata: "yes",ordered:'y');

    h.definekey('name','date');

    h.definedata('name','date','daily');

     h.definedone();

     declare hash h1(dataset: "want", multidata: "yes",ordered:'y');

    h1.definekey('name','date');

    h1.definedata(ALL: 'YES' );

     h1.definedone();

end;

array t(*) prev_date1  prev_date2  after_date1  after_date2;

array t1(*)  prev_date1_daily  prev_date2_daily   after_date1_daily     after_date2_daily;

do  until(last.name);

     set have;

     by name date;

     _daily=daily;

     _date=date;

     rc=h1.find();

     if rc=0 then

           do;

                do i= 1 to dim(t);

                     temp= t(i);

                     rc2=h.find(key:name, key:temp);

                     if rc2=0 then t1(i)=daily;

                end;

                adjusted=divide(intraday,sum(of t1(*),curr_date_daily));

           end;

     else do;

           call missing(of t(*));

           call missing(of t1(*),adjusted);

           end;

    

     output;

end;

format _date mmddyy10.;

run;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

The question is indeed interesting however the dates in your convert logic explanation seem to fall outside of range or perhaps I am the one who isn't understanding well. Please clarify:

 

 Denominator: aggregate of daily values of two days before and after that specific date and name

   Eg: 

A12/3/2013  --> 3,5, 4 ,7makes sense to me as those are within two days before and after, but what about 2 that falls on 12/7/2013 

 

 

I am sure most others may gauge your requirement thinking out of the box, but I need more comprehensive explanation. Thank you!

aminkarimid
Lapis Lazuli | Level 10
Thanks @novinosrin;
values of two days before are '3' at '12/1/2013' and '5' at '12/2/2013';
The value of current day is '4' at '12/3/2013';
Values of two days after are '7' at '12/4/2013' and 2 at '12/7/2013'. So, the denominator at date '12/3/2013' is (3+5+4+7+2).
art297
Opal | Level 21

It would help if you added a column for each row in your example explain either (1) which rows were selected or (2) why a value wasn't computed.

 

Art, CEO, AnalystFinder.com

 

novinosrin
Tourmaline | Level 20

For an ordinary bloke like me, that explanation doesn't quite suffice. But here you, my code works for your sample:

 

data have;

infile datalines expandtabs truncover ;

input name $    date :mmddyy10. time : time10.  Intraday   daily ;

format date mmddyy10. time time10.;

datalines;

A    12/1/2013  9:30:00         1   

A    12/1/2013  10:00:00   2    3

A    12/2/2013  10:30:00   3   

A    12/2/2013  11:00:00   2    5

A    12/3/2013  10:00:00   2   

A    12/3/2013  10:30:00   1   

A    12/3/2013  11:00:00   1    4

A    12/4/2013  11:30:00   1   

A    12/4/2013  12:00:00   3   

A    12/4/2013  12:30:00   3    7

A    12/7/2013  9:30:00         1   

A    12/7/2013  10:00:00   1    2

A    12/8/2013  10:30:00   2   

A    12/8/2013  11:00:00   3    5

A    12/17/2013 11:30:00   1   

A    12/17/2013 12:00:00   1    2

A    12/18/2013 12:30:00   2    2

A    12/28/2013 9:30:00         1   

A    12/28/2013 10:00:00   1    2

B    12/8/2013  10:30:00   2   

B    12/8/2013  11:30:00   2   

B    12/8/2013  12:30:00   1    5

B    12/14/2013 9:30:00         1   

B    12/14/2013 10:30:00   1    2

B    12/15/2013 12:00:00   2   

B    12/15/2013 12:30:00   1    3

B    12/24/2013 9:30:00         1    1

B    12/25/2013 10:00:00   2   

B    12/25/2013 10:30:00   2    4

B    12/26/2013 12:00:00   2    2

B    12/30/2013 9:30:00         3   

B    12/30/2013 10:00:00   2   

B    12/30/2013 10:30:00   1    6

B    1/3/2014   11:00:00   1   

B    1/3/2014   11:30:00   1   

B    1/13/2014  12:00:00   1    3

;

 

 

 

proc sort data= have(where=(daily ne .)) out=_have nodupkey;

by name date;

run;

 

 

data want;

if (_n_ = 1) then do;

    declare hash h(dataset: "_have", multidata: "yes");

    h.definekey('name','date');

    h.definedone();

end;

call missing(max_date);

do n=1 by 1 until(last.name);

     set _have;

     by name date;

     max_date=max(date,max_date);

end;

array dt(*) prev_date1  prev_date2  after_date1  after_date2;

do n1=1 by 1 until(last.name);

     set _have;

     by name date;

     if first.name then call missing(of dt(*));

     curr_date=date;

     curr_date_daily=daily;

     prev_date1=lag1(date);

     prev_date2=lag2(date);

     if n1>2 and n1<=n-2 then

           do;

                if not missing(prev_date1) and not missing(prev_date2) then

                     do;

                           c=0;

                           do t=date+1 to max_date;

                           rc=h.check(key:name, key:t);

                           if rc=0 then

                                do;

                                     c+1;

                                     if c=1 then after_date1=t;

                                     else if c=2 then

                                           do;

                                                after_date2=t;

                                                leave;

                                           end;

                                end;

                               

                           end;

                          

                     end;

                if cmiss(of dt(*))=0 then output;

               

           end;

    

end;

format t max_date prev_date1 prev_date2 after_date1 after_date2 curr_date mmddyy10.;

keep  name date prev_date1  prev_date2  after_date1  after_date2 curr_date_daily;

run;

 

data final_WANT;

keep name _date  time intraday _daily adjusted;

retain  name _date  time intraday _daily adjusted;

if (_n_ = 1) then do;

     if 0 then set have;

     if 0 then set want;

    declare hash h(dataset: "have(where=(daily ne .))", multidata: "yes",ordered:'y');

    h.definekey('name','date');

    h.definedata('name','date','daily');

     h.definedone();

     declare hash h1(dataset: "want", multidata: "yes",ordered:'y');

    h1.definekey('name','date');

    h1.definedata(ALL: 'YES' );

     h1.definedone();

end;

array t(*) prev_date1  prev_date2  after_date1  after_date2;

array t1(*)  prev_date1_daily  prev_date2_daily   after_date1_daily     after_date2_daily;

do  until(last.name);

     set have;

     by name date;

     _daily=daily;

     _date=date;

     rc=h1.find();

     if rc=0 then

           do;

                do i= 1 to dim(t);

                     temp= t(i);

                     rc2=h.find(key:name, key:temp);

                     if rc2=0 then t1(i)=daily;

                end;

                adjusted=divide(intraday,sum(of t1(*),curr_date_daily));

           end;

     else do;

           call missing(of t(*));

           call missing(of t1(*),adjusted);

           end;

    

     output;

end;

format _date mmddyy10.;

run;

Reeza
Super User

Is it +/- 2 BUSINESS days, calendar days, event days, ....random days?

 

values of two days before are '3' at '12/1/2013' and '5' at '12/2/2013';
The value of current day is '4' at '12/3/2013';
Values of two days after are '7' at '12/4/2013' and 2 at '12/7/2013'. So, the denominator at date '12/3/2013' is (3+5+4+7+2).

 

December 7 is 4 days past December 3rd NOT 2 days so why/how is it included? 

 

Business days and/or calendar days don't make sense, so I'm guessing event days of some kind?  But then those are days and that's the confusion with how you've phrased your question. Please take more time in detailing your questions. 

 

If it is event days, create an event day variable and then use a SQL join with itself and a margin of +/- 2 days. You've asked previously solved questions that demonstrate this type of logic. 

 

 

aminkarimid
Lapis Lazuli | Level 10

As you said, there is no same length between days. So, the better explanation is two rows after and before of current row.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 6 replies
  • 1089 views
  • 1 like
  • 4 in conversation