BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
elfkitty12
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

3 REPLIES 3
elfkitty12
Calcite | Level 5

occurence**  I can't spell!

Linlin
Lapis Lazuli | Level 10

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

elfkitty12
Calcite | Level 5

perfect!  thank you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is ANOVA?

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.

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