BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Gil_
Quartz | Level 8
I have a data set that changes start time to 8 am if its after hours.
Data table;
Set table;
Starttime=opendate;
If timepart(starttime)=>^22:00' t then starttime=dhms(datepart(INTX'dtday', 1,),8,0,0) ;
Format starttime datetime20.;run;

I need the time to go from=> '22:00. Thru =<08:00 next morning.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Maxim 3: read the log. Your code contains several severe syntax errors (blank between time literal and the t, missing brackets, intx instead of intnx, missing arguments to intnx function).

Run this:

data table;
input opendate datetime22.;
format opendate datetime22.;
cards;
09aug2018:22:10:00
09aug2018:21:50:00
;
run;

data table_new;
set table;
if timepart(opendate) => '22:00't
then starttime = dhms(datepart(intnx('dtday',starttime,1)),8,0,0);
else starttime = opendate;
format starttime datetime20.;
run

Correction: The code needs to care for hours before 08:00, also:

data table_new;
set table;
if timepart(opendate) => '22:00't
then starttime = dhms(datepart(intnx('dtday',opendate,1)),8,0,0);
else starttime = max(opendate,dhms(datepart(intnx('dtday',opendate,0)),8,0,0));
format starttime datetime20.;
run;

@Gil_ wrote:
I have a data set that changes start time to 8 am if its after hours.
Data table;
Set table;
Starttime=opendate;
If timepart(starttime)=>^22:00' t then starttime=dhms(datepart(INTX'dtday', 1,),8,0,0) ;
Format starttime datetime20.;run;

I need the time to go from=> '22:00. Thru =<08:00 next morning.


View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Maxim 3: read the log. Your code contains several severe syntax errors (blank between time literal and the t, missing brackets, intx instead of intnx, missing arguments to intnx function).

Run this:

data table;
input opendate datetime22.;
format opendate datetime22.;
cards;
09aug2018:22:10:00
09aug2018:21:50:00
;
run;

data table_new;
set table;
if timepart(opendate) => '22:00't
then starttime = dhms(datepart(intnx('dtday',starttime,1)),8,0,0);
else starttime = opendate;
format starttime datetime20.;
run

Correction: The code needs to care for hours before 08:00, also:

data table_new;
set table;
if timepart(opendate) => '22:00't
then starttime = dhms(datepart(intnx('dtday',opendate,1)),8,0,0);
else starttime = max(opendate,dhms(datepart(intnx('dtday',opendate,0)),8,0,0));
format starttime datetime20.;
run;

@Gil_ wrote:
I have a data set that changes start time to 8 am if its after hours.
Data table;
Set table;
Starttime=opendate;
If timepart(starttime)=>^22:00' t then starttime=dhms(datepart(INTX'dtday', 1,),8,0,0) ;
Format starttime datetime20.;run;

I need the time to go from=> '22:00. Thru =<08:00 next morning.


Patrick
Opal | Level 21

@Gil_

If I understand right what you're after then below code should do the job.

data have;
  infile datalines truncover;
  input dttm datetime.;
  format dttm datetime21.;
  datalines;
08Aug2016 21:59:59
08Aug2016 22:00:00
08Aug2016 22:00:01
08Aug2016 23:59:59
09Aug2016 00:00:00
09Aug2016 00:00:01
09Aug2016 07:59:59
09Aug2016 08:00:00
09Aug2016 08:00:01
;
run;

data want;
  set have;
  format dttm_work datetime21.;
  dttm_work=ifn(hour(intnx('dthour',dttm,2,'s'))<10, 
                intnx('dthour',intnx('dtday',intnx('dthour',dttm,2,'s'),0,'b'),8,'s'), 
                dttm
                );
run;
proc print data=want;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 2 replies
  • 1931 views
  • 0 likes
  • 3 in conversation