Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. options compress=yes;
  2.  
  3. /* Wyciagniecie wszystkich danych z pliku linia po linii*/
  4. %macro getdata();
  5. data dict.full_data(keep=col1 prefix compress=yes);
  6. infile "C:\Users\splszw\Documents\EWID\plik swd ktory mamy docelowo zaimportowac\2261kopia.swd"
  7. firstobs = 2
  8. missover
  9. flowover
  10. DSD
  11. lrecl = 32767;
  12. format col1 $500.;
  13. format prefix $5.;
  14.  
  15. input ;
  16.  
  17. col1 = _infile_;
  18. temp = scan(col1,1,',');
  19. temp2 = scan(col1,1,';');
  20.  
  21. /* if temp = "TD"*/
  22. /* then flag = 1;*/
  23.  
  24. if temp in ("B","TD","TP","RO","RD","RP","RC","D","P","WG","W")
  25. then do;
  26. prefix = temp;
  27. output;
  28. end;
  29.  
  30. else if temp2 in("X","GL","GX")
  31. then do;
  32. prefix = temp2;
  33. output;
  34. end;
  35.  
  36.  
  37. run;
  38.  
  39. %mend getdata;
  40.  
  41.  
  42. /* Pobranie listy kolumn z prefiksem "B" i listy slownikow z prefiksem "TD" */
  43. %macro get_col_list();
  44.  
  45. data
  46. &dict_list(keep=dict_id dict_name)
  47. &col_list(keep=col_id col_name)
  48. ;
  49. set dict.full_data(where=(prefix in ("TD","B","WG")));
  50.  
  51.  
  52. length dict_id $10;
  53. length dict_name $100;
  54. length col_id $10;
  55. length col_name $100;
  56.  
  57. if scan(col1,1,',')="TD" then do;
  58. dict_id = scan(col1,2,',');
  59. dict_name = compress(tranwrd(trim(upcase(strip(scan(col1,2,';')))),' ','_'),"ABCDEFGHIJKLMNOPQRSTUVWXYZ_","kis");
  60. dict_type = strip(scan(col1,3,','));
  61. output &dict_list;
  62.  
  63. end;
  64. if scan(col1,1,',')="B" then do;
  65. col_id = scan(col1,2,',');
  66. col_name = compress(tranwrd(trim(upcase(strip(scan(col1,2,';')))),' ','_'),"ABCDEFGHIJKLMNOPQRSTUVWXYZ_","kis");
  67. output &col_list;
  68.  
  69. end;
  70.  
  71. run;
  72.  
  73. %mend get_col_list;
  74.  
  75.  
  76. %macro create_tables();
  77.  
  78.  
  79. /* Create dictionaries definition */
  80.  
  81. data dicts;
  82. set dict.full_data;
  83. length table_name $20;
  84. retain table_name;
  85. length col_id $20;
  86. length col_name $32;
  87.  
  88.  
  89. if prefix = "TD" then do;
  90. table_name=scan(col1,2,',');
  91. end;
  92. if prefix = "TP" then do;
  93. col_id = strip(scan(scan(col1,1,';'),2,','));
  94. col_name = substr(compress(tranwrd(trim(upcase(strip(scan(col1,2,';')))),' ','_'),"ABCDEFGHIJKLMNOPQRSTUVWXYZ_","kis"),1,32);
  95. keep table_name col_id col_name relation;
  96. output dicts;
  97. end;
  98.  
  99. run;
  100.  
  101.  
  102.  
  103. /* Lista zmian nazw kolumn */
  104.  
  105.  
  106. data _null_;
  107. set dict.dict_list;
  108. temp = "ewid." || compress(dict_id);
  109. tab_name = upcase(compress(dict_id));
  110. tab = compress(temp); ;
  111.  
  112.  
  113. /* Create table*/
  114. call execute(
  115. 'data ' !! tab !! ';
  116. length col_name $300;
  117. set work.dicts;
  118. where table_name = "' !! dict_id !! '";
  119. keep col_id col_name;
  120. run;'
  121. );
  122.  
  123. /* Transpose dataset */
  124. call execute(
  125. 'proc transpose data = ' !! tab !! ' out = ' !! tab !! '(drop=_NAME_);
  126. id col_id;
  127. var col_name;
  128. run;'
  129. );
  130.  
  131. /* Add extra columns */
  132. call execute(
  133. 'data ' !! tab !! ';
  134. set ' !! tab !! ';
  135. length RECORD_ID $20;
  136. RECORD_ID = "";
  137. run;'
  138. );
  139.  
  140. /* Clear datasets */
  141. /* call execute(*/
  142. /* 'proc sql;*/
  143. /* delete from ' !! tab !! ';*/
  144. /* run;'*/
  145. /* );*/
  146.  
  147.  
  148. run;
  149.  
  150.  
  151. %mend create_tables;
  152.  
  153.  
  154.  
  155.  
  156.  
  157. %macro values_to_add();
  158.  
  159.  
  160. data dict.values_to_output(keep=table_name output_line)
  161. &relation_list(keep=relation_table relation_table_id relation_id foreign_table foreign_row)
  162. ;
  163. set dict.full_data(
  164. keep=col1 prefix
  165. where= (prefix in ("RO","RD","RP","RC","D","WG","X"))
  166. );
  167.  
  168. length table_name $20;
  169. retain table_name;
  170. length record_id $20;
  171. retain record_id;
  172. length output_line $500;
  173. retain output_line;
  174. length relation_id $10;
  175. length relation_name $100;
  176.  
  177.  
  178. if prefix in ("RO" ,"RD","RP","RC")
  179. then do;
  180. table_name = "ewid." || scan(col1,2,',');
  181. record_id = scan(col1,3,',');
  182. output_line = "RECORD_ID = ";
  183. output_line = strip(output_line) || '"' || strip(record_id) || '"; ';
  184. end;
  185.  
  186.  
  187. if prefix="WG" then do;
  188. relation_table = table_name;
  189. relation_table_id = record_id;
  190.  
  191. relation_id = scan(col1,2,',');
  192. foreign_table = strip(scan(col1,3,','));
  193. foreign_row =scan(scan(col1,1,';'),4,',');
  194. output &relation_list;
  195. end;
  196.  
  197.  
  198. if prefix = "D" then do;
  199. line = compress(scan(col1,4,','),'"','i');
  200. column_name = scan(col1,2,',');
  201.  
  202. output_line = strip(output_line) || strip(column_name) || '="' || strip(line) || '"; ';
  203. end;
  204.  
  205. if
  206. prefix = "X" and
  207. table_name ne ''
  208. then do;
  209. output dict.values_to_output;
  210.  
  211.  
  212.  
  213. end;
  214. run;
  215.  
  216.  
  217.  
  218. %mend values_to_add;
  219.  
  220.  
  221. %macro prepare_to_insert();
  222. proc sort data=dict.values_to_output out=tables_unique (keep=table_name) nodupkey;
  223. by table_name;
  224. run;
  225.  
  226. /* proc sql;*/
  227. /* select distinct scan(table_name,2,'.') into :table_list separated by " " */
  228. /* from tables_unique;*/
  229. /* run;*/
  230.  
  231.  
  232. data tables_unique;
  233. set tables_unique;
  234. tab = compress(scan(table_name,2,'.'));
  235. call execute('data ' !! compress(tab) !! ';
  236. set dict.values_to_output;
  237. where table_name = "' !! table_name !! '";
  238. run;'
  239. );
  240. run;
  241.  
  242. data temp;
  243. set tables_unique;
  244. call execute(cats('%input_data(',tab,')'));
  245. run;
  246.  
  247. %macro input_data(tab_name);
  248. data _null_;
  249. set &tab_name;
  250. call execute(
  251. '
  252. data ' !! table_name !! '(drop = output_line);
  253. set ' !! table_name !! 'end = eof;
  254. output;
  255. if eof = 1 then do;
  256. '!! output_line !!'
  257. output;
  258. end;
  259.  
  260.  
  261. run;
  262. '
  263. );
  264. run;
  265. %mend input_data();
  266.  
  267. %mend prepare_to_insert;
  268.  
  269.  
  270.  
  271.  
  272. %macro rename_cols(table_name);
  273.  
  274.  
  275. proc sort data=dicts ;
  276. by table_name col_name;
  277. run;
  278.  
  279. data temp(keep=table_name col_id col_name);
  280. set dicts;
  281. length col $32;
  282. retain col;
  283. if _N_ ne 1 then
  284. if col = col_name then col_name = compress(substr(col,1,31)||'1');
  285. output;
  286. col = col_name;
  287. run;
  288.  
  289.  
  290. data temp(keep=table_name line);
  291. set work.temp;
  292. line = strip(trim(col_id) || '=' || trim(col_name));
  293. run;
  294.  
  295. proc sort data=temp ;
  296. by table_name;
  297. run;
  298.  
  299.  
  300. data temp2;
  301. set temp;
  302. by table_name;
  303.  
  304. length rename_list $500;
  305. retain rename_list " ";
  306.  
  307. if first.table_name then rename_list = trim(line);
  308. else
  309. rename_list = trim(line) || " " || rename_list;
  310.  
  311. if last.table_name then do;
  312. call execute(
  313. 'proc datasets library = ewid nolist;
  314. modify ' !! table_name !! ';
  315. rename ' !! rename_list !! ';
  316. quit;'
  317. );
  318. output;
  319. end;
  320.  
  321. run;
  322.  
  323.  
  324.  
  325. %mend rename_cols;
  326.  
  327.  
  328.  
  329. %macro create_geodata();
  330. data ewid.geo_data(keep=table_name row_id p g );
  331. set full_data(
  332. keep=col1
  333. where=(scan(col1,1,',') in ('RO','RD','RP','RC','P')));
  334.  
  335. length table_name $30;
  336. retain table_name;
  337. length row_id $30;
  338. retain row_id;
  339.  
  340. if
  341. scan(col1,1,',') = "RO" or
  342. scan(col1,1,',') = "RD" or
  343. scan(col1,1,',') = "RP" or
  344. scan(col1,1,',') = "RC"
  345. then do;
  346. table_name = scan(col1,2,',');
  347. row_id = scan(col1,3,',');
  348. end;
  349.  
  350. if scan(col1,1,',') = "P" and scan(col1,2,',') = "G" then do;
  351. p = scan(col1,3,',');
  352. g = scan(col1,4,',');
  353. output;
  354. end;
  355.  
  356. run;
  357.  
  358. %mend create_geodata;
  359.  
  360.  
  361.  
  362. %getdata
  363. %get_col_list
  364. %create_tables
  365. %insert_values
  366. %create_geodata
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386. data part_data(keep=col1 prefix encoding="utf-8" compress=yes);
  387. infile "C:\Users\splszw\Documents\EWID\maly plik swd i raporty wygenerowane\226101_1_0005_005konw.swd"
  388. firstobs = 2
  389. missover
  390. flowover
  391. DSD
  392. lrecl = 32767;
  393. format col1 $500.;
  394.  
  395. input ;
  396.  
  397. col1 = _infile_;
  398. temp = scan(col1,1,',');
  399. temp2 = scan(col1,1,';');
  400.  
  401. /* if temp = "TD"*/
  402. /* then flag = 1;*/
  403.  
  404. if temp in ("B","TD","TP","RO","RD","RP","RC","D","P","WG","W")
  405. then do;
  406. prefix = temp;
  407. output;
  408. end;
  409.  
  410. else if temp2 in("X","GL","GX")
  411. then do;
  412. prefix = temp2;
  413. output;
  414. end;
  415.  
  416.  
  417. run;
  418.  
  419.  
  420. data values_to_output(keep=table_name output_line)
  421. ;
  422. set part_data(
  423. keep=col1 prefix
  424. where= (prefix in ("RO","RD","RP","RC","D","WG","X"))
  425. );
  426.  
  427. length table_name $20;
  428. retain table_name;
  429. length record_id $20;
  430. retain record_id;
  431. length output_line $500;
  432. retain output_line;
  433. length relation_id $10;
  434. length relation_name $100;
  435.  
  436.  
  437. if prefix in ("RO" ,"RD","RP","RC")
  438. then do;
  439. table_name = "ewid." || scan(col1,2,',');
  440. record_id = scan(col1,3,',');
  441. output_line = "RECORD_ID = ";
  442. output_line = strip(output_line) || '"' || strip(record_id) || '"; ';
  443. end;
  444.  
  445.  
  446. if prefix = "D" then do;
  447. line = compress(scan(col1,4,','),'"','i');
  448. column_name = scan(col1,2,',');
  449.  
  450. output_line = strip(output_line) || strip(column_name) || '="' || strip(line) || '"; ';
  451. end;
  452.  
  453. if
  454. prefix = "X" and
  455. table_name ne ''
  456. then do;
  457. output values_to_output;
  458.  
  459.  
  460. end;
  461. /* end;*/
  462. run;
  463.  
  464.  
  465.  
  466.  
  467. proc sort data=values_to_output out=tables_unique (keep=table_name) nodupkey;
  468. by table_name;
  469. run;
  470.  
  471. proc sql;
  472. select distinct scan(table_name,2,'.') into :table_list separated by " "
  473. from tables_unique;
  474. run;
  475.  
  476.  
  477. data tables_unique;
  478. set tables_unique;
  479. tab = compress(scan(table_name,2,'.'));
  480. call execute('data ' !! compress(tab) !! ';
  481. set values_to_output;
  482. where table_name = "' !! table_name !! '";
  483. run;'
  484. );
  485. run;
  486.  
  487. data temp;
  488. set tables_unique;
  489. call execute(cats('%input_data(',tab,')'));
  490. run;
  491.  
  492.  
  493. %macro input_data(tab_name);
  494. data _null_;
  495. set &tab_name;
  496. call execute(
  497. '
  498. data ' !! table_name !! '(drop = output_line);
  499. set ' !! table_name !! 'end = eof;
  500. output;
  501. if eof = 1 then do;
  502. '!! output_line !!'
  503. output;
  504. end;
  505.  
  506.  
  507. run;
  508. '
  509. );
  510. run;
  511. %mend input_data();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement