Advertisement
thomas_baldi

Project 5 SAS code

Dec 7th, 2020 (edited)
2,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 8.00 KB | None | 0 0
  1. *Macro to sort the ds by nvar;
  2. %macro sort (ds,nvar);
  3.  proc sort data = &ds;
  4.  by &nvar;
  5.  run;
  6.  %mend;
  7.  
  8. *Recall the macro;
  9. %sort(project.Product_dim,Product_ID);
  10. %sort(project.Product_list,Product_ID);
  11. %sort(project.Shipped,Product_ID);
  12.  
  13. *Format;
  14. proc format;
  15.     value $prod_line "Children"="CH"
  16.                      "Clothes & Shoes"="CL_SH"
  17.                      "Outdoors"="OU"
  18.                      "Sports"="SP"
  19.                      ;
  20.     value $country " "="_Missing_"
  21.                    "AU"="Australia"
  22.                    "BE"="Belgium"
  23.                    "CA"="Canada"
  24.                    "DK"="Denmark"
  25.                    "ES"="Spain"
  26.                    "FR"="France"
  27.                    "GB"="Great Britain"
  28.                    "NL"="Netherlands"
  29.                    "NO"="Norway"
  30.                    "PT"="Portugal"
  31.                    "SE"="Sweden"
  32.                    "US"="United States"
  33.                    ;
  34. run;
  35.    
  36. *Create permanent library;
  37. libname project "C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto";
  38.  
  39. *Final Dataset;
  40. data project.report;
  41. merge project.Product_dim project.Product_list project.Shipped;
  42. by Product_ID;
  43. keep Product_ID Product_line Product_name supplier_country ship_date quantity price price_num price_cat tot;
  44.  
  45. format Product_line $prod_line. ship_date DDMMYYD10. supplier_country $country.;
  46.  
  47. price_num=input(price,Dollar8.2);
  48.  
  49. if price_num=. then price_cat='';
  50. else if price_num <=10.35 then price_cat='1';
  51. else if price_num>10.35 and price_num<=21.35 then price_cat='2';
  52. else if price_num>21.35 and price_num<=46.85 then price_cat='3';
  53. else if price_num>46.85 then price_cat='4';
  54. tot=price_num*quantity;
  55.  
  56. run;
  57.  
  58. *table quantile;
  59. ods html close;
  60. ods pdf file="C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto\report_quantile.pdf";
  61.  
  62. proc means data=project.report q1 median q3;
  63. var price_num;
  64. output out=project.report2;
  65. run;
  66.  
  67. ods pdf close;
  68. ods html ;
  69.  
  70. *Remove missing;
  71. data project.report_missing;
  72. set project.report;
  73. where quantity ne .;
  74. run;
  75.  
  76. %sort(project.report,supplier_country);
  77.  
  78. *Summary statistics;
  79. proc means data = project.report noprint;
  80.   by supplier_country;
  81.   var price_num;
  82.   output out = project.report_trial  
  83.                                      mean = mn
  84.                                      std  = sd
  85.                                      min = inf
  86.                                      max = sup
  87.                                      ;                   
  88. run;
  89.  
  90.  
  91. %sort(project.report_missing,product_id ship_date);
  92.  
  93. *Dataset with total expenditure;
  94. data project.f_purchase;
  95. set project.report_missing;
  96. by product_id;
  97.  
  98. total_expenditure=price_num*quantity;
  99. format total_expenditure dollar8.2;
  100.  
  101. if first.product_id then output;
  102. keep Product_ID quantity price total_expenditure supplier_country ;
  103. run;
  104.  
  105. *Dataset test;
  106. data project.test;
  107. set project.f_purchase;
  108. keep total_expenditure supplier_country expenditure_cat;
  109. format expenditure_cat $6.;
  110. if total_expenditure=. then expenditure_cat='';
  111. else if total_expenditure <50 then expenditure_cat="Low";
  112. else if total_expenditure <100 then expenditure_cat="Medium";
  113. else expenditure_cat="High";
  114. run;
  115.  
  116.  
  117. *table fisher;
  118. ods html close;
  119. ods pdf file="C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto\fs.pdf";
  120.  
  121. proc freq data=project.test;
  122. table supplier_country*expenditure_cat / fisher;
  123. run;
  124.  
  125. ods pdf close;
  126. ods html;
  127.  
  128. *table summary statistics;
  129. ods html close;
  130. ods pdf file="C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto\report_trial_2.pdf";
  131. title'Summary Statistics of prices by country';
  132. options nodate nonumber;
  133.  
  134. proc report data=project.report_trial  missing
  135.  
  136. style(report)=[rules=groups]
  137. style(header)=[font_face=times font_weight=bold font_size=8pt]
  138. style(column)=[font_face=times font_size=7pt]
  139. ;
  140. column supplier_country _freq_ mn sd inf sup;
  141.        
  142.     define supplier_country /order 'Country' STYLE(column)=[cellwidth=2 CM just=center font_weight=bold];
  143.     define _freq_       /display   'N. of Purchase' STYLE(column)=[cellwidth=2 CM just=center];
  144.     define mn           /display  'Mean' STYLE(column)=[cellwidth=2 CM just=center];
  145.     define sd           /display  "Standard Deviation" STYLE(column)=[cellwidth=2 CM just=center];
  146.     define inf      /display  "Minimum" STYLE(column)=[cellwidth=2 CM just=center ];
  147.     define sup          /display  "Maximum" STYLE(column)=[cellwidth=2 CM just=center];
  148.    
  149.     endcomp;
  150. run;
  151. title;
  152. ods pdf close;
  153. ods html ;
  154. *---------------------*;
  155.  
  156. *table var grouped by product_line, without missing;
  157. ods listing close;
  158. ods pdf file="C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto\report_trial_1.pdf";
  159. options nodate nonumber;
  160.  
  161. proc report data=project.report_missing  missing
  162.  
  163. style(report)=[rules=groups]
  164. style(header)=[background=white font_face=times font_weight=bold font_size=8pt]
  165. style(column)=[font_face=times font_size=7pt]
  166. ;
  167. column Product_ID Product_line Product_name supplier_country ship_date quantity price price_cat;
  168.        
  169.     define Product_line /order noprint 'Line' STYLE(column)=[cellwidth=2 CM just=center font_weight=bold ];
  170.     define Product_ID       /display   'ID' STYLE(column)=[cellwidth=2 CM just=center];
  171.     define Product_name             /display  'Name' STYLE(column)=[cellwidth=2 CM just=center];
  172.     define supplier_country         /display  "Country" STYLE(column)=[cellwidth=2 CM just=center];
  173.     define ship_date        /display  "Shipping date" STYLE(column)=[cellwidth=2 CM just=center ];
  174.     define quantity         /display  "Quantity" STYLE(column)=[cellwidth=2 CM just=center];
  175.     define price            /display  "Price" STYLE(column)=[cellwidth=2 CM just=center];
  176.     define price_cat            /display  "Price category" STYLE(column)=[cellwidth=2 CM just=center];
  177.  
  178.     compute before product_line / style={font_face=times font_size=8pt font_weight=bold just=left};
  179.         line '';
  180.         line @2 "Line:  " product_line  $50.;
  181.     endcomp;
  182.  
  183.     compute before _page_ / style={font_face=times font_size=14pt font_weight=bold};
  184.         line "Purchased products grouped by line";
  185.     endcomp;
  186.  
  187.     endcomp;
  188. run;
  189.  
  190.  ods pdf close;
  191.  ods listing ;
  192. *---------------------*;
  193.  
  194. *table purchase;
  195. ods listing close;
  196. ods pdf file="C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto\report_trial_3.pdf";title;
  197. options nodate nonumber;
  198.  
  199. proc report data=project.f_purchase  
  200.  
  201. style(report)=[rules=groups]
  202. style(header)=[background=white font_face=times font_weight=bold font_size=8pt]
  203. style(column)=[font_face=times font_size=7pt]
  204. ;
  205. column Product_ID quantity price total_expenditure;
  206.        
  207.     define Product_ID /display  'ID' STYLE(column)=[cellwidth=2 CM just=center];
  208.     define quantity         /display   'Quantity' STYLE(column)=[cellwidth=2 CM just=center];
  209.     define total_expenditure            /display  'Total Expenditure' STYLE(column)=[cellwidth=2 CM just=center];
  210.     define price            /display  "Price" STYLE(column)=[cellwidth=2 CM just=center];
  211.    
  212.     compute before _page_ / style={font_face=times font_size=14pt font_weight=bold};
  213.         line "Total expenditure related to the first purchase (by shipping date)";
  214.     endcomp;
  215.  
  216.     endcomp;
  217. run;
  218.  
  219.  ods pdf close;
  220.  ods listing ;
  221. *---------------------*;
  222.  
  223. *table total expenditure;
  224. ods listing close;
  225. ods pdf file="C:\Users\Irene\Desktop\STATISTICAL SCIENCES UNIBO\Statistical Software\Progetto\report_trial_4.pdf";title;
  226. options nodate nonumber;
  227.  
  228. proc report data=project.project.report2  
  229.  
  230. style(report)=[rules=groups]
  231. style(header)=[background=white font_face=times font_weight=bold font_size=8pt]
  232. style(column)=[font_face=times font_size=7pt]
  233. ;
  234. column Product_ID quantity price total_expenditure;
  235.        
  236.     define Product_ID /display  'ID' STYLE(column)=[cellwidth=2 CM just=center];
  237.     define quantity         /display   'Quantity' STYLE(column)=[cellwidth=2 CM just=center];
  238.     define total_expenditure            /display  'Total Expenditure' STYLE(column)=[cellwidth=2 CM just=center];
  239.     define price            /display  "Price" STYLE(column)=[cellwidth=2 CM just=center];
  240.    
  241.     compute before _page_ / style={font_face=times font_size=14pt font_weight=bold};
  242.         line "Total expenditure related to the first purchase (by shipping date)";
  243.     endcomp;
  244.  
  245.     endcomp;
  246. run;
  247.  
  248.  ods pdf close;
  249.  ods listing ;
  250. *---------------------*;
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement