Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_prevobs.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_prevobs macro
4
5 <h4> SAS Macros </h4>
6 @li mp_prevobs.sas
7 @li mp_assert.sas
8
9 @details mp_prevobs emits statements into a data step, so it is exercised
10 inside one and the result is checked after the step. FETCH -1 reinstates the
11 observation one row before the current one.
12
13**/
14
15data work.prevobs_src;
16 do id=1 to 5;
17 lbl=byte(96+id);
18 output;
19 end;
20run;
21
22data work.prevobs_out;
23 set work.prevobs_src;
24 %mp_prevobs(INIT,history=2)
25 if _n_=4 then do;
26 %mp_prevobs(FETCH,-1)
27 restored=lbl;
28 restored_id=id;
29 output;
30 end;
31run;
32
33proc sql noprint;
34 select count(*) into: n_rows from work.prevobs_out;
35 select restored into: restored from work.prevobs_out(obs=1);
36 select restored_id into: restored_id from work.prevobs_out(obs=1);
37quit;
38
39%mp_assert(
40 iftrue=(&n_rows=1),
41 desc=Checking that the fetch of the previous observation succeeded,
42 outds=work.test_results
43)
44
45%mp_assert(
46 iftrue=(%sysfunc(strip(&restored))=c and &restored_id=3),
47 desc=Checking that the observation one row back is reinstated,
48 outds=work.test_results
49)