BookmarkSubscribeRSS Feed
ccnky123
Calcite | Level 5

The original table is like:

cst_iddate
A012015/5/6 9:13:59
A022015/5/7 10:09:8
A012015/5/7 15:12:02
A022015/5/9 15:09:45
A012015/5/12 23:07:33 
A02 2015/5/13 14:03:16

and finally get:(the real table for column date may be disorder)

cst_iddatedate1interval
A012015/5/6 9:13:59 06may2015 9:13:59 .
A012015/5/7 15:12:02 07may2015 15:12:02 107863
A012015/5/12 23:07:33  12may2015 23:07:33 460531
A022015/5/7 10:09:01 07may2015  10:09:01 .
A022015/5/9 15:09:23 09may2015 15:09:23 190837
A02 2015/5/13 14:03:16 13may2015 14:03:16 341611

 

Data step will be preferable! thank you for your attention 

2 REPLIES 2
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

proc sort data=have;

by cst_id date;

run;

now do the calculations using lag.

 

Kurt_Bremser
Super User

@ccnky123 wrote:

The original table is like:

cst_id date
A01 2015/5/6 9:13:59
A02 2015/5/7 10:09:8
A01 2015/5/7 15:12:02
A02 2015/5/9 15:09:45
A01 2015/5/12 23:07:33 
A02  2015/5/13 14:03:16

and finally get:(the real table for column date may be disorder)

cst_id date date1 interval
A01 2015/5/6 9:13:59  06may2015 9:13:59  .
A01 2015/5/7 15:12:02  07may2015 15:12:02  107863
A01 2015/5/12 23:07:33   12may2015 23:07:33  460531
A02 2015/5/7 10:09:01  07may2015  10:09:01  .
A02 2015/5/9 15:09:23  09may2015 15:09:23  190837
A02  2015/5/13 14:03:16  13may2015 14:03:16  341611

 

Data step will be preferable! thank you for your attention 


Use the scan() fubction to dissect yor "date" string. input() the first "word" with yymmdd10., and the second with time8.

Then build the datetime value with dhms(date,0,0,time).

After that, use lag() as advised.

For code, provide your example data in usable form (data step with datalines).

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