Attention : This code requires administrator privileges.
The script runs a series of tests to validate the behavior of the `%mp_lockanytable` macro. It starts by creating a control table (`work.controller`) and validating its structure. Then, it creates a test table (`tmp.sometable`), locks it, and verifies that the 'LOCKED' status is correctly recorded. It then unlocks the table and ensures that the status changes to 'UNLOCKED'. An error case is also tested by attempting to unlock a non-existent table, ensuring that the program does not stop. Finally, it checks for macro variable leakage after calling the main macro.
Data Analysis
Type : CREATION_INTERNE
All data used is created by the script itself. This includes the table to be locked (`tmp.sometable`), the lock control table (`work.controller`), and several small verification tables (`work.checkds*`) used for assertions.
1 Code Block
Macro Data
Explanation : The `%mp_coretable` macro creates the `work.controller` table intended to track lock status. The `%mp_assertcols` macro is then used to validate that this control table contains all required columns.
Copied!
%mp_coretable(LOCKTABLE,libds=work.controller)
%mp_assertcols(work.controller,
cols=lock_status_cd lock_lib lock_ds lock_user_nm lock_ref lock_pid
lock_start_dttm lock_end_dttm,
test=ALL,
desc=check all control columns exist
)
Explanation : This block enables on-the-fly directory creation, allocates a `tmp` library in the session's temporary folder, and then creates a `tmp.sometable` table for testing purposes.
Copied!
options dlcreatedir;
libname tmp "%sysfunc(pathname(work))/tmp";
data tmp.sometable;
x=1;
run;
1
options dlcreatedir;
2
LIBNAME tmp "%sysfunc(pathname(work))/tmp";
3
DATA tmp.sometable;
4
x=1;
5
RUN;
3 Code Block
Macro Data
Explanation : The `%mp_lockanytable` macro is called to lock the `tmp.sometable` table. Validation tables are created (`work.checkds1`, `work.checkds2`) and the `%mp_assertcolvals` macro verifies that the table name and 'LOCKED' status are correctly recorded in the control table.
Copied!
%mp_lockanytable(LOCK,lib=tmp,ds=sometable,ref=This Ref, ctl_ds=work.controller)
data work.checkds1;
checkval='SOMETABLE';
run;
%mp_assertcolvals(work.controller.lock_ds,
checkvals=work.checkds1.checkval,
desc=table is captured in lock,
test=ANYVAL
)
data work.checkds2;
checkval='LOCKED';
run;
%mp_assertcolvals(work.controller.lock_status_cd,
checkvals=work.checkds2.checkval,
desc=code is captured in lock,
test=ANYVAL
)
Explanation : The `tmp.sometable` table is unlocked via `%mp_lockanytable`. The script then uses `%mp_assertcolvals` to verify that the status in the control table is updated to 'UNLOCKED'.
Copied!
%mp_lockanytable(UNLOCK,lib=tmp,ds=sometable,ref=bye, ctl_ds=work.controller)
data work.checkds3;
checkval='UNLOCKED';
run;
%mp_assertcolvals(work.controller.lock_status_cd,
checkvals=work.checkds3.checkval,
desc=Ref is captured in unlock,
test=ANYVAL
)
Explanation : This block tests an error case by attempting to unlock a non-existent table. The `%mp_assert` macro validates that this operation executes without fatal error, by checking that the system return code `&syscc` remains 0.
Copied!
%mp_lockanytable(UNLOCK,lib=no,ds=doesnotexist,ref=bye, ctl_ds=work.controller)
%mp_assert(
iftrue=(&syscc=0),
desc=Ability to unlock a table that was never locked,
outds=work.test_results
)
desc=Ability to unlock a TABLE that was never locked,
6
outds=work.test_results
7
)
6 Code Block
Macro
Explanation : This block tests for macro variable 'leakage'. `%mp_assertscope(SNAPSHOT)` takes a snapshot of existing macro variables, `%mp_lockanytable` is executed, then `%mp_assertscope(COMPARE)` compares the new state to the previous one to ensure no local macro variables have been created globally.
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.