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

Dear all,

I have the following data:

 

Year         GVKEY        CDL          Date                 Exposure

2005            1                 5             02-12-2005           10

2005            2                 2              02-12-2005           10

2005            3                 1              02-12-2005           55

 

2006            1                 4              02-12-2006            20

2006             2                3              02-12-2006            40

2006            2                 3              02-11-2006            40

2006            2                 1              02-12-2006             10

 

I need to know the total and average exposure per year. But if the GVKEY = CDL, I only want to report one exposure for the latest date observed. That is, for 2006, it should be on 02-12-2006. So my final output should look like this:

 

Year         GVKEY        CDL          Date                 Exposure

2005            1                 5              02-12-2005           10

2005            2                 2              02-12-2005           10

2005            3                 1              02-12-2005           55

 

2006            1                 4              02-12-2006            20

2006            2                3              02-12-2006            40

2006            2                 1              02-12-2006            10

 

So I am not sure how to write this code. Of course, dates can vary. But this is an idea. And then I need to calculate total exposure and average exposure per each year.

 

Does anyone know how the code would look like? Thank you for your help.

 

Yelena

1 ACCEPTED SOLUTION

Accepted Solutions
sh0e
Obsidian | Level 7

Sounds like you want CDL in the sort.  For example,

 

proc sort data = in;
  by year GVKEY CDL date;run;

data out;  
  set in;  
  by year GVKEY CDL;  
  if last.CDL;  
  run;

View solution in original post

4 REPLIES 4
sh0e
Obsidian | Level 7

Will this work for you?

 

proc sort data = in;
  by year GVKEY date;
run;

data out;
set in;
by year GVKEY;
if last.GVKEY;
run;
yelena
Fluorite | Level 6

Thank you, but it does not work. It completely eliminates the row for 2006 with the following entries:

 

2006 2 3 02-12-2006 40

 

 

sh0e
Obsidian | Level 7

Sounds like you want CDL in the sort.  For example,

 

proc sort data = in;
  by year GVKEY CDL date;run;

data out;  
  set in;  
  by year GVKEY CDL;  
  if last.CDL;  
  run;
yelena
Fluorite | Level 6

Thank you! It works now!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1268 views
  • 0 likes
  • 2 in conversation