How to do a subtraction between certain rows

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to do a subtraction between certain rows

Could somebody help me to deal with this problem, thanks so much.

 

Basicly, I had thousands of PIN, dates, and mark (which I pasted below). I need to subtract dates between first "new start <400" till last "keep start <400". For example, for PIN=100, I subtract last "keep start <400" 1/7/2010 - first "new start <400" 2/25/2009.

Then, I will need another subtraction for the same pin between "keep start <400" 11/24/2015-  "new start <400" 3/2/2011.

 

PINDATE_COLLECTEDmark
1002/25/2009new START <400
1006/8/2009keep START  <400
1001/7/2010keep START  <400
1003/2/2011new START  <400
1004/26/2011keep START  <400
1006/29/2011keep START  <400
10012/14/2011keep START  <400
1006/6/2012keep START  <400
10012/12/2012keep START  <400
1006/12/2013keep START  <400
10012/11/2013keep START  <400
1004/18/2014keep START  <400
10010/7/2014keep START  <400
10012/16/2014keep START  <400
1003/13/2015keep START  <400
10011/24/2015keep START  <400

 

Any suggestion would be appreciated.


Accepted Solutions
Solution
‎09-06-2017 09:48 AM
Super User
Posts: 6,632

Re: How to do a subtraction between certain rows

Posted in reply to linda0910

Try:

 

data want;

set have;

by PIN mark notsorted;

if first.PIN then baseline = . ;

retain baseline;

if mark = "new start <400" then baseline = date;

if last.mark and mark = "keep START  <400" then difference = date - baseline;

drop baseline;

run;

View solution in original post


All Replies
Solution
‎09-06-2017 09:48 AM
Super User
Posts: 6,632

Re: How to do a subtraction between certain rows

Posted in reply to linda0910

Try:

 

data want;

set have;

by PIN mark notsorted;

if first.PIN then baseline = . ;

retain baseline;

if mark = "new start <400" then baseline = date;

if last.mark and mark = "keep START  <400" then difference = date - baseline;

drop baseline;

run;

New Contributor
Posts: 2

Re: How to do a subtraction between certain rows

Posted in reply to Astounding

Thanks so much. That's exactly what I want. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 112 views
  • 0 likes
  • 2 in conversation