-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtime.sas
207 lines (161 loc) · 6.32 KB
/
time.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
%macro time(period,note,log=N,out=Log_time,diff=N) / des='Insert current time';
/****************************************************************************
BEGIN MACRO HEADER
****************************************************************************
Name: Time
Author: Chris Swenson
Created: 2010-09-16
Purpose: Insert and evaluate time variables
Arguments: period - Period, either B (Begin), M (Midpoint), or E (End)
note - Note to output with period time
log= - Y/N flag to indicate whether to maintain a log of the
periods and messages
out= - name of log output data set, defaults to Log_time
diff= - Y/N flag to indicate whether to output a macro
variable, named diff_period, containing the differences
between periods
Revisions
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Date Author Comments
¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯
YYYY-MM-DD III Please use this format and insert new entries above
****************************************************************************
END MACRO HEADER
****************************************************************************/
/****************************************************************************
Settings
****************************************************************************/
/* Set to upper case */
%if "&PERIOD" ne "" %then %let period=%substr(%upcase(&period), 1, 1);
%let log=%substr(%upcase(&log), 1, 1);
/* Check arguments */
%if %index(*B*E*M*,*&PERIOD*)=0 %then %do;
%put %str(E)RROR: %str(I)nvalid period specified. Please use B (Begin), M (Midpoint), or E (End).;
%return;
%end;
%if "&log" ne "" %then %do;
%if %index(*N*Y*,*&LOG*)=0 %then %do;
%put %str(E)RROR: %str(I)nvalid log argument specified. Please use Y or N.;
%return;
%end;
%end;
%if %index(*N*Y*,*&DIFF*)=0 %then %do;
%put %str(E)RROR: %str(I)nvalid DIFF= argument specified. Please use Y or N.;
%return;
%end;
/* Check for prior midpoint and retain */
%local prior_mid;
%if &period=M and %symexist(mid_datetime) %then %let prior_mid=&mid_datetime;
/* Set the period argument to the expanded name */
%if %upcase(&period)=B %then %do;
%let period=Beg;
%let period_long=Beginning;
%end;
%else %if %upcase(&period)=M %then %do;
%let period=Mid;
%let period_long=Mid Point;
%end;
%else %if %upcase(&period)=E %then %do;
%let period=End;
%let period_long=End;
%end;
/* Add "at" to note argument */
%if "¬e" ne "" %then %let dash=-;
%else %let dash=;
/* Manage scope */
%global &period._datetime;
/****************************************************************************
Set Specified Period
****************************************************************************/
/* Set the time and date macro variables and output to the log */
%let &period._datetime=%sysfunc(datetime(), datetime20.);
/* Output note */
%local time;
%let time=NOTE: &period_long Time: &&&period._datetime &dash ¬e;
/****************************************************************************
Calculate Differences
****************************************************************************/
%local flag_s flag_m flag_e dif_s dif_m;
%macro TimeCalc(datetime1,datetime2,msgname,diff=N) / des="Calculate difference in datetimes";
%if "&datetime1"="" %then %do;
%put %str(E)RROR: The first argument is blank.;
%return;
%end;
%if "&datetime2"="" %then %do;
%put %str(E)RROR: The second argument is blank;
%return;
%end;
%if "&msgname"="" %then %let msgname=TimeDiff;
%if %index(*N*Y*,*&DIFF*)=0 %then %do;
%put %str(E)RROR: %str(I)nvalid DIFF= argument specified. Please use Y or N.;
%return;
%end;
%global &msgname;
/* Calculate difference */
%local calc dys hrs min sec;
%let calc=%eval(%sysfunc(inputn(&datetime2, datetime20.))-%sysfunc(inputn(&datetime1, datetime20.)));
%if &DIFF = Y %then %do;
%global diff_period;
%let diff_period = &CALC;
%end;
/* Split differences */
%let dys=%eval(&calc/86400);
%let calc=%eval(&calc-(&dys*86400));
%let hrs=%eval(&calc/3600);
%let calc=%eval(&calc-(&hrs*3600));
%let min=%eval(&calc/60);
%let calc=%eval(&calc-(&min*60));
%let sec=&calc;
/* Set note */
%let &msgname=&dys days, &hrs hours, &min minutes, and &sec seconds;
%mend TimeCalc;
%if &period=Mid %then %do;
%if "&prior_mid" ne "" %then %do;
%TimeCalc(&prior_mid, &mid_datetime, diff=&DIFF);
%let dif_m=NOTE: &timediff since the last mid point.;
%let flag_m=1;
%end;
%if %symexist(beg_datetime) %then %do;
%TimeCalc(&beg_datetime, &mid_datetime, diff=&DIFF);
%let dif_s=NOTE: &timediff since the beginning.;
%let flag_s=1;
%end;
%symdel timediff / nowarn;
%end;
%else %if &period=End %then %do;
%if %symexist(mid_datetime) %then %do;
%TimeCalc(&mid_datetime, &end_datetime, diff=&DIFF);
%let dif_m=NOTE: &timediff since the last mid point.;
%let flag_m=1;
%end;
%if %symexist(beg_datetime) %then %do;
%TimeCalc(&beg_datetime, &end_datetime, diff=&DIFF);
%let dif_s=NOTE: &timediff since the beginning.;
%let flag_s=1;
%end;
%symdel timediff / nowarn;
%end;
/****************************************************************************
Output Log
****************************************************************************/
%if &log=Y %then %do;
data _period_;
format Datetime datetime20. Type $10. Note $100.;
Datetime = "&&&period._datetime"dt;
Type = "&period_long";
Note = "¬e";
run;
/* Append to master */
proc append base = &out data = _period_;
run;
/* Drop temporary table */
proc sql;
drop table _period_;
quit;
%end;
%put ;
%if &flag_m=1 %then %put &dif_m;
%if &flag_s=1 %then %put &dif_s;
%put &time;
%put ;
%mend time;