DATA Step, Macro, Functions and more

subsequest values

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

subsequest values

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!


Accepted Solutions
Solution
‎07-12-2017 12:19 PM
Trusted Advisor
Posts: 1,137

Re: subsequest values

 

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


All Replies
Solution
‎07-12-2017 12:19 PM
Trusted Advisor
Posts: 1,137

Re: subsequest values

 

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
Super User
Posts: 11,343

Re: subsequest values

Posted in reply to Jagadishkatam

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);

 

Contributor
Posts: 67

Re: subsequest values

Thanks so much!!

it's very helpful!

Contributor
Posts: 67

Re: subsequest values

Posted in reply to Jagadishkatam

Thank you so much for  your prompt solution response.

I appreciated it.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 104 views
  • 4 likes
  • 3 in conversation