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

Hi All,

 

I am trying to get the difference between the dates (quarters) in consequent rows.  I have the following data: 

 

data have;
input ID year $ quarter $;
cards;

1 2012 2
1 2015 1
2 2011 1
2 2012 3
2 2015 4
3 2012 2
3 2013 3
4 2014 1
4 2014 2
5 2014 2
5 2015 4
;
run;

data have;
set have;
date=input(cats(year,'Q',quarter),yyq6.);
format date yyq6.;
run;

 

Tabel desired:

 

ID year quarter date difference
2012 2012Q2.
20152015Q15
20112011Q1 .
2012 2012Q3 6
2015 2015Q49
32012    2012Q2 .
320132013Q3 5
20142014Q1 .
2014 2014Q2 1
2014 22014Q2 .
520152015Q1

2

 

 

Thanks.

Sandeep

 

 

 

1 ACCEPTED SOLUTION
4 REPLIES 4
sandyzman1
Obsidian | Level 7

KurtBremser,

 

Thanks for your solution. It worked.

 

For the future reference, I used following code:


data temp;
set temp;
by ID;
lag1_value = lag(date);
if first.id then lag1_value = .;
format lag1_value yyq6.;
run;


data temp1;
set temp;
quarters=intck('qtr',lag1_value, date);
run;

 

Thanks.

andreas_lds
Jade | Level 19

The values you want as difference don't match what i would expect, please explain. The difference between 2012Q2 and 2015Q1 should be 11

sandyzman1
Obsidian | Level 7
Apology for the typo !! You are correct. It should have been 11 instead of 5. Thanks.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 592 views
  • 0 likes
  • 3 in conversation