Advertisement
Guest User

asdf

a guest
Jan 25th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 11.92 KB | None | 0 0
  1. /*Kod - projekt zaliczeniowy z przedmiotu Podstawowe i zaawansowane programowanie w SAS*/
  2. /*(c) Karol Przanowski*/
  3. /*kprzan@sgh.waw.pl*/
  4.  
  5. options compress=yes;
  6.  
  7. %let dir_projekt=/folders/myfolders/projektPD/ProjektVin/dane;
  8.  
  9. libname wej "&dir_projekt" compress=yes;
  10.  
  11. /*1 KROK - Vintage macro*/
  12.  
  13. /* Creating lists variables */
  14. %let a = '1#2#3';
  15. %let b = 'ins#css#12';
  16.  
  17. %macro vintage;
  18. /* iterating through vins */
  19. %do i = 1 %to 3;
  20. /* iterating through credit types */
  21. %do j = 1 %to 3;
  22.  
  23. %let due = %scan (&a,&i,'#');
  24. %let prod = %scan (&b,&j,'#');
  25.  
  26. data vint&due._∏
  27. set wej.Transactions;
  28. seniority = intck('month', input(fin_period, yymmn6.), input(period, yymmn6.));
  29. vin&due =(due_installments>=&due);
  30. output;
  31. /* counting vins according rto seniorities */
  32. if status in ('B','C') and period<='200812' then do;
  33.     n_steps=intck('month',input(period,yymmn6.),input('200812',yymmn6.));
  34.         do i=1 to n_steps;
  35.         period=put(intnx('month',input(period,yymmn6.),1,'end'),yymmn6.);
  36.         seniority=intck('month',input(fin_period,yymmn6.),input(period,yymmn6.));
  37.         output;
  38.     end;
  39. end;
  40.  
  41. keep aid product fin_period vin&due seniority;
  42. run;
  43.  
  44. /* dividing into types of credits */
  45. data vint&due._&prod;
  46. set vint&due._&prod;
  47. /* zastanawiam się nad porzuceniem tego 12 dla ograniczenia ewentualnego pszypału  */
  48. if 12  = &prod then output;
  49. else if product="&prod" then output;
  50. drop product;
  51. run;
  52.  
  53. /* chyab nie rozumiem po co to jest  */
  54. /* counts how many times vin conditions are met, statistically, in every seniority and fin period combination  */
  55. /* zmienilbym tu nazwę vintagr, jest niestandardowa i nie widze jej logiki  */
  56. proc means data=vint&due._&prod noprint nway;
  57. class fin_period seniority;
  58. var vin&due;
  59. output out=vintagr&due._&prod (drop=_freq_ _type_) n()=production mean()=vintage&due;
  60. format vintage&due nlpct12.2;
  61. run;
  62.  
  63. /* displaying number of credits given */
  64. proc means data=vint&due._&prod noprint nway;
  65. class fin_period;
  66. var vin&due;
  67. output out=production(drop=_freq_ _type_) n()=production;
  68. where seniority=0;
  69. run;
  70.  
  71. /* Preparing data for plotting vintages */
  72. proc transpose data=vintagr&due._&prod out=vintage&due._&prod prefix=After_ suffix=_months;
  73. by fin_period;
  74. var vintage&due;
  75. id seniority;
  76. run;
  77.  
  78. data vintage&due._&prod;
  79. set vintage&due._&prod;
  80. drop _NAME_;
  81. run;
  82.  
  83.  
  84. /* filtering records with desired seniority */
  85. /* tu bym duzo pokombinował, to nasze główne pole do popisu*/
  86. data vin&due._&prod;
  87. set vint&due._&prod;
  88. if seniority in (3, 6, 9, 12);
  89. run;
  90.  
  91. /* sprawdziłbym co tu się dzieje, duża szansa spytania nas o to */
  92. /* data must be sorted to transpose it again */
  93. proc sort data = vin&due._&prod out=vin&due._&prod;
  94. by aid;
  95. run;
  96.  
  97. /* transforming into table where one record represents one credit with its chosen seniorities vins */
  98. proc transpose data=vin&due._&prod out=vin&due._&prod (drop=_name_) prefix=vin&due._;
  99. by aid;
  100. var vin&due;
  101. id seniority;
  102. run;
  103.  
  104. /* słabo rozumiem co tu się odbywa */
  105. /* merging obtained information with production table */
  106. proc sql noprint;
  107. create table production&due._&prod as
  108. select a.*, b.*
  109. from wej.production a
  110. left join vin&due._&prod b
  111. on a.aid=b.aid;
  112. quit;
  113.  
  114.  
  115. /* dividing tables into credit types? */
  116. data wej.production&due._&prod;
  117. set production&due._&prod;
  118. if product = "&prod" or 12 = &prod;
  119. run;
  120. %end;
  121. %end;
  122. %mend;
  123.  
  124.  
  125.  
  126. %vintage;
  127.  
  128.  
  129. /* preparing macrovariables for tree method */
  130. %let zb=wej.Production3_12;
  131. %put &zb;
  132. %let tar=vin3_12;
  133. %put &tar;
  134. /* poważnie bym sie zastanowil nad zmiana nazwy zmienne_int_ord*/
  135. /* writing variables from productions into macrovariable zmienne_int_ord */
  136.  
  137. proc sql noprint;
  138. select name
  139. into :zmienne_int_ord separated by ' '
  140. from dictionary.columns where libname='WEJ' and memname='PRODUCTION' and type='num';
  141. quit;
  142. /* number of variables in macrovariable */
  143. %let il_zm = &sqlobs;
  144.  
  145. %put ***&il_zm***&zmienne_int_ord;
  146.  
  147. /* tree parameters */
  148. /* tu znowu trzeba tym powachlować i potestować */
  149. /*maksymalna liczba podzia��w minus 1*/
  150. %let max_il_podz=2;
  151. /*minimalna liczba obs w li�ciu*/
  152. %let min_percent=3;
  153.  
  154. /*running tree.sas*/
  155. /* trzeba dokładnie rozgryźc to source2, wszędzie jest, ja bym na ich miejscu o to pytał  */
  156. %include "&dir.tree.sas" / source2;
  157.  
  158. /*Formatting Podzialy_int_niem into variable which can be passed into macrovariable*/
  159. /* tą nazwę koniecznei trzeba zmienic, najlepiej na coś z angielskiegoi nie 'key' */
  160. data wyj.warunek;
  161. set wyj.Podzialy_int_niem;
  162. low = scan(war,1,'<');
  163. if substr(low,1,1) = 'n' then low = '1=1and';
  164. if substr(low,1,3) ne '1=1' then low = catt(lowcase(zmienna),'>',low,'and');
  165. high = scan(war,2,'=');
  166. if high='' then high ='1=1';
  167. else high = catt(lowcase(zmienna),'=<',high);
  168. if substr(war,1,1) = 'n' then miss = cats('not missing(',lowcase(zmienna),')and');
  169. warunek = catt(miss,low,high);
  170. warunek = transtrn(warunek,'and',' and ');
  171. warunek = transtrn(warunek,'=<',' =< ');
  172. warunek = transtrn(warunek,'>',' > ');
  173. klucz=catt(lowcase(zmienna),'#',grp,'#',warunek);
  174. keep zmienna grp warunek klucz;
  175. run;
  176.  
  177.  
  178. /* saving formated data into macrovariable */
  179. /* znowu trzeba zmienic nazwe  */
  180. proc sql noprint;
  181. select klucz into :warunki separated by '^'
  182. from wyj.warunek;
  183. quit;
  184.  
  185. %put &warunki;
  186. %put &sqlobs;
  187.  
  188. /*defining boundaries for divideing columns*/
  189. %macro kategoryzacja;
  190. data wyj.vin_kat;  
  191. set wej.Production3_12;
  192. %do i = 1 %to &sqlobs;
  193. %let warunek = %scan(%scan(&warunki,&i,'^'),3,'#');
  194. %let zmienna = %scan(%scan(&warunki,&i,'^'),1,'#');
  195. %let grupa = %scan(%scan(&warunki,&i,'^'),2,'#');
  196. /*dividing columns into groups  */
  197. if &warunek then &zmienna=&grupa;
  198. %end;
  199. output;
  200. run;
  201. %mend;
  202. %kategoryzacja;
  203.  
  204.  
  205.  
  206. /* finding Cramer output */
  207. %let gr = act#ags#agr#app;
  208. %macro vcram;
  209. %do i = 1 %to 4;
  210. %let j = %scan(&gr,&i,'#');
  211. data wyj.vin_grp_&j;
  212. set wyj.vin_kat;
  213. keep vin3_12 &j.:;
  214. run;
  215.  
  216. proc contents data=wyj.vin_grp_&j out=varlist_&j noprint;
  217. run;
  218.  
  219. proc sql noprint;
  220. select name into:zm_&j separated by '#'
  221. from varlist_&j;
  222. quit;
  223. %let liczba=&sqlobs;
  224.  
  225. data vcram_&j;
  226. length vcram _cramv_ 8 zmienna $30;
  227. run;
  228.  
  229. %do k = 1 %to (&liczba-1);
  230. %let zm=%scan(&&zm_&j,&k,'#');
  231. proc freq data = wyj.vin_grp_&j noprint;
  232. tables vin3_12*&zm /chisq;
  233. output out = vcram_&zm cramv;
  234. run;
  235.  
  236. data vcram_&zm;
  237. set vcram_&zm;
  238. zmienna = "&zm";
  239. vcram = abs(_cramv_);
  240. run;
  241.  
  242. data vcram_&j;
  243. set vcram_&j vcram_&zm;
  244. run;
  245.  
  246. %end;
  247.  
  248. /* sorting obtaied cramer outputs */
  249. proc sort data = vcram_&j out = wyj.vcram_&j;
  250. by descending vcram;
  251. run;
  252.  
  253. /*taking highest 5 cramer outputs*/
  254. data wyj.vcram_&j;
  255. set wyj.vcram_&j (obs = 5);
  256. run;
  257.  
  258. %end;
  259. %mend;
  260. %vcram;
  261.  
  262.  
  263. /* ======================= PLOTTING =========================== */
  264. proc sql;
  265. create table wej.selected_v
  266. as
  267. select * from wyj.vcram_act
  268. union all
  269. select * from wyj.vcram_agr
  270. union all
  271. select * from wyj.vcram_ags
  272. union all
  273. select * from wyj.vcram_app;
  274. quit;
  275.  
  276. proc sql;
  277. select zmienna into :selected_v separated by '#'
  278. from wej.selected_v;
  279. quit;
  280. %let ilosc_v = &sqlobs;
  281. %put ***&selected_v***&ilosc_v***;
  282.  
  283.  
  284. %macro vintage_gr;
  285. %do a=1 %to &ilosc_v;
  286. %let nazwa_v = %scan(&selected_v,&a,'#');
  287. data wej.vin_grp;
  288. set wyj.vin_kat (keep = &nazwa_v aid cid);
  289. run;
  290.  
  291. proc sql noprint;
  292. create table wej.liczba_kat as
  293. select count(distinct &nazwa_v) as kategorie
  294. from wej.vin_grp;
  295. quit;
  296.  
  297. proc sql noprint;
  298. select kategorie into :il_kat
  299. from wej.liczba_kat;
  300. quit;
  301.  
  302. proc sql;
  303. create table wej.transactions_plus
  304. as
  305. select a.*,  b.*
  306. from wej.transactions a
  307. inner join
  308. wej.vin_grp b
  309. on a.aid=b.aid and a.cid=b.cid;
  310. quit;
  311.  
  312. %do due = 1 %to 3;
  313. %do kat = 1 %to &il_kat;
  314.  
  315. data vint_&due._&nazwa_v._&kat;
  316. set wej.transactions_plus;
  317. seniority = intck('month', input(fin_period, yymmn6.), input(period, yymmn6.));
  318. vin&due =(due_installments>=&due);
  319. if &nazwa_v = &kat;
  320. output;
  321.  
  322. if status in ('B','C') and period<='200812' then do;
  323.     n_steps=intck('month',input(period,yymmn6.),input('200812',yymmn6.));
  324.         do i=1 to n_steps;
  325.         period=put(intnx('month',input(period,yymmn6.),1,'end'),yymmn6.);
  326.         seniority=intck('month',input(fin_period,yymmn6.),input(period,yymmn6.));
  327.         output;
  328.     end;
  329. end;
  330.  
  331. keep aid &nazwa_v fin_period vin&due seniority;
  332. run;
  333.  
  334. proc means data=vint_&due._&nazwa_v._&kat noprint nway;
  335. class fin_period seniority;
  336. var vin&due;
  337. output out=vintagr&due._&nazwa_v._&kat (drop=_freq_ _type_) n()=production mean()=vintage&due;
  338. format vintage&due nlpct12.2;
  339. run;
  340.  
  341. proc transpose data=vintagr&due._&nazwa_v._&kat out=wyj.vintage&due._&nazwa_v._&kat prefix=After_ suffix=_months;
  342. by fin_period;
  343. var vintage&due;
  344. id seniority;
  345. run;
  346.  
  347. data wyj.vintage&due._&nazwa_v._&kat;
  348. set wyj.vintage&due._&nazwa_v._&kat;
  349. drop _NAME_;
  350. run;
  351.  
  352. proc export data=wyj.vintage&due._&nazwa_v._&kat dbms=xlsx outfile='D:\SAS_projekt\proba projektu\vintage_kat' replace;
  353. sheet=vin&due._&nazwa_v._&kat;
  354. run;
  355.  
  356.  
  357. %end;
  358. %end;
  359. %end;
  360. %mend;
  361. %vintage_gr;
  362.  
  363.  
  364.  
  365. %macro vintage_gr_char;
  366. data wyj.vin_kat;
  367. set wyj.vin_kat;
  368. if app_char_branch='Radio-TV' then app_char_branch='RadioTV';
  369. run;
  370.  
  371. proc freq data=wyj.vin_kat noprint;
  372. tables app_char_branch /out=kat_znakowe;
  373. run;
  374.  
  375. proc sql noprint;
  376. select app_char_branch into :kat_znakowe separated by '#'
  377. from kat_znakowe;
  378. quit;
  379.  
  380. %let il_kat=&sqlobs;
  381.  
  382. data wej.vin_grp;
  383. set wyj.vin_kat (keep = app_char_branch aid cid);
  384. run;
  385.  
  386.  
  387. proc sql;
  388. create table wej.transactions_plus
  389. as
  390. select a.*,  b.*
  391. from wej.transactions a
  392. inner join
  393. wej.vin_grp b
  394. on a.aid=b.aid and a.cid=b.cid;
  395. quit;
  396.  
  397. %do due=1 %to 3;
  398. %do i=1 %to &il_kat;
  399. %let kat = %scan(&kat_znakowe,&i,'#');
  400.  
  401. data vint_&due._app_char_branch_&kat;
  402. set wej.transactions_plus;
  403. seniority = intck('month', input(fin_period, yymmn6.), input(period, yymmn6.));
  404. vin&due =(due_installments>=&due);
  405. if app_char_branch = &kat;
  406. output;
  407.  
  408. if status in ('B','C') and period<='200812' then do;
  409.     n_steps=intck('month',input(period,yymmn6.),input('200812',yymmn6.));
  410.         do i=1 to n_steps;
  411.         period=put(intnx('month',input(period,yymmn6.),1,'end'),yymmn6.);
  412.         seniority=intck('month',input(fin_period,yymmn6.),input(period,yymmn6.));
  413.         output;
  414.     end;
  415. end;
  416.  
  417. keep aid app_char_branch fin_period vin&due seniority;
  418. run;
  419.  
  420. proc means data=vint_&due._app_char_branch_&kat noprint nway;
  421. class fin_period seniority;
  422. var vin&due;
  423. output out=vintagr&due._app_char_branch_&kat (drop=_freq_ _type_) n()=production mean()=vintage&due;
  424. format vintage&due nlpct12.2;
  425. run;
  426.  
  427. proc transpose data=vintagr&due._app_char_branch_&kat out=wyj.vintage_&due._app_char_branch_&kat prefix=After_ suffix=_months;
  428. by fin_period;
  429. var vintage&due;
  430. id seniority;
  431. run;
  432.  
  433. data wyj.vintage_&due._app_char_branch_&kat;
  434. set wyj.vintage_&due._app_char_branch_&kat;
  435. drop _NAME_;
  436. run;
  437.  
  438. proc export data=wyj.vintage_&due._app_char_branch_&kat dbms=xlsx outfile='D:\SAS_projekt\proba projektu\vintage_kat' replace;
  439. sheet=vin&due._app_char_branch_&kat;
  440. run;
  441. %end;
  442. %end;
  443. %mend;
  444. %vintage_gr_char;
  445.  
  446.  
  447.  
  448.  
  449. /*Analiza vintage zbiorczo i w podziale na grupy produkt�w - export excel*/
  450. %macro vintage2;
  451. %do i = 1 %to 3;
  452. %do j = 1 %to 3;
  453. %let kred = %scan(&b,&j,'#');
  454.  
  455. proc export data = vintage&i._&kred dbms=xlsx outfile  = 'C:\Users\patry_000\Desktop\Programowanie w SAS\PROJEKT KO�COWY\Vintage og�em i w grupach produktowych\vintagev3.xlsx' replace;
  456. sheet = vintage&i._&kred;
  457. run;
  458.  
  459. %end;
  460. %end;
  461. %mend;
  462. %vintage2;
  463.  
  464.  
  465. /* PROGNOZA dla Vintage3_12 */
  466. PROC IMPORT OUT= WORK.a DATAFILE= "C:\Users\Admin\Desktop\podstawowe i zaawansowane programowanie i statystyka w SAS\Zeszyt1.xlsx"
  467.             DBMS=xlsx REPLACE;
  468.      SHEET="Arkusz1";
  469.      GETNAMES=YES;
  470. RUN;
  471.  
  472. data aa;
  473. merge work.Vintage3_12 work.a;
  474. by fin_period;
  475. output;
  476. run;
  477.  
  478. proc arima data=Aa;
  479. identify var=production;
  480. estimate p=2;
  481. identify var=After_12_months crosscorr=production;
  482. estimate p=1 q=1 input=production;
  483. forecast lead=12 out=results;
  484. run;
  485. quit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement