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
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;
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;
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
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;
Thank you! It works now!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.