Programming the statistical procedures from SAS

Changing dates by Individuals

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Changing dates by Individuals

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!!!


Accepted Solutions
Solution
‎01-13-2013 05:11 PM
Super Contributor
Posts: 1,636

Re: Changing dates by Individuals

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

View solution in original post


All Replies
Occasional Contributor
Posts: 9

Re: Changing dates by Individuals

occurence**  I can't spell!

Solution
‎01-13-2013 05:11 PM
Super Contributor
Posts: 1,636

Re: Changing dates by Individuals

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

Occasional Contributor
Posts: 9

Re: Changing dates by Individuals

perfect!  thank you!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 157 views
  • 1 like
  • 2 in conversation