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!
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
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
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.