I have a dataset that looks like the following:
ID Use_Date Other Variables
Bob 14Jan2012
Bob 24Jan2012
Bob 31Jan2012
Joe 13Jan2012
Joe 25Jan2012
Phil 10Jan2012
Phil 11Jan2012
Phil 23Jan2012
I want to create a new variable, say fixed_date. Fixed_date will be zero for the earliest occuring observation (for each individual). For the other observations (for each individual), fixed_date will be the # of days after that earliest occurance. So I want it to look like this:
ID Use_Date Fixed_Date
Bob 14Jan2012 0
Bob 24Jan2012 10
Bob 31Jan2012 17
Joe 13Jan2012 0
Joe 25Jan2012 12
Phil 10Jan2012 0
Phil 11Jan2012 1
Phil 23Jan2012 13
Can anyone help me do this? Thank you!!!
data have;
input ID $ Use_Date : date9.;
format use_date date9.;
cards;
Bob 14Jan2012
Bob 24Jan2012
Bob 31Jan2012
Joe 13Jan2012
Joe 25Jan2012
Phil 10Jan2012
Phil 11Jan2012
Phil 23Jan2012
;
proc sort data=have;by id use_date;
data want;
retain d1;
set have;
by id;
if first.id then do; d1=use_date;fixed_date=0;end;
else fixed_date=use_date-d1;
drop d1;
proc print;run;
fixed_
Obs ID Use_Date date
1 Bob 14JAN2012 0
2 Bob 24JAN2012 10
3 Bob 31JAN2012 17
4 Joe 13JAN2012 0
5 Joe 25JAN2012 12
6 Phil 10JAN2012 0
7 Phil 11JAN2012 1
8 Phil 23JAN2012 13
occurence** I can't spell!
data have;
input ID $ Use_Date : date9.;
format use_date date9.;
cards;
Bob 14Jan2012
Bob 24Jan2012
Bob 31Jan2012
Joe 13Jan2012
Joe 25Jan2012
Phil 10Jan2012
Phil 11Jan2012
Phil 23Jan2012
;
proc sort data=have;by id use_date;
data want;
retain d1;
set have;
by id;
if first.id then do; d1=use_date;fixed_date=0;end;
else fixed_date=use_date-d1;
drop d1;
proc print;run;
fixed_
Obs ID Use_Date date
1 Bob 14JAN2012 0
2 Bob 24JAN2012 10
3 Bob 31JAN2012 17
4 Joe 13JAN2012 0
5 Joe 25JAN2012 12
6 Phil 10JAN2012 0
7 Phil 11JAN2012 1
8 Phil 23JAN2012 13
perfect! thank you!
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!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.