DATA Step, Macro, Functions and more

Increment datetime by 6hours

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

Increment datetime by 6hours

Hi There,

I want to prepare dataset which has values for every 6 hours.

 

data new;
input loop dsstartdate ;
datalines;
1 1827705600
;

run;

data new2;
set new;

format dsstartdate datetime25.6;
run;

 

 

 

so my data looks like this 

 

Loop   DSSTARTDATE

1        01DEC2017:00:00:00.000000

 

And this is how I want 

 

1 01DEC2017:00:00:00.000000
2 01DEC2017:06:00:00.000000
3 01DEC2017:12:00:00.000000
4 01DEC2017:18:00:00.000000
1 02DEC2017:00:00:00.000000
2 02DEC2017:06:00:00.000000
3 02DEC2017:12:00:00.000000
4 02DEC2017:18:00:00.000000
1 03DEC2017:00:00:00.000000
2 03DEC2017:06:00:00.000000
3 03DEC2017:12:00:00.000000
4 03DEC2017:18:00:00.000000

 

So Basically A loop through all the dates by every 6 hours. I was using intnx in a do loop

However somehow cant be able to crack it. 

Anyhelp is appreaciated


Accepted Solutions
Solution
‎12-09-2017 02:25 AM
Esteemed Advisor
Posts: 5,482

Re: Increment datetime by 6hours

Posted in reply to yashpande

A loop like this would work:

 

do dt = DSSTARTDATE to DSENDDATE by '06:00:00't;
    output;
    end;
PG

View solution in original post


All Replies
Super User
Posts: 23,295

Re: Increment datetime by 6hours

Posted in reply to yashpande

Post the code you’ve tried please. 

Solution
‎12-09-2017 02:25 AM
Esteemed Advisor
Posts: 5,482

Re: Increment datetime by 6hours

Posted in reply to yashpande

A loop like this would work:

 

do dt = DSSTARTDATE to DSENDDATE by '06:00:00't;
    output;
    end;
PG
Contributor
Posts: 52

Re: Increment datetime by 6hours

Thanks for the clue. Worked like a charm. Just in case if anyone needs the code.

 

data have;
input dsstartdate dsenddate;
cards;
1827705600 1861920000
;
run;

data want;
set have;
format dsstartdate dsenddate datetime25.6;
do dt = DSSTARTDATE to DSENDDATE by '06:00:00't;
if timepart(dt)=0 then
counter=1;
if timepart(dt)=21600 then
counter=2;
if timepart(dt)=43200 then
counter=3;
if timepart(dt)=64800 then
counter=4;
format dt datetime25.6;
output;
end;

keep dt counter;
run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 189 views
  • 1 like
  • 3 in conversation