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:
name | date | time | Intraday | daily | adjusted |
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 | 0.0952381 | |
A | 12/3/2013 | 10:30:00 | 1 | 0.04761905 | |
A | 12/3/2013 | 11:00:00 | 1 | 4 | 0.04761905 |
A | 12/4/2013 | 11:30:00 | 1 | 0.04347826 | |
A | 12/4/2013 | 12:00:00 | 3 | 0.13043478 | |
A | 12/4/2013 | 12:30:00 | 3 | 7 | 0.13043478 |
A | 12/7/2013 | 9:30:00 | 1 | 0.05 | |
A | 12/7/2013 | 10:00:00 | 1 | 2 | 0.05 |
A | 12/8/2013 | 10:30:00 | 2 | 0.11111111 | |
A | 12/8/2013 | 11:00:00 | 3 | 5 | 0.16666667 |
A | 12/17/2013 | 11:30:00 | 1 | 0.07692308 | |
A | 12/17/2013 | 12:00:00 | 1 | 2 | 0.07692308 |
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 | 0.13333333 | |
B | 12/15/2013 | 12:30:00 | 1 | 3 | 0.06666667 |
B | 12/24/2013 | 9:30:00 | 1 | 1 | 0.08333333 |
B | 12/25/2013 | 10:00:00 | 2 | 0.125 | |
B | 12/25/2013 | 10:30:00 | 2 | 4 | 0.125 |
B | 12/26/2013 | 12:00:00 | 2 | 2 | 0.125 |
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 |
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.
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;
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:
A | 12/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!
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
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;
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.
As you said, there is no same length between days. So, the better explanation is two rows after and before of current row.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.