Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.48 KB | None | 0 0
  1.  
  2. /*---------------------------------------------------------------------------*/
  3. /* */
  4. /* ADSspace */
  5. /* */
  6. /* This macro return a summary of tables in schema ADSDATA */
  7. /* */
  8. /* Maintenance Log: */
  9. /* 2014-05-14 */
  10. /*---------------------------------------------------------------------------*/
  11.  
  12. %macro adsspace (
  13. order = , /*--- 'time, size or both/all' output options ---*/
  14. excel = NO, /*--- 'YES' to create a excel ouput ---*/
  15. email = NO, /*--- 'YES' to genarate an email ---*/
  16. maintenance = NO
  17. );
  18.  
  19. /*----------------------------------------------------------------------------*/
  20. /* Set ASDP connection to database if you did not set ODBC info in ProdSAS */
  21. /*----------------------------------------------------------------------------*/
  22.  
  23. /*
  24. %let ODBC_User = <your-asdp-username>;
  25. %let ODBC_pw = <your-asdp-password>;
  26.  
  27. %let ODBC_PROD = ASDP;
  28. */
  29.  
  30. /*----------------------------------------------------------------------------*/
  31. /* Initialize parameters */
  32. /*----------------------------------------------------------------------------*/
  33.  
  34. %let order = %QTRIM(%upcase(&order)) ;
  35. %if &order ^= and
  36. &order ^= TIME and
  37. &order ^= SIZE and
  38. &order ^= BOTH and
  39. &order ^= ALL
  40. %then %do;
  41. %put **************** invalid order option;
  42. %let order =;
  43. %end;
  44.  
  45. %if &order = %then %do;
  46. options nomprint ;
  47. options nonotes ;
  48. %end;
  49.  
  50.  
  51. %let excel = %upcase(&excel) ;
  52. %let email = %upcase(&email) ;
  53. %let maintenance = %upcase(&maintenance) ;
  54. %let ODBC_User = %upcase(&ODBC_User) ;
  55.  
  56. %let adsdata_total_space=225; /*Total space capacity in schema ADSDATA */
  57. %let adsdata_space_delimiter=5; /*Space reserved for emergency uses */
  58.  
  59. /*----------------------------------------------------------------------------*/
  60. /* Start Job */
  61. /*----------------------------------------------------------------------------*/
  62. %StartJob(client=ADSdata,job=ADSDATA_space_summary,storesaslog=no); run;
  63.  
  64. /*----------------------------------------------------------------------------*/
  65. /* Create required directories and allocate required files */
  66. /*----------------------------------------------------------------------------*/
  67. %let Out_data = &Databasesroot\&GBClient\&GBJobNam\;
  68.  
  69. /*----------------------------------------------------------------------------*/
  70. /* Create output directory if it does not exist */
  71. /*----------------------------------------------------------------------------*/
  72. %CreateDirectory(&Out_data); run;
  73.  
  74. %alloclib(OUT_Data,&Out_Data.);
  75.  
  76. /*----------------------------------------------------------------------------*/
  77. /* Load Qurey in Macro */
  78. /*----------------------------------------------------------------------------*/
  79. %macro SQLCode1;
  80.  
  81. select a.tabschema as schema_name,
  82. a.tabname as table_name,
  83. a.owner, a.type, a.create_time, a.stats_time, a.colcount,
  84. card as rows_in_table,
  85. (float(a.fpages)*PAGESIZE/1024) as size_Kb,
  86. decimal(float(a.fpages)*PAGESIZE/(power(1024,3)),8,2) as size_Gb
  87. from syscat.tables a,
  88. syscat.tablespaces b
  89. where a.TBSPACEID=b.TBSPACEID and
  90. tabschema='ADSDATA'
  91. order by a.owner, a.tabname
  92.  
  93. %mend sqlcode1;
  94.  
  95.  
  96. /*----------------------------------------------------------------------------*/
  97. /* Run Qurey and creat SAS dataset with results */
  98. /*----------------------------------------------------------------------------*/
  99.  
  100. proc sql;
  101. connect to DB2 (USER=&ODBC_User password= "&ODBC_pw." database=&ODBC_PROD );
  102.  
  103. create table out_data.adsdata_spacesummary as
  104. select * from connection to db2
  105. (%sqlcode1);
  106.  
  107. disconnect from db2;
  108. quit;
  109.  
  110. /*----------------------------------------------------------------------------*/
  111. /* Display necessary outputs */
  112. /*----------------------------------------------------------------------------*/
  113.  
  114. proc sql noprint;
  115. select count(*), sum(size_gb), &adsdata_total_space -&adsdata_space_delimiter -sum(size_gb) into :ntable, :totalsize, :leftsize
  116. from out_data.adsdata_spacesummary;
  117. quit;
  118.  
  119. proc sql noprint;
  120. select count(*), sum(size_gb) into :user_ntable, :user_totalsize
  121. from out_data.adsdata_spacesummary
  122. where owner = "&ODBC_User";
  123. quit;
  124.  
  125.  
  126.  
  127. %put -----> A total of %QTRIM(&ntable) tables/views in Schema ADSDATA;
  128. %put -----> User &ODBC_User has %QTRIM(&user_ntable) tables/views, uses %QTRIM(&user_totalsize) Gb space;
  129. %put -----> Schema ASDDATA: %QTRIM(&totalsize)/&adsdata_total_space Gb is used, %QTRIM(&leftsize) Gb is free (&adsdata_space_delimiter Gb reserved);
  130.  
  131. /*----------------------------------------------------------------------------*/
  132. /* Display opional outputs */
  133. /*----------------------------------------------------------------------------*/
  134.  
  135.  
  136. /* ADSDATA summary order by create time */
  137. %macro SQLCode2;
  138. select *
  139. from out_data.adsdata_spacesummary
  140. order by create_time
  141. %mend SQLCode2;
  142.  
  143.  
  144. %if &order=TIME or &order=BOTH or &order=ALL %then %do;
  145. title 'ADSDATA summary order by table created time';
  146. proc sql;
  147. %SQLCode2;
  148. quit;
  149. %end;
  150.  
  151. /* ADSDATA summary order by size in kb */
  152. %macro SQLCode3;
  153. select *
  154. from out_data.adsdata_spacesummary
  155. order by size_kb desc
  156. %mend SQLCode3;
  157.  
  158. %if &order=SIZE or &order=BOTH or &order=ALL %then %do;
  159. title 'ADSDATA summary order by table size';
  160. proc sql;
  161. %SQLCode3;
  162. quit;
  163. %end;
  164.  
  165. /* ADSDATA summary grouped by owner */
  166. %macro SQLCode4;
  167. select owner, total_tables, space_used_gb, free_space, last_time_used format=datetime20. from (
  168. (select 1 as order_no,
  169. owner,
  170. count(*) as total_tables,
  171. sum(size_gb) as space_used_gb,
  172. . as free_space,
  173. max (stats_time) as last_time_used
  174. from out_data.adsdata_spacesummary
  175. group by owner
  176. having owner = "&ODBC_User"
  177. ) union all
  178. (
  179. select 2 as order_no,
  180. owner,
  181. count(*) as total_tables,
  182. sum(size_gb) as space_used_gb,
  183. . as free_space,
  184. max (stats_time) as last_time_used
  185. from out_data.adsdata_spacesummary
  186. group by owner
  187. having owner ^= "&ODBC_User"
  188. ) union all
  189. (
  190. select 3 as order_no,
  191. 'ALL USERS' as owner,
  192. count(*) as total_tables,
  193. sum(size_gb) as space_used_gb,
  194. 225 - sum(size_gb) as free_space,
  195. max (stats_time) as last_time_used
  196. from out_data.adsdata_spacesummary
  197. ) )
  198. order by order_no, owner
  199. %mend SQLCode4;
  200.  
  201. %if &order ^= %then %do;
  202. title 'ADSDATA table owner summary';
  203. proc sql;
  204. %SQLCode4;
  205. quit;
  206. %end;
  207.  
  208. /*--------------------------------------------------------------------*/
  209. /* Creat copy of Template for loading */
  210. /*--------------------------------------------------------------------*/
  211. %if &excel = YES or &email = YES or &maintenance = YES %then %do;
  212.  
  213. %mfiledelete(&outputroot\ADSdata_summary.xlsx); run;
  214. %mfilecopy(&prodsasroot\&GBClient\Templates\ADSdata_template.xlsx,
  215. &outputroot\ADSdata_summary.xlsx); run;
  216. /*----------------------------------------------------------------------------*/
  217. /* Create Excel output */
  218. /*----------------------------------------------------------------------------*/
  219.  
  220. libname xls pcfiles path="&outputroot\ADSdata_summary.xlsx";
  221.  
  222. proc datasets lib = xls nolist;
  223. delete CREATE SIZE OWNER;
  224. quit;
  225.  
  226. proc sql;
  227.  
  228. create table xls.CREATE as
  229. %SQLCode2;
  230.  
  231. create table xls.SIZE as
  232. %SQLCode3;
  233.  
  234. create table xls.OWNER as
  235. %SQLCode4;
  236.  
  237. quit;
  238.  
  239. libname xls clear;
  240. %end;
  241.  
  242.  
  243. /*----------------------------------------------------------------------------*/
  244. /* Create Automatic Email */
  245. /*----------------------------------------------------------------------------*/
  246.  
  247. %if &email = YES %then %do;
  248.  
  249.  
  250. %let emailsubject = ADSDATA Schema Summary;
  251. %let emailfrom = &ODBC_User.@<****.ca>;
  252. %let emailto = &ODBC_User.@<****.ca>;
  253.  
  254. %allocfile(msg,Email_msg.txt,o); run;
  255.  
  256. DATA _NULL_;
  257. FILE "&MSG" NOTITLES RECFM=V lrecl=200;
  258. put "ADSDATA Schema Summary:";
  259. put " ";
  260. put "A total of %QTRIM(&ntable) tables/views in Schema ADSDATA ";
  261. put "User &ODBC_User has %QTRIM(&user_ntable) tables/views, uses %QTRIM(&user_totalsize) Gb space ";
  262. put "Schema ASDDATA: %QTRIM(&totalsize)/&adsdata_total_space Gb is used, %QTRIM(&leftsize) Gb is free (&adsdata_space_delimiter Gb reserved) ";
  263. put " ";
  264. put "Attached is spreadsheet containing the ADSDATA Schema Summary";
  265. put " ";
  266. put "Please review the attached.";
  267. put " ";
  268. put " ";
  269. put "********************************************************************************************";
  270. put "Please do not respond to this email address as it is not monitored. ";
  271. put "For inquiries, please contact me at rzhao@transunion.ca ";
  272. put "********************************************************************************************";
  273. put " ";
  274. put "%sysfunc(date(),weekdate17.) %sysfunc(time(),time8.0)";
  275. put "Thank you.";
  276. put "&ODBC_User. ";
  277. RUN;
  278.  
  279. %emailmessage(subject = &emailsubject,
  280. to = &emailto,
  281. from = &emailfrom,
  282. messagefile=&msg,
  283. attach = &outputroot\ADSdata_summary.xlsx); run;
  284.  
  285.  
  286. %end;
  287.  
  288. /*----------------------------------------------------------------------------*/
  289. /* Create Database Maintenance Email */
  290. /*----------------------------------------------------------------------------*/
  291.  
  292. %if &maintenance = YES %then %do;
  293.  
  294. proc sql;
  295. create table out_data.adsdata_owners as
  296. select owner, count(*)+1 as count_tables from out_data.adsdata_spacesummary group by owner;
  297. quit;
  298.  
  299. proc sql noprint;
  300. select count(*) into :count_owner from out_data.adsdata_owners;
  301. quit;
  302.  
  303. %do i=1 %to &count_owner;
  304. data _null_;
  305. set out_data.adsdata_owners;
  306. if _n_= &i then do;
  307. call symput ('owner',trim(left(owner)));
  308. call symput ('count_tables',trim(left(count_tables)));
  309. end;
  310. run;
  311.  
  312. proc sql;
  313. create table out_data.adsdata_part as
  314. select * from out_data.adsdata_spacesummary where owner = "&owner";
  315. quit;
  316.  
  317. %let email_user = &owner;
  318.  
  319. %if &owner = <people do not want emails> %then %let email_user = <macro owner>;
  320.  
  321. /* %let email_user = rzhao;*/ *for test purpose;
  322.  
  323. %let emailsubject = ADS Data Warehouse Maintenance;
  324. %let emailfrom = &ODBC_User.@<****.ca>;
  325. %let emailto = &email_user.@<****.ca>;
  326.  
  327. %allocfile(msg,Email_msg.txt,o); run;
  328.  
  329. DATA _NULL_;
  330. FILE "&MSG" NOTITLES RECFM=V lrecl=200;
  331. put "Dear User &owner";
  332. put " ";
  333. put "Please note that the ADS Team is currently performing data warehouse maintenance.";
  334. put "If your following tables are no longer in use, please drop them for better perfomance of data warehouse";
  335. put " ";
  336. put "Attached is spreadsheet containing the ADSDATA Schema Summary";
  337. put " ";
  338. put "%sysfunc(date(),weekdate17.) %sysfunc(time(),time8.0)";
  339. put "Thank you.";
  340. put "&ODBC_User. ";
  341. put " ";
  342. put " ";
  343. put "------------------------- List of tables you created: -------------------------";
  344. put " ";
  345. put "TABLE_NAME SIZE_GB CREATE_TIME ";
  346. put " ";
  347. DO n=1 to &count_tables;
  348. SET out_data.adsdata_part;
  349. PUT @1'ADSDATA.' table_name @41 size_gb @51 create_time;
  350. END;
  351. RUN;
  352.  
  353. %emailmessage(subject = &emailsubject,
  354. to = &emailto,
  355. from = &emailfrom,
  356. messagefile=&msg,
  357. attach = &outputroot\ADSdata_summary.xlsx); run;
  358.  
  359. %end;
  360. %end;
  361.  
  362. /*----------------------------------------------------------------------------*/
  363. /* Set SAS option back to default */
  364. /*----------------------------------------------------------------------------*/
  365.  
  366. title;
  367. options notes mprint;
  368.  
  369.  
  370.  
  371. %mend adsspace;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement