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

Hi All, 

 

I would like to assign a random date for each observation. There are two conditions to fulfill.

1. The assign date has to be between Jan 1, 2015 and Dec 31, 2015. 

2. The assign date has to be after each observation's start date. 

 

ID            Start_Date             

1              01JAN2015

2              01JUN2015

3              31DEC2015

 

Thank you! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use the RAND() function with 'INTEGER' type.

data have ;
  input id Start_Date :date.;
  format start_date date9.;
cards;
1              01JAN2015
2              01JUN2015
3              31DEC2015
;

data want;
  set have;
  stop_date = rand('integer',max(start_date,"01JAN2015"d),"31DEC2015"d);
  format stop_date date9.;
run;

proc print;
run;

Results:

               Start_
Obs    id         Date    stop_date

 1      1    01JAN2015    05OCT2015
 2      2    01JUN2015    10AUG2015
 3      3    31DEC2015    31DEC2015

View solution in original post

7 REPLIES 7
di_niu0
Obsidian | Level 7
For ID 3, the assign date is Dec 31, 2015.
Tom
Super User Tom
Super User

Use the RAND() function with 'INTEGER' type.

data have ;
  input id Start_Date :date.;
  format start_date date9.;
cards;
1              01JAN2015
2              01JUN2015
3              31DEC2015
;

data want;
  set have;
  stop_date = rand('integer',max(start_date,"01JAN2015"d),"31DEC2015"d);
  format stop_date date9.;
run;

proc print;
run;

Results:

               Start_
Obs    id         Date    stop_date

 1      1    01JAN2015    05OCT2015
 2      2    01JUN2015    10AUG2015
 3      3    31DEC2015    31DEC2015
di_niu0
Obsidian | Level 7
That works well. Thank you!
Kurt_Bremser
Super User

Use the RAND function with the INTEGER distribution:

data have;
input ID $ Start_Date :date9.;
format start_date yymmdd10.;
datalines;             
1              01JAN2015
2              01JUN2015
3              31DEC2015
;

data want;
set have;
assign_date = rand('integer',max(start_date,'01jan2015'd),'31dec2015'd);
format assign_date yymmdd10.;
run;
di_niu0
Obsidian | Level 7
Thank you!
PaigeMiller
Diamond | Level 26
data have; 
input ID Start_Date :date9.;   
cards; 
1              01JAN2015
2              01JUN2015
3              31DEC2015
;

data want;
    set have;
    end_date='31DEC2015'd;
    number_of_days_start_to_end=end_date-start_date+1;
    random_date=start_date+rand('integer',0,number_of_days_start_to_end);
    format random_date date9.;
run;
--
Paige Miller
di_niu0
Obsidian | Level 7
Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1638 views
  • 7 likes
  • 4 in conversation