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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1126 views
  • 0 likes
  • 2 in conversation