BookmarkSubscribeRSS Feed
sas112
Calcite | Level 5
I have a dataset below. I would like to find the total payment of each EFAMID. The 
total payment is calculated as Pay during 60 days prior to and 90 days after the service (OpCode=12)
date. For example, EFAMID=101, the total payment is 10.7 + 18.0 + 10.2 = 38.9 because
the following observations are within 60 days prior to and 90 days after the
01/14/2013 (the SvcDate of its OpCode=12).

101 01/13/2013 19 10.7
101 01/14/2013 12 18.0
101 04/16/2013 13 10.2

I try to set the filtering conditions at where, but it is not correct. How do I specify
multiple conditions at where? Thanks in advance.

data have;
input EFAMID $ SvcDate :mmddyy10. OpCode :$2. Pay;
format svcdate yymmdd10.;
datalines;
101 09/03/2012 10 11.0
101 01/13/2013 19 10.7
101 01/14/2013 12 18.0
101 04/16/2013 13 10.2
101 11/10/2013 19 10.0
102 02/10/2013 10 9.7
102 02/12/2013 12 17.5
102 02/13/2013 15 10.2
;


proc sql;
create table want as
select a.EFAMID,
(
select sum(Pay)
from have c
where
a.EFAMID = c.EFAMID and
abs(a.SvcDate - c.SvcDate) le 60 or ((c.SvcDate gt a.SvcDate) and (c.SvcDate - a.SvcDate le 90))
) as THU
from have a
where a.opcode = '12'
;
quit;
4 REPLIES 4
mkeintz
PROC Star

@sas112 wrote:
I have a dataset below. I would like to find the total payment of each EFAMID. The 
total payment is calculated as Pay during 60 days prior to and 90 days after the service (OpCode=12)
date. For example, EFAMID=101, the total payment is 10.7 + 18.0 + 10.2 = 38.9 because
the following observations are within 60 days prior to and 90 days after the
01/14/2013 (the SvcDate of its OpCode=12).

101 01/13/2013 19 10.7
101 01/14/2013 12 18.0
101 04/16/2013 13 10.2

...
I try to set the filtering conditions at where, but it is not correct.
...

 

But 4/16/2013 is NOT within 90 days of the opcode 12 date of 1/14/2013.  It is 92 days later, so should not be included in your total for EFAMID 101.   If by "it is not correct" you mean your total for 101 is only 28.7, then it is correct.

 

BTW, here is a data step solution.  It assumes:

  1. The data are sorted by EFAMID  (but not necessarily svcdate within EFAMID)
  2. You never have more than 1 instance of opcode 12 for any id.
data have;
input EFAMID $ SvcDate :mmddyy10. OpCode :$2. Pay;
format svcdate yymmdd10.;
datalines;
101 09/03/2012 10 11.0
101 01/13/2013 19 10.7
101 01/14/2013 12 18.0
101 04/16/2013 13 10.2
101 11/10/2013 19 10.0
102 02/10/2013 10 9.7
102 02/12/2013 12 17.5
102 02/13/2013 15 10.2
;

data want (keep=EFAMID OPCODE12_DATE THU);
  merge have
        have (where=(opcode='12') rename=(svcdate=opcode12_date)) ;
  by efamid;
  if first.efamid then THU=0;

  if (opcode12_date-60) <= svcdate and svcdate <= (opcode12_date+90) then THU+pay;

  if last.efamid;
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sas112
Calcite | Level 5

Thanks for point out

    But 4/16/2013 is NOT within 90 days of the opcode 12 date of 1/14/2013.  It is 92 days later,  so should not be included in your total for EFAMID 101.

You are right. 4/16/2013 is NOT within 90 days of the opcode 12 date of 1/14/2013.

I need to set the conditions at proc sql since it is just part of my process. Do you have any idea how to specify those conditions at proc sql? Thanks.

PGStats
Opal | Level 21

With SQL you could do:

 

proc sql;
select 
    a.efamid, a.svcDate,
    sum(b.Pay) as totalPay
from
    have as a inner join
    have as b on a.efamid=b.efamid and intck('day', a.svcDate, b.svcDate) between -60 and 90
where a.opCode = "12"
group by a.efamid, a.svcDate;
quit;

PGStats_0-1612676768221.png

 

PG
sas112
Calcite | Level 5
intck('day', a.svcDate, b.svcDate) between -60 and 90

is what I need. Thanks a lot.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 2644 views
  • 0 likes
  • 3 in conversation