/* 2018.03.12. 6. gyakorlat - SAS GRAPH, Grafikus megjelenitesek SAS Studio-val */ /* SASHELP.CLASS adatallomany megismerese */ proc print data=sashelp.class; run; /* Sajat hasonlo adatok letrehozasa */ data xclass; input nem $ 1. kor korcsop mag kg; cards; 1 15 2 154 58 2 16 3 154 53 1 15 2 160 60 1 15 2 153 57 1 14 2 159 58 2 15 2 150 49 2 16 3 162 60 1 16 3 168 64 2 17 3 170 59 2 13 1 149 53 2 14 2 152 53 1 15 2 170 70 1 14 2 174 72 1 13 1 168 67 2 12 1 146 46 1 12 1 145 47 2 12 1 150 50 2 12 1 149 50 1 13 1 151 54 2 15 2 162 56 1 16 3 177 74 2 14 2 160 60 ; run; /* Hasonlo, mint a Task\Data\Transform data, lehet egyenileg definialni - nem pont igy */ data xclass; set xclass; if nem = 1 then nem_f = "Ferfi"; else nem_f = "No"; if korcsop = 1 then kcs = "12-13"; else if korcsop = 2 then kcs = "14-15"; else kcs = "16"; run; /********** Leiro statisztika *************/ /* Tasks\Statistics\Data exploration - Classification variables: diszkret valtozok, mozaic plot */ options validvarname=any; ods noproctitle; ods graphics / imagemap=off; proc freq data=WORK.XCLASS; ods select MosaicPlot; tables nem_f*kcs / plots=mosaicplot(square); run; ods graphics / imagemap=on; /* Continuous variables: kor, mag, kg */ options validvarname=any; ods noproctitle; ods graphics / imagemap=on; /* Scatter plot matrix macro */ %macro scatterPlotMatrix(xVars=, title=, groupVar=); proc sgscatter data=WORK.XCLASS; matrix &xVars / %if(&groupVar ne %str()) %then %do; group=&groupVar legend=(sortorder=ascending) %end; diagonal=(histogram kernel normal); title &title; run; title; %mend scatterPlotMatrix; /* Histogram and box plot template */ proc template; define statgraph histobox; dynamic AVAR; begingraph; entrytitle "Distribution of " eval(catq('q', colname(AVAR))); layout lattice / rows=2 columndatarange=union rowgutter=0 rowweights=(0.75 0.25); layout overlay / yaxisopts=(offsetmax=0.1) xaxisopts=(display=none); layout gridded / columns=2 border=on autoalign=(topright topleft); %let _lft = halign=left; %let _rgt = halign=right; entry &_lft "Mean"; entry &_rgt eval(strip(put(mean(AVAR), best.))); entry &_lft "Std Dev"; entry &_rgt eval(strip(put(stddev(AVAR), best.))); entry &_lft "N"; entry &_rgt eval(strip(put(n(AVAR), best.))); endlayout; histogram AVAR /; endlayout; layout overlay /; BoxPlot Y=AVAR / orient=horizontal; endlayout; endlayout; endgraph; end; run; %scatterPlotMatrix(xVars=kor mag kg, title="Scatter plot matrix", groupVar=); proc sgrender data=WORK.XCLASS template=histobox; dynamic AVAR='kor'; run; proc sgrender data=WORK.XCLASS template=histobox; dynamic AVAR='mag'; run; proc sgrender data=WORK.XCLASS template=histobox; dynamic AVAR='kg'; run; /* Csoportonkenti eloszlasok: kg ~ kcs Continuous variable: kg Classification variable: kcs */ options validvarname=any; ods noproctitle; ods graphics / imagemap=on; /* Histogram (one-way or two-way) */ %macro DEHisto(data=, avar=, classVar=); %local i numAVars numCVars cVar cVar1 cVar2; %let numAVars=%Sysfunc(countw(%str(&avar), %str( ), %str(q))); %let numCVars=%Sysfunc(countw(%str(&classVar), %str( ), %str(q))); %if(&numAVars>0 & &numCVars>0) %then %do; %if(&numCVars=1) %then %do; %let cVar=%scan(%str(&classVar), 1, %str( ), %str(q)); proc sql noprint; select count(distinct &cVar) into :nrows from &data; quit; /* One-way histogram */ proc univariate data=&data noprint; var &avar; class &cVar; histogram &avar / nrows=&nrows normal(noprint) kernel; inset mean std n / position=ne; run; %end; %else %do; /* One-way histogram of each class variable */ %do i=1 %to %eval(&numCVars); %let cVar=%scan(%str(&classVar), &i, %str( ), %str(q)); proc sql noprint; select count(distinct &cVar) into :nrows from &data; quit; proc univariate data=&data noprint; var &avar; class &cVar; histogram &avar / nrows=&nrows normal(noprint) kernel; inset mean std n / position=ne; run; %end; /* Two-way histogram */ %let cVar1=%scan(%str(&classVar), 1, %str( ), %str(q)); %let cVar2=%scan(%str(&classVar), 2, %str( ), %str(q)); proc sql noprint; select count(distinct &cVar1) into :nrows from &data; quit; proc sql noprint; select count(distinct &cVar2) into :ncols from &data; quit; proc univariate data=&data noprint; var &avar; class &cVar1 &cVar2; histogram &avar / nrows=&nrows ncols=&ncols normal(noprint) kernel; inset mean std n / position=ne; run; %end; %end; %mend DEHisto; %DEHisto(data=WORK.XCLASS, avar=kg, classVar=kcs); proc sort data=WORK.XCLASS out=WORK.TempSorted4877; by kcs; run; proc boxplot data=WORK.TempSorted4877; plot (kg)*kcs / boxstyle=schematic; run; proc datasets library=WORK noprint; delete TempSorted4877; run; /********* Summary statistics *********/ /* Analysis variable: mag * Classification variable: kcs */ ods noproctitle; ods graphics / imagemap=on; proc means data=WORK.XCLASS chartype mean std min max median n nmiss vardef=df skewness kurtosis p5 q1 q3 p95 qmethod=os; var mag; class kcs; run; proc univariate data=WORK.XCLASS vardef=df noprint; var mag; class kcs; histogram mag / normal(noprint) kernel; inset mean std min max median n nmiss skewness kurtosis p5 q1 q3 p95 / position=ne; run; proc sort data=WORK.XCLASS out=WORK.TempSorted2236; by kcs; run; proc boxplot data=WORK.TempSorted2236; plot (mag)*kcs / boxstyle=schematic; inset mean stddev min max nobs / position=ne; insetgroup mean stddev min max n q1 q2 q3 / position=top; run; proc datasets library=WORK noprint; delete TempSorted2236; run; /*********** Distribution analysis ***********/ /* Analysis variable: mag * Classification variable: nem_f */ ods noproctitle; ods graphics / imagemap=on; /*** Exploring Data ***/ proc univariate data=WORK.XCLASS; ods select Histogram; var mag; class nem_f; histogram mag / normal kernel; inset n / position=ne; run; proc univariate data=WORK.XCLASS; ods select Histogram GoodnessOfFit QQPlot; var mag; /*** Checking for Normality ***/ histogram mag / normal(mu=est sigma=est); inset mean std skewness kurtosis n / position=ne; qqplot mag / normal(mu=est sigma=est); inset mean std skewness kurtosis n / position=nw; run; /***************** Tasks\Graph\Bar Chart *****************/ /* Category: nem_f * Subcategory: kcs */ ods graphics / reset width=6.4in height=4.8in imagemap; proc sgplot data=WORK.XCLASS; title height=14pt "Gyakorisági oszlopdiagram"; footnote2 justify=left height=12pt "Szeged, 2018. március 12."; vbar nem_f / group=kcs groupdisplay=cluster; yaxis grid; run; ods graphics / reset; title; footnote2; /* Kis modositassal: Ne a gyakorisagokat, hanem folytonos valtozo: kg atlag ertekeit mutassuk +/- SE */ ods graphics / reset width=6.4in height=4.8in imagemap; proc sgplot data=WORK.XCLASS; title height=14pt "Átlag-szórás diagram (átlag +/- SE)"; footnote2 justify=left height=12pt "Szeged, 2018. március 12."; vbar nem_f / response=kg group=kcs groupdisplay=cluster datalabel limits=both limitstat=stderr stat=mean; yaxis grid; run; ods graphics / reset; title; footnote2; /* BARWIDTH parameter allitasa + szurkearnyalatos kep beallitasa: ODS style=journal es kimentese: gpath, imagename */ /* Google: sas sgplot vbar http://support.sas.com/documentation/cdl/en/grstatproc/62603/HTML/default/viewer.htm#vbar-stmt.htm https://support.sas.com/resources/papers/proceedings15/2441-2015.pdf */ ODS LISTING GPATH = '/home/laszlo.anna/a2018'; ods listing style=journal; ods graphics / reset width=6.4in height=4.8in imagemap imagename='plot1'; proc sgplot data=WORK.XCLASS; title height=14pt "Átlag-szórás diagram (átlag +/- SE)"; footnote2 justify=left height=12pt "Szeged, 2018. március 12."; vbar nem_f / response=kg group=kcs groupdisplay=cluster datalabel limits=both limitstat=stderr stat=mean barwidth=.8 ; yaxis grid; run; ods graphics / reset; title; footnote2; /*********** Scatter plot **********/ /* X: mag * Y: kg * Group: nem_f * esetleges modositas: reg ... / ... degree=1 clm='CL Mean' */ ods graphics / reset width=6.4in height=4.8in imagemap; proc sgplot data=WORK.XCLASS; reg x=mag y=kg / nomarkers group=nem_f; scatter x=mag y=kg / group=nem_f; xaxis grid; yaxis grid; run; ods graphics / reset; /************ Snippets\Graph *************/ /*--Bar Panel--*/ title 'Mileage by Origin and Type'; proc sgpanel data=sashelp.cars(where=(type in ('Sedan' 'Sports'))) noautolegend; panelby Type / novarname columns=2 onepanel; vbar origin / response=mpg_city stat=mean group=origin; rowaxis grid; run; /*--Fit Plot--*/ title 'Mileage by Horsepower for USA'; proc sgplot data=sashelp.cars(where=(origin='USA')); reg x=horsepower y=mpg_city / degree=2 clm='CL Mean'; keylegend / location=inside position=topright across=1; xaxis grid; yaxis grid; run;