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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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