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  .      .

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1 reply
  • 431 views
  • 0 likes
  • 2 in conversation