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

Hi there,

 

Please see below is what I try to get:

ID visit  want  
01 30 0  
01 34 4  = 34-30
01 39 5 = 39-34
01 45 6 =  45-39
02 35 0 = 35-35
02 40 5 = 40-35
02 42 2 = 42-40
       

 

How to code in SAS to get the value in "want"?

any help will be very appriciate.

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

 

data have;
input ID 	visit;
cards;
01 	30 	
01 	34 	
01 	39 	
01 	45 	
02 	35 	
02 	40 	
02 	42 	
;

data want;
set have;
by id visit;
want=visit-lag(visit);
if first.id then want=0;
run;
Thanks,
Jag

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

 

data have;
input ID 	visit;
cards;
01 	30 	
01 	34 	
01 	39 	
01 	45 	
02 	35 	
02 	40 	
02 	42 	
;

data want;
set have;
by id visit;
want=visit-lag(visit);
if first.id then want=0;
run;
Thanks,
Jag
ballardw
Super User

Minor change suggestion to @Jagadishkatam

data want;
   set have;
   by id;
   want = dif(visit);
   if first.id then want=0;
run;

The Dif function is the equivalent of : var- lag(var);

 

ursula
Pyrite | Level 9

Thanks so much!!

it's very helpful!

ursula
Pyrite | Level 9

Thank you so much for  your prompt solution response.

I appreciated it.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 751 views
  • 4 likes
  • 3 in conversation