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

Hello,

 

I have a sample dataset to which I need to append current date and time(hh:mm) with interval of 30 minutes.  The challenge is to break the total records into groups of 30 minutes between 9 am and 3 pm. So for example I have 1000 records in my data and I want to randomly take  ~77(1000/13)  and assign 05/16/2020 09:00 and the next batch of random 77 would get 05/16/2020 09:30 and the next would get 05/16/2020 10:00  and so on till 05/16/2020 15:00

 

Sample data

Data have;
do i = 1 to 1000;
ID = 123 * i;
ID_time = put(today(),mmddyys10.) ||''||put(time(),hhmm.);
drop i;
output;
end;
run;

 Sample output:

ID ID_time
123 5/16/2020 9:00
246 5/16/2020 9:00
369 5/16/2020 9:00
492 5/16/2020 9:00
615 5/16/2020 9:00
738 5/16/2020 9:00
861 5/16/2020 9:30
984 5/16/2020 9:30
1107 5/16/2020 9:30
1230 5/16/2020 9:30
1353 5/16/2020 9:30
1476 5/16/2020 9:30
1599 5/16/2020 10:00
1722 5/16/2020 10:00
1845 5/16/2020 10:00
1968 5/16/2020 10:00
2091 5/16/2020 10:00
2214 5/16/2020 10:00

 

Thanks for your time and help!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@vicky07 wrote:

Thank you for your reply. Quick follow up questions, how do i change my date to 05/16/2020 format in the final output. Also, I want to display current date in my output instead of using hard coded date.   Cold you please help with changing the date format and adding current date? 

 

Thanks!


data want;
    set have2;
    group=floor((_n_-1)/77);
    today=today();
    hour='09:00't+1800*group;
    date_plus_time=catx(' ',put(today,mmddyys10.),put(hour,time5.));
    drop today hour group;
run;
--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

You would assign random numbers to each ID. Then once that is done, you can assign to the first 77 the date and time 05/16/2020 09:00, and so on.

 

data have2;
    set have;
    y=rand('uniform');
run;
proc sort data=have2;
    by y;
run;
data want;
    set have2;
    group=floor((_n_-1)/77);
    time = '16MAY2020:09:00:00'dt+30*60*group;
    format time datetime16.;
run;

 

 

 

 

--
Paige Miller
vicky07
Quartz | Level 8

Thank you for your reply. Quick follow up questions, how do i change my date to 05/16/2020 format in the final output. Also, I want to display current date in my output instead of using hard coded date.   Cold you please help with changing the date format and adding current date? 

 

Thanks!

PaigeMiller
Diamond | Level 26

@vicky07 wrote:

Thank you for your reply. Quick follow up questions, how do i change my date to 05/16/2020 format in the final output. Also, I want to display current date in my output instead of using hard coded date.   Cold you please help with changing the date format and adding current date? 

 

Thanks!


data want;
    set have2;
    group=floor((_n_-1)/77);
    today=today();
    hour='09:00't+1800*group;
    date_plus_time=catx(' ',put(today,mmddyys10.),put(hour,time5.));
    drop today hour group;
run;
--
Paige Miller
vicky07
Quartz | Level 8
This is perfect. Thank you very much!!!
RichardDeVen
Barite | Level 11

 

The SURVEYSELECT Procedure provides a variety of methods for selecting probability-based random samples.

Example:

data have;
  do id = 1 to 1000;
    output;
  end;
run;

proc surveyselect noprint data=have out=groups groups=13;
run;

data want;
  set groups;
  timestamp = dhms(today(),8,30,0) + groupId * 1800;
  format timestamp dateAMPM.;
run;

proc freq data=want;
  table timestamp;
run;

Output

 

freq2.png

vicky07
Quartz | Level 8
Thank you very much,good to learn survey select method.
RichardDeVen
Barite | Level 11
Just being picky, aren't there 12 intervals whose time ranges fall BETWEEN 9AM and 3PM. The 13th, or 3:00PM one ends at 3:30
vicky07
Quartz | Level 8
You are right, the 3.00 pm one ends at 3.30.

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
  • 8 replies
  • 1732 views
  • 1 like
  • 3 in conversation