Skip to content

Instantly share code, notes, and snippets.

@rudvfaden
Created June 3, 2021 09:04
Show Gist options
  • Save rudvfaden/2b08bd60277d2ae40c0e67ac4f29bbc7 to your computer and use it in GitHub Desktop.
Save rudvfaden/2b08bd60277d2ae40c0e67ac4f29bbc7 to your computer and use it in GitHub Desktop.
Tester om start og slutdato ligger i et tidsinterval
data have;
infile cards dsd dlm=',';
attrib id format=$1.;
attrib start informat=datetime20. format=datetime20.;
attrib slut informat=datetime20. format=datetime20.;
input id start slut;
cards;
1, 01-may-2020 08:00:00, 01-may-2020 12:00:00
1, 01-may-2020 09:00:00, 01-may-2020 10:00:00
1, 01-may-2020 13:00:00, 01-may-2020 22:00:00
2, 01-may-2020 14:00:00, 01-may-2020 16:00:00
2, 01-may-2020 18:00:00, 01-may-2020 20:00:00
3, 01-may-2020 08:00:00, 01-may-2020 16:00:00
;
run;
*tjekker for hver time i døgnet om starttidspunkt er mindre en
sluttidspunkt. Hvis sand sættes aktiv=1 ellers aktiv=0;
data timer;
set have;
do dage=datepart(start) to datepart(slut);
do timer=1 to 24 by 1;
aktiv = (max(start, dhms(dage, timer,-1,0)) < min(slut, dhms(dage, timer,0,0)));
tidspunkt=dhms(dage, timer-1,0,0);
if aktiv=1 then output;
end;
end;
format tidspunkt datetime.;
run;
*tæller unikke aktive for hvert interval;
proc sql;
create table stat_timer
as select tidspunkt, aktiv, count(distinct id) as antal
from timer
group by tidspunkt
;
quit;
*Samme som overstående. Bare hvor intervallet er et minut;
data minutter;
set have;
do dage=datepart(start) to datepart(slut);
do timer=1 to 24 by 1;
do minutter=1 to 60;
aktiv = (max(start, dhms(dage, timer,minutter,-1)) < min(slut, dhms(dage, timer,minutter,0)));
tidspunkt=dhms(dage, timer,minutter-1,0);
if aktiv=1 then output;
end;
end;
end;
format tidspunkt datetime.;
run;
proc sql;
create table stat_minutter
as select tidspunkt, aktiv, count(distinct id) as antal
from minutter
group by tidspunkt
;
quit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment