BookmarkSubscribeRSS Feed
ttqkroe
Calcite | Level 5

Hi everyone, This is what the dataset looks like

IDDATEVALUE
12022-11-2010
12022-11-2210
22022-11-1220
22022-11-1820
22022-11-2520
32022-11-21 
32022-11-24 
32022-11-27 

 

I would like to have the result like this

IDDATEVALUEDIF
12022-11-2010 
12022-11-22102
22022-11-1220 
22022-11-18206
22022-11-252013
32022-11-21  
32022-11-24  
32022-11-27  

 

So if VALUE is same in the same group which is the ID, then I need get the difference of Date between the first date in the group and the other date in the group. And if VALUE is blank, then I do not need the difference. I have tried use "if value ^=. and value ne first.value"  then get value difference, but it will get wrong answer. Please help!

Thanks so much 

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID DATE :yymmdd10. VALUE;
infile datalines missover;
format DATE yymmdd10.;
datalines;
1 2022-11-20 10 
1 2022-11-22 10 
2 2022-11-12 20 
2 2022-11-18 20 
2 2022-11-25 20 
3 2022-11-21    
3 2022-11-24    
3 2022-11-27    
;

data want(drop = d);
   set have;
   by ID;
   if first.id then d = date;
   if VALUE & first.ID = 0 then dif = date - d;
   retain d;
run;

 

Result:

 

ID  DATE        VALUE  dif
1   2022-11-20  10     .
1   2022-11-22  10     2
2   2022-11-12  20     .
2   2022-11-18  20     6
2   2022-11-25  20     13
3   2022-11-21  .      .
3   2022-11-24  .      .
3   2022-11-27  .      .

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 241 views
  • 0 likes
  • 2 in conversation