BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sghatak
Fluorite | Level 6

Hi,
I have data is following format
Id Date
1 201701
1 201711
2 201809
2 201812
3 201702
3 201708
3 201812

I need to group by ID's and find the difference between the dates, so my output looks like -
Id Date Difference
1 201701 0
1 201711 10
2 201809 0
2 201812 3
3 201702 0
3 201708 6
3 201812 4

data want;
_n_ =0;
do until(last.ID);
set have; by Id;
_n_+1;
if _n_ = _n_ - 1 then dateflag = date;
if _n_ = _n_ +1 then difference = dateflag - date;
end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input Id Date yymmn6.;
format date date9.;
cards;
1 201701
1 201711
2 201809
2 201812
3 201702
3 201708
3 201812
;

data want;
set have;
by id;
k=lag(date);
if first.id then want=0;
else want=intck('month',k,date);
run;

@sghatak 

3 201708 6
3 201812 4 

 

In your sample is incorrect . The year should be either 2017 or Difference should be 16. Please review and  verify

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data have;
input Id Date yymmn6.;
format date date9.;
cards;
1 201701
1 201711
2 201809
2 201812
3 201702
3 201708
3 201812
;

data want;
set have;
by id;
k=lag(date);
if first.id then want=0;
else want=intck('month',k,date);
run;

@sghatak 

3 201708 6
3 201812 4 

 

In your sample is incorrect . The year should be either 2017 or Difference should be 16. Please review and  verify

sghatak
Fluorite | Level 6
My Id is char, date is num, hence i am trying to subtract rows, thank you for quick response, appreciate it.
novinosrin
Tourmaline | Level 20

No problem with your ID being char or num. We aren't using that at all. Basically your date is a numeric date and what i understand is you are trying to compute the month intervals between two dates, hence I used the intck function. Please test. 

 

I really hope you are understanding the code and mostly importantly your requirement. 

 

 


@sghatak wrote:
My Id is char, date is num, hence i am trying to subtract rows, thank you for quick response, appreciate it.

 

sghatak
Fluorite | Level 6
I got it. I modified the code to just the difference between k and date, hence i get the numbers i was hoping for, thank you, this was a simple solution rather than doing Do loop; appreciate timely help.
sghatak
Fluorite | Level 6

one more thing forgot to add, i needed to group by Id's , so for iD = 1 subtract, ID=2, subtract until last.ID. Anyway , have to loop it for this. Thanks.

novinosrin
Tourmaline | Level 20

what do you mean?

 

It's a straight forward current-lag(current) within an id(by group)

 

Relax. Do not confuse 🙂

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1335 views
  • 4 likes
  • 2 in conversation