Advertisement
Guest User

Untitled

a guest
Jan 6th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 5.51 KB | None | 0 0
  1. *4GI;
  2. data z1;
  3. set sashelp.cars;
  4. run;
  5.  
  6. proc sql;
  7.     create table zla as
  8.     select *
  9.     from sashelp.cars;
  10.    
  11. quit;
  12.    
  13. *typowe zapytanie;
  14. proc sql;
  15. create table z2 as
  16. select*
  17. from sashelp.cars
  18. where make='Audi';
  19. quit;
  20.  
  21. *drop jako opcja zbioru;
  22. proc sql;
  23. create table z2 as
  24. select*
  25. from sashelp.cars(drop=MPG_:);
  26. quit;
  27.  
  28. *konkretne kolumny;
  29. proc sql;
  30. create table z2 as
  31. select make, model, invoice
  32. from sashelp.cars
  33. where make='Audi';
  34. quit;
  35.  
  36. *dla malych i duzych liter jednoczesnie;
  37. proc sql;
  38. create table z2 as
  39. select make, model, invoice
  40. from sashelp.cars
  41. where lowcase(make) eq 'audi';
  42. quit;
  43.  
  44. *operator OR;
  45. proc sql;
  46. create table z2 as
  47. select *
  48. from sashelp.cars
  49. where make='BMW' or make='Audi';
  50. quit;
  51.  
  52. *in;
  53. proc sql;
  54. create table z2 as
  55. select*
  56. from sashelp.cars
  57. where make in ('BMW','Audi'); *maxymalnie do 1000 wzorcow;
  58. quit;
  59.  
  60. *operator like ---samochody zaczynajace sie na a;
  61. proc sql;
  62. create table z2 as
  63. select*
  64. from sashelp.cars
  65. where make like 'A%'; /* %zero lub więcej, _-jeden*/
  66. quit;
  67.  
  68. *where kako opcja zbioru;
  69. proc sql;
  70. create table z2 as
  71. select*
  72. from sashelp.cars(where=(make eq 'BMW'))
  73. where horsepower >= 200;
  74. quit;
  75.  
  76. *length;
  77. proc sql;
  78. create table z2 as
  79. select*
  80. from sashelp.cars
  81. where length(make)=4;
  82. quit;
  83.  
  84. *distinct;
  85. proc sql;
  86. create table z2 as
  87. select distinct type
  88. from sashelp.cars;
  89. quit;
  90.  
  91. proc sql;
  92. create table z2 as
  93. select distinct make, type
  94. from sashelp.cars;
  95. quit;
  96.  
  97. *przeksztalcenia;
  98. proc sql;
  99. create table z2 as
  100. select make, invoice, 0.8*invoice as nowa_cena
  101. from sashelp.cars;
  102. quit;
  103.  
  104. *atrybuty kolumn;
  105. proc sql;
  106. create table z2 as
  107. select make length=200,
  108.     invoice,
  109.     0.8*invoice as nowa_cena label='Cena w PLN'
  110.     format commax20.2
  111. from sashelp.cars;
  112. quit;
  113.  
  114. *case when tzw else=if;
  115. proc sql;
  116. create table z3 as
  117. select make, invoice,
  118.     case
  119.         when invoice lt 40000 then 'tanie'
  120.         when invoice lt 60000 then 'przecietnie'
  121.         else 'drogie'
  122.     end as kategoria
  123. from sashelp.cars;
  124. quit;
  125.  
  126. *calculated;
  127. proc sql;
  128. create table z4a as
  129. select make, 0.8*invoice as invoice,
  130.     case
  131.         when invoice lt 40000 then 'tanie'
  132.         when invoice lt 60000 then 'przecietnie'
  133.         else 'drogie'
  134.     end as kategoria
  135. from sashelp.cars;
  136. quit;
  137.  
  138. proc sql;
  139. create table z1a as
  140. select make, invoice,
  141.     case
  142.         when calculated invoice lt 40000 then 'tanie'
  143.         when calculated invoice lt 60000 then 'przecietnie'
  144.         else 'drogie'
  145.     end as kategoria
  146. from sashelp.cars;
  147. quit;
  148.  
  149. *liczba wierszy;
  150. proc sql outobs=10;
  151. create table z2 as
  152. select*
  153. from sashelp.cars;
  154. quit;
  155.  
  156. proc sql;
  157. create table z3 as
  158. select*
  159. from sashelp.cars
  160. where monotonic()<=10;
  161. quit;
  162.  
  163. *group by;
  164. proc sql;
  165. create table z2 as
  166. select make, count(*) as liczba
  167. from sashelp.cars
  168. group by make;
  169. quit;
  170.  
  171. proc sql;
  172. create table z2a as
  173. select make, type, count(*) as liczba
  174. from sashelp.cars
  175. group by make;
  176. quit;
  177.  
  178. *kilka wyliczeń;
  179. proc sql;
  180. create table z3 as
  181. select make, count(*) as liczba,
  182. avg(invoice) as avg
  183. from sashelp.cars
  184. group by make;
  185. quit;
  186.  
  187. *group by 1;
  188. proc sql;
  189. create table z3 as
  190. select make, count(*) as liczba,
  191. avg(invoice) as avg
  192. from sashelp.cars
  193. group by 1;
  194. quit;
  195.  
  196. *where;
  197. proc sql;
  198. create table z3 as
  199. select make, count(*) as liczba
  200. from sashelp.cars
  201. where type='Sedan'
  202. group by make;
  203. quit;
  204.  
  205. *having;
  206. proc sql;
  207. create table z3a as
  208. select make, count(*) as liczba
  209. from sashelp.cars
  210. where type='Sedan'
  211. group by make
  212. having count(*)>10;
  213. quit;
  214.  
  215. *max;
  216. proc sql;
  217. create table z4 as
  218. select make, model, invoice
  219. from sashelp.cars
  220. group by make
  221. having invoice=max(invoice)
  222. order by invoice DESC;
  223. quit;
  224.  
  225. *max;
  226. proc sql;
  227. create table z4a as
  228. select make, model, invoice
  229. from sashelp.cars
  230. having invoice=max(invoice);
  231. quit;
  232.  
  233. *podzapytanie;
  234. proc sql;
  235. create table z5 as
  236. select origin, count(*) as liczba,
  237. (count(*)/(select count(*) from sashelp.cars)) * 100 as udział
  238. from sashelp.cars
  239. group by origin;
  240. quit;
  241.  
  242. *dodatkowe polecenia;
  243. proc sql;
  244. create table one(X num, A char(1));
  245. insert into one
  246.     Values(1,'a')
  247.     Values(2, 'b');
  248. quit;
  249.  
  250. proc sql;
  251. drop table one;
  252. quit;
  253.  
  254. proc sql;
  255. create table one(X num, A char(1));
  256. insert into one
  257. Values(1, 'a')
  258. Values(2, 'd')
  259. Values(4, 'b');
  260.  
  261. create table two(X num, B char(1));
  262. insert into two
  263. Values(2, 'x')
  264. Values(2, 'y')
  265. Values(3, 'z');
  266.  
  267. create table three(X num, C char(1));
  268. insert into three
  269. Values(0, 'k')
  270. Values(2, 'l')
  271. Values(6, 'j');
  272. quit;
  273.  
  274. *join;
  275. proc sql;
  276. create table l1 as
  277. select one.x as one_x, one.A as one_a, two.*
  278. from one, two;
  279. quit;
  280.  
  281. *inner join;
  282. proc sql;
  283. create table l2 as
  284. select one.x as one_x, one.A as one_a, two.*
  285. from one, two
  286. where one.x=two.x;
  287. quit;
  288.  
  289. proc sql;
  290. create table l2 as
  291. select one.x as one_x, one.A as one_a, two.*
  292. from one inner join two
  293. on (one.x=two.x);
  294. quit;
  295.  
  296. *aliasy;
  297. proc sql;
  298. create table l3 as
  299. select a.x as one_x, a.A as one_a, b.*
  300. from one as a inner join two as b
  301. on (a.x=b.x);
  302. quit;
  303.  
  304. *left join;
  305. proc sql;
  306. create table l4 as
  307. select a.x as one_x, a.A as one_a, b.*
  308. from one as a left join two as b
  309. on (a.x=b.x);
  310. quit;
  311.  
  312. *right join;
  313. proc sql;
  314. create table l5 as
  315. select a.x as one_x, a.A as one_a, b.*
  316. from one as a right join two as b
  317. on (a.x=b.x);
  318. quit;
  319.  
  320. *full join;
  321. proc sql;
  322. create table l5 as
  323. select a.x as one_x, a.A as one_a, b.*
  324. from one as a full join two as b
  325. on (a.x=b.x);
  326. quit;
  327.  
  328. *wiecej niz dwie tabele;
  329. proc sql;
  330. create table l6 as
  331. select one.x as one_x, b, c, two.x as two_x, three.x as three_x
  332. from one left join two on(one.x=two.x)
  333. full join three on (one.x=three.x);
  334. quit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement