Thursday, October 27, 2016

Graphs using SAS Programming


SAS is powerful business intelligence tools for creating a wide range of business and scientific graphs. Graph is another way to analyze the data and user can easily reach to your conclusion by just look at graph instead of seeing the large dataset.
SAS software contains collection of procedures, sas datasets containing map data, and other utility procedures and application that help manage graphical data and output.
After the version of SAS 9.2 a new family of procedures introduces for creating the statistical graphs. This family of SG procedures includes the SGPLOT, SGSCATTER and SGPANEL procedures. The SG procedures provides detail concept to create graphs commonly used in data analysis in many industries.

So let see how we use these procedures to create the graphs in SAS studio.

SGPLOT PROCEDURE:
The SGPLOT procedure is designed to create a single-celled graph, with multiple plots overlaid within a single set of axes. The SGPLOT procedure is optimized to display overload plots on a single set of axes. For Example:

Scatterplot Graph:
Scatterplot is a type of graph which is best for representing the relationship between two variables.

proc sgplot data=SASHELP.IRIS;

            /*--TITLE and FOOTNOTE--*/
            title H=12pt 'ScatterPlot Graph';

            /*--Scatter plot settings--*/
            scatter x=SepalLength y=PetalLength / group=Species
                        markerattrs=(symbol=DiamondFilled size=8) transparency=0.1   name='Scatter';

            /*--X Axis--*/
            xaxis grid;

            /*--Y Axis--*/
            yaxis grid;

            /*--Legend Settings--*/

            keylegend / location=Inside across=1;
run;
                           
Bar Chart:
Bar graphs provide a visual presentation of categorical data.
proc sgplot data=SASHELP.RETAIL;

            /*--TITLE and FOOTNOTE--*/
            title 'Bar Graph';

            /*--Bar chart settings--*/
            vbar YEAR / response=SALES stat=Mean name='Bar' fillattrs=(color=OLIVE) datalabel=sales dataskin=pressed;

            /*--Response Axis--*/
            yaxis grid;
run;



Line Graph:
Line graph is a type of graph which is used to represent the time series data.
PROC SGPLOT DATA=sashelp.tourism;
            series x=Year y= vsp / LEGENDLABEL='VSP' MARKERS LINEATTRS=(COLOR=OLIVE
                        THICKNESS=2 PATTERN=SOLID) MARKERATTRS=(COLOR=OLIVE symbol=circlefilled);
                        xaxis grid;
                        yaxis grid;
           
            series x=Year y= cpisp / LEGENDLABEL='cpisp' MARKERS LINEATTRS=(COLOR=MAROON
                        THICKNESS=2 PATTERN=SOLID) MARKERATTRS=(COLOR=MAROON symbol=circlefilled);
                        xaxis grid;
                        yaxis grid;
           
            INSET 'Tourism Data Analysis' / POSITION= topleft border;
            TITLE "Line Graph";
RUN;


By using SGPLOT procedure you can plot many graph like series plots, band plots, needle plots and vector plot etc.

SGPANEL PROCEDURE:
SGPANEL procedure created a panel for the values of one or more classification variables. Each graph in the panel can contain either a single plot or multiple overlaid plots.
title1 "Distribution of Cholesterol Levels";
proc sgpanel data=sashelp.heart;
panelby weight_status sex / layout=lattice
novarname;
hbox cholesterol / group=status;
run;


SGSCATTER PROCEDURE:
The SGSCATTER procedure creates a paneled graph for multiple combinations of variables. For example:
proc sgscatter data=sashelp.cars;
compare y=mpg_highway
x=(weight enginesize horsepower )
/ group=type;
run;



This way you can plot lots of attractive graph using SG procedure.

Tuesday, October 25, 2016

Loops in SAS

When we do programming, you want that a block of code is executed several number of times. In general statements are executed sequentially like the first statement in a function is executed first, followed by another statement and so on. In that case we need to understand the concepts of Looping. In Looping there are different keywords to define the iteration or loop statement. The most wee known statement is the “for loop” which is used by many language like C/C++, MATLA, R Java etc. There is similar concepts in SAS like other language to define the loop. But in SAS looping is done by DO Statement. And it is also called DO Loop.  There are four types of DO Statement.

DO Statement

Do Statement classifies a group of statement which execute a group of statement a certain number of times? The syntax for DO statement is
DO;
SAS Statement…;
END


An end statement represents the end of the loop for example:

DATA  _null_;
  do x = 1;
put x =;
end;
run;

Output  x = 1



The output of this program is shown in Log File on SAS. Let take an another example like if I remove the puts statements what you will get?

DATA  _null_;
  do x = 1;
put x =;
end;
run;

Output  x = 1


By default you will get the value of x equals to 1. If I assign more than one value of x like 1, 2,3  then you will see that It print the all the values of x in output.

DATA  _null_;
  do x = 1,2,3;
put x =;
end;
run;

Output x = 1
                   x = 2
                   x = 3


Now I want to apply loop in string values how can I define it.


DATA  _null_;
  do x = “AAA”,”BBBB”,”CCCC”;
put x =;
end;
run;

Output  x = AAA
                   x = BBBB
                   x = CCCC


Suppose we want to print the values from 1 to 100. In that case it really hectic to someone to assign all the values in a particular variable one by one. To solve this issues we have another solution to set the range.


DATA  _null_;
  do x = 1 to 5;
put i =;
end;
run;

output  i = 1
                   i = 2
                   i = 3
                   i = 4
                   i = 5


Iterative DO Loop

The iterative DO Loop executes the statement between DO and End repetively baes on the value of the index values. The syntax for Iterative DO Loop statement is:

DO Index-variable = Start TO Stop <By Increment>;
SAS Statement…;
END

By default, each iteration of a DO statement increments the value of the counter by 1, but you can also use the BY statement option to increment the counter by other amounts. For example, each iteration the following DATA step increments the values by 0.5. For example:

DATA  _null_;
  do i = 1 to 10 by 2;
put i =;
end;
run;

output  i = 1
                   i = 3
                   i = 5
                   i = 7
                   i = 9


DO While Loop

The loop continues till the while condition becomes False. It almost works like the iterative DO statement, except that you don’t need to specify an index-variable or start and stop. The syntax of DO While Loop is:

DO WHILE (expression)
----SAS Statement----
END;

data _null_;
n=0;
do while(n<5);
  put n=;
  n+1;
end;
run;
output  n = 1
                   n = 2
                   n = 3
                   n = 4

In while loop the expression is evaluated before the loop executes, and If the expression is false the first time it is evaluated, then the loop will not execute at all.





Creating Compelling Pie Charts in Looker: A Step-by-Step Guide with Examples

Creating Compelling Pie Charts in Looker: A Step-by-Step Guide with Examples   In the realm of data visualization, pie charts are a clas...