Hi SAS community-
I would like to retain multiple variables within groups. I can figure out how to retain 1 variable, but cannot get my code to do the 2nd variable in the way that I would like within groups. Here's current sas code along with my 'want' dataset. My starting dataset has just the 3 variables: id, count and date.
Thanks!
proc sort data=have; by id;
data want;
set have;
by id;
retain date_r1-date_r3;
if first.id then date_r1=date;
????
run;
data have;
input id count date date_r1 date_r2 date_r3;
datalines;
aa 1 01/20/2014 . . .
aa 2 03/24/2015 01/20/2014 . .
aa 3 04/23/2015 01/20/2014 03/24/2015 .
bb 1 10/23/2013 . . .
bb 2 03/24/2015 10/23/2013 . .
cc 3 08/10/2014 . . .
dd . . . . .
dd 2 05/19/2014 . . .
ee 1 03/02/2014 . . .
ee 2 03/19/2013 03/02/2014 . .
ee 3 04/23/2015 03/02/2014 03/19/2013 .
ee 4 10/12/2015 03/02/2014 03/19/2013 04/23/2015
Not easy to understand the question. Maybe this:
data have;
input id $ count date :mmddyy10.;
format date yymmdd10.;
datalines;
aa 1 01/20/2014
aa 2 03/24/2015
aa 3 04/23/2015
bb 1 10/23/2013
bb 2 03/24/2015
cc 3 08/10/2014
dd . .
dd 2 05/19/2014
ee 1 03/02/2014
ee 2 03/19/2013
ee 3 04/23/2015
ee 4 10/12/2015
;
data want;
array date_r{3};
format date_r: yymmdd10.;
do i = 1 by 1 until(last.id);
set have; by id;
output;
if i <= dim(date_r) then date_r{i} = date;
end;
drop i;
run;
proc print noobs; var id count date date_r1-date_r3; run;
Not sure without seeing the desired data set ( WANT). Simple use of "????" does not show what you need. Will it work if you use a DO - satement like:
if first.id then DO;
date_r1=date;
date_r2 = date; /* NOT SURE WHAT IS TO BE ASSIGNED */
date_r3 = date; /* NOT SURE WHAT IS TO BE ASSIGNED */
END;
It is not clear what output you want.
Ususally I declare RETAIN with new-temporary variable names.
It is useles to declare retain of input variable name, like yours date_r1 - date_r3.
Anyway, you can assign more then one retained variable, using DO statement,
something like:
data want;
set have;
by id;
retain var1 - var3;
if first.id then do;
var1 = date_r1;
var2 = date_r2;
var3 = date_r3;
end;
else do; /* 2nd to last obs in a group */
......
end;
if last.id then output; /* or any other function(s) */
run;
Please post yout desired output, in order to get more efficient code.
Not easy to understand the question. Maybe this:
data have;
input id $ count date :mmddyy10.;
format date yymmdd10.;
datalines;
aa 1 01/20/2014
aa 2 03/24/2015
aa 3 04/23/2015
bb 1 10/23/2013
bb 2 03/24/2015
cc 3 08/10/2014
dd . .
dd 2 05/19/2014
ee 1 03/02/2014
ee 2 03/19/2013
ee 3 04/23/2015
ee 4 10/12/2015
;
data want;
array date_r{3};
format date_r: yymmdd10.;
do i = 1 by 1 until(last.id);
set have; by id;
output;
if i <= dim(date_r) then date_r{i} = date;
end;
drop i;
run;
proc print noobs; var id count date date_r1-date_r3; run;
Thank you! This is exactly what I was looking for in the code. Apologies for the lack of clarity in my initial post.
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 25. Read more here about why you should contribute and what is in it for you!
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.