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

Hello,

Example of data:

 

ID        Date                            amount_of_days

1          01-01-2017                

1          04-01-2017                

1          12-01-2017                 12

2          01-04-2017

2          01-05-2017                 31

3          01-02-2018

3          05-02-2018

3          11-02-2018                            

3          21-02-2018                 10

 

is it  possible to calculate the amount of days between the last and second-last date within the ID?

 

Thank you very much for any comments.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Well, your formula is not consistent.  Do you want to count both the first and last day in the total number of days?  At any rate, you can certainly do this:

 

data want;

set have;

by id;

amount_of_days = dif(date);

if first.id=1 then amount_of_days = 0;

run;

 

But it's not clear from your example if  you want to use:

 

amount_of_days = dif(date) + 1;

 

Test the formulas, to see which one is right for you.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

Your dates have to be actual SAS dates, not character strings.  As long as your data is in order by ID:

 

data want;

set have;

by id;

amount_of_days = dif(date);

if last.id=0 or first.id=1 then amount_of_days = .;

run;

HansSteenhuis
Calcite | Level 5

thank you very much, it works 🙂

 

Is is also possible to calculate the amount of days like this Example:

 

Example of data:

 

ID        Date                            number_of_days

1          01-01-2017                 0

1          04-01-2017                 4

1          12-01-2017                 9

2          01-04-2017                 0

2          01-05-2017                 2

3          01-02-2018                 276

3          05-02-2018                 5

3          11-02-2018                 7         

3          21-02-2018                 11

Reeza
Super User

Use the DIF() function and then set it to 0 at the first of each group using FIRST.

 


@HansSteenhuis wrote:

thank you very much, it works 🙂

 

Is is also possible to calculate the amount of days like this Example:

 

Example of data:

 

ID        Date                            number_of_days

1          01-01-2017                 0

1          04-01-2017                 4

1          12-01-2017                 9

2          01-04-2017                 0

2          01-05-2017                 2

3          01-02-2018                 276

3          05-02-2018                 5

3          11-02-2018                 7         

3          21-02-2018                 11


 

Astounding
PROC Star

Well, your formula is not consistent.  Do you want to count both the first and last day in the total number of days?  At any rate, you can certainly do this:

 

data want;

set have;

by id;

amount_of_days = dif(date);

if first.id=1 then amount_of_days = 0;

run;

 

But it's not clear from your example if  you want to use:

 

amount_of_days = dif(date) + 1;

 

Test the formulas, to see which one is right for you.

HansSteenhuis
Calcite | Level 5
Thanks you. I now also use this code for the calculation

data want;
set have;
by id;
amount_of_days = dif(date);
if first.id=1 then amount_of_days = 0;
run;
HansSteenhuis
Calcite | Level 5
Thank you. I also use this code for the calculation

data want;
set have;
by id;
amount_of_days = dif(date);
if last.id=0 or first.id=1 then amount_of_days = .;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1770 views
  • 0 likes
  • 3 in conversation